2 * Copyright (c) 1993 Paul Kranenburg
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Paul Kranenburg.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * $FreeBSD: src/libexec/rtld-aout/dynamic.h,v 1.5 1999/08/28 00:10:05 peter Exp $
31 * $DragonFly: src/libexec/rtld-aout/Attic/dynamic.h,v 1.3 2003/11/14 03:54:31 dillon Exp $
42 #ifndef RELOC_JMPTAB_P
44 #define RELOC_JMPTAB_P(r) ((r)->r_jmptable)
45 #define RELOC_BASEREL_P(r) ((r)->r_baserel)
46 #define RELOC_RELATIVE_P(r) ((r)->r_relative)
47 #define RELOC_COPY_P(r) ((r)->r_copy)
48 #define RELOC_LAZY_P(r) ((r)->r_jmptable)
50 #define CHECK_GOT_RELOC(r) ((r)->r_pcrel)
51 #define RELOC_PIC_TYPE(r) ((r)->r_baserel? \
52 PIC_TYPE_LARGE:PIC_TYPE_NONE)
55 #ifndef RELOC_INIT_SEGMENT_RELOC
56 #define RELOC_INIT_SEGMENT_RELOC(r)
60 #define MAX_GOTOFF(x) (LONG_MAX)
64 #define MIN_GOTOFF(x) (LONG_MIN)
68 * Internal representation of relocation types
70 #define RELTYPE_EXTERN 1
71 #define RELTYPE_JMPSLOT 2
72 #define RELTYPE_BASEREL 4
73 #define RELTYPE_RELATIVE 8
74 #define RELTYPE_COPY 16
76 #define N_ISWEAK(p) (N_BIND(p) & BIND_WEAK)
78 typedef struct localsymbol {
79 struct nzlist nzlist; /* n[z]list from file */
80 struct glosym *symbol; /* Corresponding global symbol,
82 struct localsymbol *next; /* List of definitions */
83 struct file_entry *entry; /* Backpointer to file */
84 long gotslot_offset; /* Position in GOT, if any */
85 int symbolnum; /* Position in output nlist */
87 #define LS_L_SYMBOL 1 /* Local symbol starts with an `L' */
88 #define LS_WRITE 2 /* Symbol goes in output symtable */
89 #define LS_RENAME 4 /* xlat name to `<file>.<name>' */
90 #define LS_HASGOTSLOT 8 /* This symbol has a GOT entry */
91 #define LS_WARNING 16 /* Second part of a N_WARNING duo */
95 * Global symbol data is recorded in these structures, one for each global
96 * symbol. They are found via hashing in 'symtab', which points to a vector
97 * of buckets. Each bucket is a chain of these structures through the link
100 * Rewritten version to support extra info for dynamic linking.
104 struct glosym *link; /* Next symbol hash bucket. */
105 char *name; /* Name of this symbol. */
106 long value; /* Value of this symbol */
107 localsymbol_t *refs; /* Chain of local symbols from object
108 files pertaining to this global
110 localsymbol_t *sorefs;/* Same for local symbols from shared
113 char *warning; /* message, from N_WARNING nlists */
114 int common_size; /* Common size */
115 int symbolnum; /* Symbol index in output symbol table */
116 int rrs_symbolnum; /* Symbol index in RRS symbol table */
118 localsymbol_t *def_lsp; /* The local symbol that gave this
119 global symbol its definition */
121 char defined; /* Definition of this symbol */
122 char so_defined; /* Definition of this symbol in a shared
123 object. These go into the RRS symbol table */
124 u_char undef_refs; /* Count of number of "undefined"
125 messages printed for this symbol */
126 u_char mult_defs; /* Same for "multiply defined" symbols */
127 struct glosym *alias; /* For symbols of type N_INDR, this
128 points at the real symbol. */
129 int setv_count; /* Number of elements in N_SETV symbols */
130 int size; /* Size of this symbol (either from N_SIZE
131 symbols or a from shared object's RRS */
132 int aux; /* Auxiliary type information conveyed in
133 the `n_other' field of nlists */
135 /* The offset into one of the RRS tables, -1 if not used */
141 #define GS_DEFINED 0x1 /* Symbol has definition (notyetused)*/
142 #define GS_REFERENCED 0x2 /* Symbol is referred to by something
144 #define GS_TRACE 0x4 /* Symbol will be traced */
145 #define GS_HASJMPSLOT 0x8 /* */
146 #define GS_HASGOTSLOT 0x10 /* Some state bits concerning */
147 #define GS_CPYRELOCRESERVED 0x20 /* entries in GOT and PLT tables */
148 #define GS_CPYRELOCCLAIMED 0x40 /* */
149 #define GS_WEAK 0x80 /* Symbol is weakly defined */
152 #ifndef __symbol_defined__
153 #define __symbol_defined__
154 typedef struct glosym symbol;
157 /* The symbol hash table: a vector of SYMTABSIZE pointers to struct glosym. */
158 extern symbol *symtab[];
159 #define FOR_EACH_SYMBOL(i,sp) { \
161 for (i = 0; i < SYMTABSIZE; i++) { \
162 register symbol *sp; \
163 for (sp = symtab[i]; sp; sp = sp->link)
165 #define END_EACH_SYMBOL }}
167 extern symbol *got_symbol; /* the symbol __GLOBAL_OFFSET_TABLE_ */
168 extern symbol *dynamic_symbol; /* the symbol __DYNAMIC */
171 * Each input file, and each library member ("subfile") being loaded, has a
172 * `file_entry' structure for it.
174 * For files specified by command args, these are contained in the vector which
175 * `file_table' points to.
177 * For library members, they are dynamically allocated, and chained through the
178 * `chain' field. The chain is found in the `subfiles' field of the
179 * `file_entry'. The `file_entry' objects for the members have `superfile'
180 * fields pointing to the one for the library.
182 * Rewritten version to support extra info for dynamic linking.
186 char *filename; /* Name of this file. */
188 * Name to use for the symbol giving address of text start Usually
189 * the same as filename, but for a file spec'd with -l this is the -l
190 * switch itself rather than the filename.
192 char *local_sym_name;
193 struct exec header; /* The file's a.out header. */
194 localsymbol_t *symbols; /* Symbol table of the file. */
195 int nsymbols; /* Number of symbols in above array. */
196 int string_size; /* Size in bytes of string table. */
197 char *strings; /* Pointer to the string table when
198 in core, NULL otherwise */
199 int strings_offset; /* Offset of string table,
200 (normally N_STROFF() + 4) */
202 * Next two used only if `relocatable_output' or if needed for
203 * output of undefined reference line numbers.
205 struct relocation_info *textrel; /* Text relocations */
206 int ntextrel; /* # of text relocations */
207 struct relocation_info *datarel; /* Data relocations */
208 int ndatarel; /* # of data relocations */
211 * Relation of this file's segments to the output file.
213 int text_start_address; /* Start of this file's text segment
214 in the output file core image. */
215 int data_start_address; /* Start of this file's data segment
216 in the output file core image. */
217 int bss_start_address; /* Start of this file's bss segment
218 in the output file core image. */
219 struct file_entry *subfiles; /* For a library, points to chain of
220 entries for the library members. */
221 struct file_entry *superfile; /* For library member, points to the
222 library's own entry. */
223 struct file_entry *chain; /* For library member, points to next
224 entry for next member. */
225 int starting_offset; /* For a library member, offset of the
226 member within the archive. Zero for
227 files that are not library members.*/
228 int total_size; /* Size of contents of this file,
229 if library member. */
231 struct file_entry *silly_archive;/* For shared libraries which have
234 int lib_major, lib_minor; /* Version numbers of a shared object */
237 #define E_IS_LIBRARY 1 /* File is a an archive */
238 #define E_HEADER_VALID 2 /* File's header has been read */
239 #define E_SEARCH_DIRS 4 /* Search directories for file */
240 #define E_SEARCH_DYNAMIC 8 /* Search for shared libs allowed */
241 #define E_JUST_SYMS 0x10 /* File is used for incremental load */
242 #define E_DYNAMIC 0x20 /* File is a shared object */
243 #define E_SCRAPPED 0x40 /* Ignore this file */
244 #define E_SYMBOLS_USED 0x80 /* Symbols from this entry were used */
245 #define E_SECONDCLASS 0x100 /* Shared object is a subsidiary */
249 * Runtime Relocation Section (RRS).
250 * This describes the data structures that go into the output text and data
251 * segments to support the run-time linker. The RRS can be empty (plain old
252 * static linking), or can just exist of GOT and PLT entries (in case of
253 * statically linked PIC code).
255 extern int rrs_section_type; /* What's in the RRS section */
257 #define RRS_PARTIAL 1
259 extern int rrs_text_size; /* Size of RRS text additions */
260 extern int rrs_text_start; /* Location of above */
261 extern int rrs_data_size; /* Size of RRS data additions */
262 extern int rrs_data_start; /* Location of above */
263 extern char *rrs_search_paths; /* `-L' RT paths */
265 /* Version number to put in __DYNAMIC (set by -V) */
266 extern int soversion;
267 #ifndef DEFAULT_SOVERSION
268 #define DEFAULT_SOVERSION LD_VERSION_BSD
271 extern int pc_relocation; /* Current PC reloc value */
273 extern int number_of_shobjs; /* # of shared objects linked in */
275 /* Current link mode */
276 extern int link_mode;
277 #define DYNAMIC 1 /* Consider shared libraries */
278 #define SYMBOLIC 2 /* Force symbolic resolution */
279 #define FORCEARCHIVE 4 /* Force inclusion of all members
281 #define SHAREABLE 8 /* Build a shared object */
282 #define SILLYARCHIVE 16 /* Process .sa companions, if any */
283 #define FORCEDYNAMIC 32 /* Force dynamic output even if no
284 shared libraries included */
285 #define WARNRRSTEXT 64 /* Warn about rrs in text */
287 extern FILE *outstream; /* Output file. */
288 extern struct exec outheader; /* Output file header. */
289 extern int magic; /* Output file magic. */
291 extern int relocatable_output;
293 #define PIC_TYPE_NONE 0
294 #define PIC_TYPE_SMALL 1
295 #define PIC_TYPE_LARGE 2
297 void read_header (int, struct file_entry *);
298 void read_entry_symbols (int, struct file_entry *);
299 void read_entry_strings (int, struct file_entry *);
300 void read_entry_relocation (int, struct file_entry *);
301 void enter_file_symbols (struct file_entry *);
302 void read_file_symbols (struct file_entry *);
303 int set_element_prefixed_p (char *);
304 int text_offset (struct file_entry *);
305 int file_open (struct file_entry *);
306 void each_file (void (*)(), void *);
307 void each_full_file (void (*)(), void *);
308 unsigned long check_each_file (unsigned long (*)(), void *);
309 void mywrite (void *, int, int, FILE *);
310 void padfile (int, FILE *);
313 void perror_name (char *);
314 void perror_file (struct file_entry *);
315 void print_symbols (FILE *);
316 char *get_file_name (struct file_entry *);
317 void print_file_name (struct file_entry *, FILE *);
318 void prline_file_name (struct file_entry *, FILE *);
319 int do_warnings (FILE *);
325 void symtab_init (int);
326 symbol *getsym (char *), *getsym_soft (char *);
329 void search_library (int, struct file_entry *);
330 void read_shared_object (int, struct file_entry *);
331 int findlib (struct file_entry *);
337 void init_rrs (void);
338 int rrs_add_shobj (struct file_entry *);
339 void alloc_rrs_reloc (struct file_entry *, symbol *);
340 void alloc_rrs_segment_reloc (struct file_entry *, struct relocation_info *);
341 void alloc_rrs_jmpslot (struct file_entry *, symbol *);
342 void alloc_rrs_gotslot (struct file_entry *, struct relocation_info *, localsymbol_t *);
343 void alloc_rrs_cpy_reloc (struct file_entry *, symbol *);
345 int claim_rrs_reloc (struct file_entry *, struct relocation_info *, symbol *, long *);
346 long claim_rrs_jmpslot (struct file_entry *, struct relocation_info *, symbol *, long);
347 long claim_rrs_gotslot (struct file_entry *, struct relocation_info *, struct localsymbol *, long);
348 long claim_rrs_internal_gotslot (struct file_entry *, struct relocation_info *, struct localsymbol *, long);
349 void claim_rrs_cpy_reloc (struct file_entry *, struct relocation_info *, symbol *);
350 void claim_rrs_segment_reloc (struct file_entry *, struct relocation_info *);
351 void consider_rrs_section_lengths (void);
352 void relocate_rrs_addresses (void);
353 void write_rrs (void);
356 void md_init_header (struct exec *, int, int);
357 long md_get_addend (struct relocation_info *, unsigned char *);
358 void md_relocate (struct relocation_info *, long, unsigned char *, int);
359 void md_make_jmpslot (jmpslot_t *, long, long);
360 void md_fix_jmpslot (jmpslot_t *, long, u_long);
361 int md_make_reloc (struct relocation_info *, struct relocation_info *, int);
362 void md_make_jmpreloc (struct relocation_info *, struct relocation_info *, int);
363 void md_make_gotreloc (struct relocation_info *, struct relocation_info *, int);
364 void md_make_copyreloc (struct relocation_info *, struct relocation_info *);
365 void md_set_breakpoint (long, long *);
369 void swap_longs (long *, int);
370 void swap_symbols (struct nlist *, int);
371 void swap_zsymbols (struct nzlist *, int);
372 void swap_ranlib_hdr (struct ranlib *, int);
373 void swap__dynamic (struct link_dynamic *);
374 void swap_section_dispatch_table (struct section_dispatch_table *);
375 void swap_so_debug (struct so_debug *);
376 void swapin_sod (struct sod *, int);
377 void swapout_sod (struct sod *, int);
378 void swapout_fshash (struct fshash *, int);
381 #endif /* __DYNAMIC_H__ */