Initial import from FreeBSD RELENG_4:
[dragonfly.git] / libexec / rtld-aout / dynamic.h
1 /*
2  * Copyright (c) 1993 Paul Kranenburg
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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
18  *
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.
29  *
30  * $FreeBSD: src/libexec/rtld-aout/dynamic.h,v 1.5 1999/08/28 00:10:05 peter Exp $
31  */
32
33 #ifndef __DYNAMIC_H__
34 #define __DYNAMIC_H__
35
36 #define SUN_COMPAT
37
38 #include "md.h"
39 #include "link.h"
40
41 #ifndef RELOC_JMPTAB_P
42
43 #define RELOC_JMPTAB_P(r)               ((r)->r_jmptable)
44 #define RELOC_BASEREL_P(r)              ((r)->r_baserel)
45 #define RELOC_RELATIVE_P(r)             ((r)->r_relative)
46 #define RELOC_COPY_P(r)                 ((r)->r_copy)
47 #define RELOC_LAZY_P(r)                 ((r)->r_jmptable)
48
49 #define CHECK_GOT_RELOC(r)              ((r)->r_pcrel)
50 #define RELOC_PIC_TYPE(r)               ((r)->r_baserel? \
51                                                 PIC_TYPE_LARGE:PIC_TYPE_NONE)
52 #endif
53
54 #ifndef RELOC_INIT_SEGMENT_RELOC
55 #define RELOC_INIT_SEGMENT_RELOC(r)
56 #endif
57
58 #ifndef MAX_GOTOFF
59 #define MAX_GOTOFF(x)   (LONG_MAX)
60 #endif
61
62 #ifndef MIN_GOTOFF
63 #define MIN_GOTOFF(x)   (LONG_MIN)
64 #endif
65
66 /*
67  * Internal representation of relocation types
68  */
69 #define RELTYPE_EXTERN          1
70 #define RELTYPE_JMPSLOT         2
71 #define RELTYPE_BASEREL         4
72 #define RELTYPE_RELATIVE        8
73 #define RELTYPE_COPY            16
74
75 #define N_ISWEAK(p)             (N_BIND(p) & BIND_WEAK)
76
77 typedef struct localsymbol {
78         struct nzlist           nzlist;         /* n[z]list from file */
79         struct glosym           *symbol;        /* Corresponding global symbol,
80                                                    if any */
81         struct localsymbol      *next;          /* List of definitions */
82         struct file_entry       *entry;         /* Backpointer to file */
83         long                    gotslot_offset; /* Position in GOT, if any */
84         int                     symbolnum;      /* Position in output nlist */
85         int                     flags;
86 #define LS_L_SYMBOL             1       /* Local symbol starts with an `L' */
87 #define LS_WRITE                2       /* Symbol goes in output symtable */
88 #define LS_RENAME               4       /* xlat name to `<file>.<name>' */
89 #define LS_HASGOTSLOT           8       /* This symbol has a GOT entry */
90 #define LS_WARNING              16      /* Second part of a N_WARNING duo */
91 } localsymbol_t;
92
93 /*
94  * Global symbol data is recorded in these structures, one for each global
95  * symbol. They are found via hashing in 'symtab', which points to a vector
96  * of buckets. Each bucket is a chain of these structures through the link
97  * field.
98  *
99  * Rewritten version to support extra info for dynamic linking.
100  */
101
102 struct glosym {
103         struct glosym   *link;  /* Next symbol hash bucket. */
104         char            *name;  /* Name of this symbol.  */
105         long            value;  /* Value of this symbol */
106         localsymbol_t   *refs;  /* Chain of local symbols from object
107                                    files pertaining to this global
108                                    symbol */
109         localsymbol_t   *sorefs;/* Same for local symbols from shared
110                                    object files. */
111
112         char    *warning;       /* message, from N_WARNING nlists */
113         int     common_size;    /* Common size */
114         int     symbolnum;      /* Symbol index in output symbol table */
115         int     rrs_symbolnum;  /* Symbol index in RRS symbol table */
116
117         localsymbol_t   *def_lsp;       /* The local symbol that gave this
118                                            global symbol its definition */
119
120         char    defined;        /* Definition of this symbol */
121         char    so_defined;     /* Definition of this symbol in a shared
122                                    object. These go into the RRS symbol table */
123         u_char  undef_refs;     /* Count of number of "undefined"
124                                    messages printed for this symbol */
125         u_char  mult_defs;      /* Same for "multiply defined" symbols */
126         struct glosym   *alias; /* For symbols of type N_INDR, this
127                                    points at the real symbol. */
128         int     setv_count;     /* Number of elements in N_SETV symbols */
129         int     size;           /* Size of this symbol (either from N_SIZE
130                                    symbols or a from shared object's RRS */
131         int     aux;            /* Auxiliary type information conveyed in
132                                    the `n_other' field of nlists */
133
134         /* The offset into one of the RRS tables, -1 if not used */
135         long    jmpslot_offset;
136         long    gotslot_offset;
137
138         long                    flags;
139
140 #define GS_DEFINED              0x1     /* Symbol has definition (notyetused)*/
141 #define GS_REFERENCED           0x2     /* Symbol is referred to by something
142                                            interesting */
143 #define GS_TRACE                0x4     /* Symbol will be traced */
144 #define GS_HASJMPSLOT           0x8     /*                               */
145 #define GS_HASGOTSLOT           0x10    /* Some state bits concerning    */
146 #define GS_CPYRELOCRESERVED     0x20    /* entries in GOT and PLT tables */
147 #define GS_CPYRELOCCLAIMED      0x40    /*                               */
148 #define GS_WEAK                 0x80    /* Symbol is weakly defined */
149
150 };
151 #ifndef __symbol_defined__
152 #define __symbol_defined__
153 typedef struct glosym symbol;
154 #endif
155
156 /* The symbol hash table: a vector of SYMTABSIZE pointers to struct glosym. */
157 extern symbol *symtab[];
158 #define FOR_EACH_SYMBOL(i,sp) {                                 \
159         int i;                                                  \
160         for (i = 0; i < SYMTABSIZE; i++) {                              \
161                 register symbol *sp;                            \
162                 for (sp = symtab[i]; sp; sp = sp->link)
163
164 #define END_EACH_SYMBOL }}
165
166 extern symbol   *got_symbol;            /* the symbol __GLOBAL_OFFSET_TABLE_ */
167 extern symbol   *dynamic_symbol;        /* the symbol __DYNAMIC */
168
169 /*
170  * Each input file, and each library member ("subfile") being loaded, has a
171  * `file_entry' structure for it.
172  *
173  * For files specified by command args, these are contained in the vector which
174  * `file_table' points to.
175  *
176  * For library members, they are dynamically allocated, and chained through the
177  * `chain' field. The chain is found in the `subfiles' field of the
178  * `file_entry'. The `file_entry' objects for the members have `superfile'
179  * fields pointing to the one for the library.
180  *
181  * Rewritten version to support extra info for dynamic linking.
182  */
183
184 struct file_entry {
185         char    *filename;      /* Name of this file.  */
186         /*
187          * Name to use for the symbol giving address of text start Usually
188          * the same as filename, but for a file spec'd with -l this is the -l
189          * switch itself rather than the filename.
190          */
191         char            *local_sym_name;
192         struct exec     header; /* The file's a.out header.  */
193         localsymbol_t   *symbols;       /* Symbol table of the file. */
194         int             nsymbols;       /* Number of symbols in above array. */
195         int             string_size;    /* Size in bytes of string table. */
196         char            *strings;       /* Pointer to the string table when
197                                            in core, NULL otherwise */
198         int             strings_offset; /* Offset of string table,
199                                            (normally N_STROFF() + 4) */
200         /*
201          * Next two used only if `relocatable_output' or if needed for
202          * output of undefined reference line numbers.
203          */
204         struct relocation_info  *textrel;       /* Text relocations */
205         int                     ntextrel;       /* # of text relocations */
206         struct relocation_info  *datarel;       /* Data relocations */
207         int                     ndatarel;       /* # of data relocations */
208
209         /*
210          * Relation of this file's segments to the output file.
211          */
212         int     text_start_address;     /* Start of this file's text segment
213                                            in the output file core image. */
214         int     data_start_address;     /* Start of this file's data segment
215                                            in the output file core image. */
216         int     bss_start_address;      /* Start of this file's bss segment
217                                            in the output file core image. */
218         struct file_entry *subfiles;    /* For a library, points to chain of
219                                            entries for the library members. */
220         struct file_entry *superfile;   /* For library member, points to the
221                                            library's own entry.  */
222         struct file_entry *chain;       /* For library member, points to next
223                                            entry for next member.  */
224         int     starting_offset;        /* For a library member, offset of the
225                                            member within the archive. Zero for
226                                            files that are not library members.*/
227         int     total_size;             /* Size of contents of this file,
228                                            if library member. */
229 #ifdef SUN_COMPAT
230         struct file_entry *silly_archive;/* For shared libraries which have
231                                             a .sa companion */
232 #endif
233         int     lib_major, lib_minor;   /* Version numbers of a shared object */
234
235         int     flags;
236 #define E_IS_LIBRARY            1       /* File is a an archive */
237 #define E_HEADER_VALID          2       /* File's header has been read */
238 #define E_SEARCH_DIRS           4       /* Search directories for file */
239 #define E_SEARCH_DYNAMIC        8       /* Search for shared libs allowed */
240 #define E_JUST_SYMS             0x10    /* File is used for incremental load */
241 #define E_DYNAMIC               0x20    /* File is a shared object */
242 #define E_SCRAPPED              0x40    /* Ignore this file */
243 #define E_SYMBOLS_USED          0x80    /* Symbols from this entry were used */
244 #define E_SECONDCLASS           0x100   /* Shared object is a subsidiary */
245 };
246
247 /*
248  * Runtime Relocation Section (RRS).
249  * This describes the data structures that go into the output text and data
250  * segments to support the run-time linker. The RRS can be empty (plain old
251  * static linking), or can just exist of GOT and PLT entries (in case of
252  * statically linked PIC code).
253  */
254 extern int              rrs_section_type;       /* What's in the RRS section */
255 #define RRS_NONE        0
256 #define RRS_PARTIAL     1
257 #define RRS_FULL        2
258 extern int              rrs_text_size;          /* Size of RRS text additions */
259 extern int              rrs_text_start;         /* Location of above */
260 extern int              rrs_data_size;          /* Size of RRS data additions */
261 extern int              rrs_data_start;         /* Location of above */
262 extern char             *rrs_search_paths;      /* `-L' RT paths */
263
264 /* Version number to put in __DYNAMIC (set by -V) */
265 extern int      soversion;
266 #ifndef DEFAULT_SOVERSION
267 #define DEFAULT_SOVERSION       LD_VERSION_BSD
268 #endif
269
270 extern int              pc_relocation;          /* Current PC reloc value */
271
272 extern int              number_of_shobjs;       /* # of shared objects linked in */
273
274 /* Current link mode */
275 extern int              link_mode;
276 #define DYNAMIC         1               /* Consider shared libraries */
277 #define SYMBOLIC        2               /* Force symbolic resolution */
278 #define FORCEARCHIVE    4               /* Force inclusion of all members
279                                            of archives */
280 #define SHAREABLE       8               /* Build a shared object */
281 #define SILLYARCHIVE    16              /* Process .sa companions, if any */
282 #define FORCEDYNAMIC    32              /* Force dynamic output even if no
283                                            shared libraries included */
284 #define WARNRRSTEXT     64              /* Warn about rrs in text */
285
286 extern FILE             *outstream;     /* Output file. */
287 extern struct exec      outheader;      /* Output file header. */
288 extern int              magic;          /* Output file magic. */
289 extern int              oldmagic;
290 extern int              relocatable_output;
291 extern int              pic_type;
292 #define PIC_TYPE_NONE   0
293 #define PIC_TYPE_SMALL  1
294 #define PIC_TYPE_LARGE  2
295
296 void    read_header __P((int, struct file_entry *));
297 void    read_entry_symbols __P((int, struct file_entry *));
298 void    read_entry_strings __P((int, struct file_entry *));
299 void    read_entry_relocation __P((int, struct file_entry *));
300 void    enter_file_symbols __P((struct file_entry *));
301 void    read_file_symbols __P((struct file_entry *));
302 int     set_element_prefixed_p __P((char *));
303 int     text_offset __P((struct file_entry *));
304 int     file_open __P((struct file_entry *));
305 void    each_file __P((void (*)(), void *));
306 void    each_full_file __P((void (*)(), void *));
307 unsigned long   check_each_file __P((unsigned long (*)(), void *));
308 void    mywrite __P((void *, int, int, FILE *));
309 void    padfile __P((int, FILE *));
310
311 /* In warnings.c: */
312 void    perror_name __P((char *));
313 void    perror_file __P((struct file_entry *));
314 void    print_symbols __P((FILE *));
315 char    *get_file_name __P((struct file_entry *));
316 void    print_file_name __P((struct file_entry *, FILE *));
317 void    prline_file_name __P((struct file_entry *, FILE *));
318 int     do_warnings __P((FILE *));
319
320 /* In etc.c: */
321 #include "support.h"
322
323 /* In symbol.c: */
324 void    symtab_init __P((int));
325 symbol  *getsym __P((char *)), *getsym_soft __P((char *));
326
327 /* In lib.c: */
328 void    search_library __P((int, struct file_entry *));
329 void    read_shared_object __P((int, struct file_entry *));
330 int     findlib __P((struct file_entry *));
331
332 /* In shlib.c: */
333 #include "shlib.h"
334
335 /* In rrs.c: */
336 void    init_rrs __P((void));
337 int     rrs_add_shobj __P((struct file_entry *));
338 void    alloc_rrs_reloc __P((struct file_entry *, symbol *));
339 void    alloc_rrs_segment_reloc __P((struct file_entry *, struct relocation_info  *));
340 void    alloc_rrs_jmpslot __P((struct file_entry *, symbol *));
341 void    alloc_rrs_gotslot __P((struct file_entry *, struct relocation_info  *, localsymbol_t *));
342 void    alloc_rrs_cpy_reloc __P((struct file_entry *, symbol *));
343
344 int     claim_rrs_reloc __P((struct file_entry *, struct relocation_info *, symbol *, long *));
345 long    claim_rrs_jmpslot __P((struct file_entry *, struct relocation_info *, symbol *, long));
346 long    claim_rrs_gotslot __P((struct file_entry *, struct relocation_info *, struct localsymbol *, long));
347 long    claim_rrs_internal_gotslot __P((struct file_entry *, struct relocation_info *, struct localsymbol *, long));
348 void    claim_rrs_cpy_reloc __P((struct file_entry *, struct relocation_info *, symbol *));
349 void    claim_rrs_segment_reloc __P((struct file_entry *, struct relocation_info *));
350 void    consider_rrs_section_lengths __P((void));
351 void    relocate_rrs_addresses __P((void));
352 void    write_rrs __P((void));
353
354 /* In <md>.c */
355 void    md_init_header __P((struct exec *, int, int));
356 long    md_get_addend __P((struct relocation_info *, unsigned char *));
357 void    md_relocate __P((struct relocation_info *, long, unsigned char *, int));
358 void    md_make_jmpslot __P((jmpslot_t *, long, long));
359 void    md_fix_jmpslot __P((jmpslot_t *, long, u_long));
360 int     md_make_reloc __P((struct relocation_info *, struct relocation_info *, int));
361 void    md_make_jmpreloc __P((struct relocation_info *, struct relocation_info *, int));
362 void    md_make_gotreloc __P((struct relocation_info *, struct relocation_info *, int));
363 void    md_make_copyreloc __P((struct relocation_info *, struct relocation_info *));
364 void    md_set_breakpoint __P((long, long *));
365
366 #ifdef NEED_SWAP
367 /* In xbits.c: */
368 void    swap_longs __P((long *, int));
369 void    swap_symbols __P((struct nlist *, int));
370 void    swap_zsymbols __P((struct nzlist *, int));
371 void    swap_ranlib_hdr __P((struct ranlib *, int));
372 void    swap__dynamic __P((struct link_dynamic *));
373 void    swap_section_dispatch_table __P((struct section_dispatch_table *));
374 void    swap_so_debug __P((struct so_debug *));
375 void    swapin_sod __P((struct sod *, int));
376 void    swapout_sod __P((struct sod *, int));
377 void    swapout_fshash __P((struct fshash *, int));
378 #endif
379
380 #endif /* __DYNAMIC_H__ */