xref: /netbsd-src/external/gpl3/binutils.old/dist/include/aout/sun4.h (revision e992f068c547fd6e84b3f104dc2340adcc955732)
175fd0b74Schristos /* SPARC-specific values for a.out files
275fd0b74Schristos 
3*e992f068Schristos    Copyright (C) 2001-2022 Free Software Foundation, Inc.
475fd0b74Schristos 
575fd0b74Schristos    This program is free software; you can redistribute it and/or modify
675fd0b74Schristos    it under the terms of the GNU General Public License as published by
775fd0b74Schristos    the Free Software Foundation; either version 3 of the License, or
875fd0b74Schristos    (at your option) any later version.
975fd0b74Schristos 
1075fd0b74Schristos    This program is distributed in the hope that it will be useful,
1175fd0b74Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1275fd0b74Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1375fd0b74Schristos    GNU General Public License for more details.
1475fd0b74Schristos 
1575fd0b74Schristos    You should have received a copy of the GNU General Public License
1675fd0b74Schristos    along with this program; if not, write to the Free Software
1775fd0b74Schristos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
1875fd0b74Schristos    MA 02110-1301, USA.  */
1975fd0b74Schristos 
2075fd0b74Schristos /* Some systems, e.g., AIX, may have defined this in header files already
2175fd0b74Schristos    included.  */
2275fd0b74Schristos #undef  TARGET_PAGE_SIZE
2375fd0b74Schristos #define TARGET_PAGE_SIZE	0x2000		/* 8K.  aka NBPG in <sys/param.h> */
2475fd0b74Schristos /* Note that some SPARCs have 4K pages, some 8K, some others.  */
2575fd0b74Schristos 
2675fd0b74Schristos #define SEG_SIZE_SPARC	TARGET_PAGE_SIZE
2775fd0b74Schristos #define	SEG_SIZE_SUN3	0x20000		/* Resolution of r/w protection hw */
2875fd0b74Schristos 
2975fd0b74Schristos #define TEXT_START_ADDR	TARGET_PAGE_SIZE	/* Location 0 is not accessible */
3075fd0b74Schristos #define N_HEADER_IN_TEXT(x) 1
3175fd0b74Schristos 
3275fd0b74Schristos /* Non-default definitions of the accessor macros... */
3375fd0b74Schristos 
3475fd0b74Schristos /* Segment size varies on Sun-3 versus Sun-4.  */
3575fd0b74Schristos 
3675fd0b74Schristos #define N_SEGSIZE(x)	(N_MACHTYPE(x) == M_SPARC?	SEG_SIZE_SPARC:	\
3775fd0b74Schristos 			 N_MACHTYPE(x) == M_68020?	SEG_SIZE_SUN3:	\
3875fd0b74Schristos 			/* Guess? */			TARGET_PAGE_SIZE)
3975fd0b74Schristos 
4075fd0b74Schristos /* Virtual Address of text segment from the a.out file.  For OMAGIC,
4175fd0b74Schristos    (almost always "unlinked .o's" these days), should be zero.
4275fd0b74Schristos    Sun added a kludge so that shared libraries linked ZMAGIC get
4375fd0b74Schristos    an address of zero if a_entry (!!!) is lower than the otherwise
4475fd0b74Schristos    expected text address.  These kludges have gotta go!
4575fd0b74Schristos    For linked files, should reflect reality if we know it.  */
4675fd0b74Schristos 
4775fd0b74Schristos #define N_SHARED_LIB(x) ((x)->a_entry < TEXT_START_ADDR \
4875fd0b74Schristos 			 && (x)->a_text >= EXEC_BYTES_SIZE)
4975fd0b74Schristos 
5075fd0b74Schristos /* This differs from the version in aout64.h (which we override by defining
5175fd0b74Schristos    it here) only for NMAGIC (we return TEXT_START_ADDR+EXEC_BYTES_SIZE;
5275fd0b74Schristos    they return 0).  */
5375fd0b74Schristos 
5475fd0b74Schristos #define N_TXTADDR(x) \
5575fd0b74Schristos     (N_MAGIC(x)==OMAGIC? 0 \
5675fd0b74Schristos      : (N_MAGIC(x) == ZMAGIC && (x)->a_entry < TEXT_START_ADDR)? 0 \
5775fd0b74Schristos      : TEXT_START_ADDR+EXEC_BYTES_SIZE)
5875fd0b74Schristos 
5975fd0b74Schristos /* When a file is linked against a shared library on SunOS 4, the
6075fd0b74Schristos    dynamic bit in the exec header is set, and the first symbol in the
6175fd0b74Schristos    symbol table is __DYNAMIC.  Its value is the address of the
6275fd0b74Schristos    following structure.  */
6375fd0b74Schristos 
6475fd0b74Schristos struct external_sun4_dynamic
6575fd0b74Schristos {
6675fd0b74Schristos   /* The version number of the structure.  SunOS 4.1.x creates files
6775fd0b74Schristos      with version number 3, which is what this structure is based on.
6875fd0b74Schristos      According to gdb, version 2 is similar.  I believe that version 2
6975fd0b74Schristos      used a different type of procedure linkage table, and there may
7075fd0b74Schristos      have been other differences.  */
7175fd0b74Schristos   bfd_byte ld_version[4];
7275fd0b74Schristos   /* The virtual address of a 28 byte structure used in debugging.
7375fd0b74Schristos      The contents are filled in at run time by ld.so.  */
7475fd0b74Schristos   bfd_byte ldd[4];
7575fd0b74Schristos   /* The virtual address of another structure with information about
7675fd0b74Schristos      how to relocate the executable at run time.  */
7775fd0b74Schristos   bfd_byte ld[4];
7875fd0b74Schristos };
7975fd0b74Schristos 
8075fd0b74Schristos /* The size of the debugging structure pointed to by the debugger
8175fd0b74Schristos    field of __DYNAMIC.  */
8275fd0b74Schristos #define EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE (24)
8375fd0b74Schristos 
8475fd0b74Schristos /* The structure pointed to by the linker field of __DYNAMIC.  As far
8575fd0b74Schristos    as I can tell, most of the addresses in this structure are offsets
8675fd0b74Schristos    within the file, but some are actually virtual addresses.  */
8775fd0b74Schristos 
8875fd0b74Schristos struct internal_sun4_dynamic_link
8975fd0b74Schristos {
9075fd0b74Schristos   /* Linked list of loaded objects.  This is filled in at runtime by
9175fd0b74Schristos      ld.so and probably by dlopen.  */
9275fd0b74Schristos   unsigned long ld_loaded;
9375fd0b74Schristos 
9475fd0b74Schristos   /* The address of the list of names of shared objects which must be
9575fd0b74Schristos      included at runtime.  Each entry in the list is 16 bytes: the 4
9675fd0b74Schristos      byte address of the string naming the object (e.g., for -lc this
9775fd0b74Schristos      is "c"); 4 bytes of flags--the high bit is whether to search for
9875fd0b74Schristos      the object using the library path; the 2 byte major version
9975fd0b74Schristos      number; the 2 byte minor version number; the 4 byte address of
10075fd0b74Schristos      the next entry in the list (zero if this is the last entry).  The
10175fd0b74Schristos      version numbers seem to only be non-zero when doing library
10275fd0b74Schristos      searching.  */
10375fd0b74Schristos   unsigned long ld_need;
10475fd0b74Schristos 
10575fd0b74Schristos   /* The address of the path to search for the shared objects which
10675fd0b74Schristos      must be included.  This points to a string in PATH format which
10775fd0b74Schristos      is generated from the -L arguments to the linker.  According to
10875fd0b74Schristos      the man page, ld.so implicitly adds ${LD_LIBRARY_PATH} to the
10975fd0b74Schristos      beginning of this string and /lib:/usr/lib:/usr/local/lib to the
11075fd0b74Schristos      end.  The string is terminated by a null byte.  This field is
11175fd0b74Schristos      zero if there is no additional path.  */
11275fd0b74Schristos   unsigned long ld_rules;
11375fd0b74Schristos 
11475fd0b74Schristos   /* The address of the global offset table.  This appears to be a
11575fd0b74Schristos      virtual address, not a file offset.  The first entry in the
11675fd0b74Schristos      global offset table seems to be the virtual address of the
11775fd0b74Schristos      sun4_dynamic structure (the same value as the __DYNAMIC symbol).
11875fd0b74Schristos      The global offset table is used for PIC code to hold the
11975fd0b74Schristos      addresses of variables.  A dynamically linked file which does not
12075fd0b74Schristos      itself contain PIC code has a four byte global offset table.  */
12175fd0b74Schristos   unsigned long ld_got;
12275fd0b74Schristos 
12375fd0b74Schristos   /* The address of the procedure linkage table.  This appears to be a
12475fd0b74Schristos      virtual address, not a file offset.
12575fd0b74Schristos 
12675fd0b74Schristos      On a SPARC, the table is composed of 12 byte entries, each of
12775fd0b74Schristos      which consists of three instructions.  The first entry is
12875fd0b74Schristos          sethi %hi(0),%g1
12975fd0b74Schristos 	 jmp %g1
13075fd0b74Schristos 	 nop
13175fd0b74Schristos      These instructions are changed by ld.so into a jump directly into
13275fd0b74Schristos      ld.so itself.  Each subsequent entry is
13375fd0b74Schristos          save %sp, -96, %sp
13475fd0b74Schristos 	 call <address of first entry in procedure linkage table>
13575fd0b74Schristos 	 <reloc_number | 0x01000000>
13675fd0b74Schristos      The reloc_number is the number of the reloc to use to resolve
13775fd0b74Schristos      this entry.  The reloc will be a JMP_SLOT reloc against some
13875fd0b74Schristos      symbol that is not defined in this object file but should be
13975fd0b74Schristos      defined in a shared object (if it is not, ld.so will report a
14075fd0b74Schristos      runtime error and exit).  The constant 0x010000000 turns the
14175fd0b74Schristos      reloc number into a sethi of %g0, which does nothing since %g0 is
14275fd0b74Schristos      hardwired to zero.
14375fd0b74Schristos 
14475fd0b74Schristos      When one of these entries is executed, it winds up calling into
14575fd0b74Schristos      ld.so.  ld.so looks at the reloc number, available via the return
14675fd0b74Schristos      address, to determine which entry this is.  It then looks at the
14775fd0b74Schristos      reloc and patches up the entry in the table into a sethi and jmp
14875fd0b74Schristos      to the real address followed by a nop.  This means that the reloc
14975fd0b74Schristos      lookup only has to happen once, and it also means that the
15075fd0b74Schristos      relocation only needs to be done if the function is actually
15175fd0b74Schristos      called.  The relocation is expensive because ld.so must look up
15275fd0b74Schristos      the symbol by name.
15375fd0b74Schristos 
15475fd0b74Schristos      The size of the procedure linkage table is given by the ld_plt_sz
15575fd0b74Schristos      field.  */
15675fd0b74Schristos   unsigned long ld_plt;
15775fd0b74Schristos 
15875fd0b74Schristos   /* The address of the relocs.  These are in the same format as
15975fd0b74Schristos      ordinary relocs.  Symbol index numbers refer to the symbols
16075fd0b74Schristos      pointed to by ld_stab.  I think the only way to determine the
16175fd0b74Schristos      number of relocs is to assume that all the bytes from ld_rel to
16275fd0b74Schristos      ld_hash contain reloc entries.  */
16375fd0b74Schristos   unsigned long ld_rel;
16475fd0b74Schristos 
16575fd0b74Schristos   /* The address of a hash table of symbols.  The hash table has
16675fd0b74Schristos      roughly the same number of entries as there are dynamic symbols;
16775fd0b74Schristos      I think the only way to get the exact size is to assume that
16875fd0b74Schristos      every byte from ld_hash to ld_stab is devoted to the hash table.
16975fd0b74Schristos 
17075fd0b74Schristos      Each entry in the hash table is eight bytes.  The first four
17175fd0b74Schristos      bytes are a symbol index into the dynamic symbols.  The second
17275fd0b74Schristos      four bytes are the index of the next hash table entry in the
17375fd0b74Schristos      bucket.  The ld_buckets field gives the number of buckets, say B.
17475fd0b74Schristos      The first B entries in the hash table each start a bucket which
17575fd0b74Schristos      is chained through the second four bytes of each entry.  A value
17675fd0b74Schristos      of zero ends the chain.
17775fd0b74Schristos 
17875fd0b74Schristos      The hash function is simply
17975fd0b74Schristos          h = 0;
18075fd0b74Schristos          while (*string != '\0')
18175fd0b74Schristos 	   h = (h << 1) + *string++;
18275fd0b74Schristos 	 h &= 0x7fffffff;
18375fd0b74Schristos 
18475fd0b74Schristos      To look up a symbol, compute the hash value of the name.  Take
18575fd0b74Schristos      the modulos of hash value and the number of buckets.  Start at
18675fd0b74Schristos      that entry in the hash table.  See if the symbol (from the first
18775fd0b74Schristos      four bytes of the hash table entry) has the name you are looking
18875fd0b74Schristos      for.  If not, use the chain field (the second four bytes of the
18975fd0b74Schristos      hash table entry) to move on to the next entry in this bucket.
19075fd0b74Schristos      If the chain field is zero you have reached the end of the
19175fd0b74Schristos      bucket, and the symbol is not in the hash table.  */
19275fd0b74Schristos   unsigned long ld_hash;
19375fd0b74Schristos 
19475fd0b74Schristos   /* The address of the symbol table.  This is a list of
19575fd0b74Schristos      external_nlist structures.  The string indices are relative to
19675fd0b74Schristos      the ld_symbols field.  I think the only way to determine the
19775fd0b74Schristos      number of symbols is to assume that all the bytes between ld_stab
19875fd0b74Schristos      and ld_symbols are external_nlist structures.  */
19975fd0b74Schristos   unsigned long ld_stab;
20075fd0b74Schristos 
20175fd0b74Schristos   /* I don't know what this is for.  It seems to always be zero.  */
20275fd0b74Schristos   unsigned long ld_stab_hash;
20375fd0b74Schristos 
20475fd0b74Schristos   /* The number of buckets in the hash table.  */
20575fd0b74Schristos   unsigned long ld_buckets;
20675fd0b74Schristos 
20775fd0b74Schristos   /* The address of the symbol string table.  The first string in this
20875fd0b74Schristos      string table need not be the empty string.  */
20975fd0b74Schristos   unsigned long ld_symbols;
21075fd0b74Schristos 
21175fd0b74Schristos   /* The size in bytes of the symbol string table.  */
21275fd0b74Schristos   unsigned long ld_symb_size;
21375fd0b74Schristos 
21475fd0b74Schristos   /* The size in bytes of the text segment.  */
21575fd0b74Schristos   unsigned long ld_text;
21675fd0b74Schristos 
21775fd0b74Schristos   /* The size in bytes of the procedure linkage table.  */
21875fd0b74Schristos   unsigned long ld_plt_sz;
21975fd0b74Schristos };
22075fd0b74Schristos 
22175fd0b74Schristos /* The external form of the structure.  */
22275fd0b74Schristos 
22375fd0b74Schristos struct external_sun4_dynamic_link
22475fd0b74Schristos {
22575fd0b74Schristos   bfd_byte ld_loaded[4];
22675fd0b74Schristos   bfd_byte ld_need[4];
22775fd0b74Schristos   bfd_byte ld_rules[4];
22875fd0b74Schristos   bfd_byte ld_got[4];
22975fd0b74Schristos   bfd_byte ld_plt[4];
23075fd0b74Schristos   bfd_byte ld_rel[4];
23175fd0b74Schristos   bfd_byte ld_hash[4];
23275fd0b74Schristos   bfd_byte ld_stab[4];
23375fd0b74Schristos   bfd_byte ld_stab_hash[4];
23475fd0b74Schristos   bfd_byte ld_buckets[4];
23575fd0b74Schristos   bfd_byte ld_symbols[4];
23675fd0b74Schristos   bfd_byte ld_symb_size[4];
23775fd0b74Schristos   bfd_byte ld_text[4];
23875fd0b74Schristos   bfd_byte ld_plt_sz[4];
23975fd0b74Schristos };
240