70c1edd2d52804127f5538fa5b87b7332bc21ad3
[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  * $DragonFly: src/libexec/rtld-aout/Attic/dynamic.h,v 1.2 2003/06/17 04:27:08 dillon Exp $
32  */
33
34 #ifndef __DYNAMIC_H__
35 #define __DYNAMIC_H__
36
37 #define SUN_COMPAT
38
39 #include "md.h"
40 #include "link.h"
41
42 #ifndef RELOC_JMPTAB_P
43
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)
49
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)
53 #endif
54
55 #ifndef RELOC_INIT_SEGMENT_RELOC
56 #define RELOC_INIT_SEGMENT_RELOC(r)
57 #endif
58
59 #ifndef MAX_GOTOFF
60 #define MAX_GOTOFF(x)   (LONG_MAX)
61 #endif
62
63 #ifndef MIN_GOTOFF
64 #define MIN_GOTOFF(x)   (LONG_MIN)
65 #endif
66
67 /*
68  * Internal representation of relocation types
69  */
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
75
76 #define N_ISWEAK(p)             (N_BIND(p) & BIND_WEAK)
77
78 typedef struct localsymbol {
79         struct nzlist           nzlist;         /* n[z]list from file */
80         struct glosym           *symbol;        /* Corresponding global symbol,
81                                                    if any */
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 */
86         int                     flags;
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 */
92 } localsymbol_t;
93
94 /*
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
98  * field.
99  *
100  * Rewritten version to support extra info for dynamic linking.
101  */
102
103 struct glosym {
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
109                                    symbol */
110         localsymbol_t   *sorefs;/* Same for local symbols from shared
111                                    object files. */
112
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 */
117
118         localsymbol_t   *def_lsp;       /* The local symbol that gave this
119                                            global symbol its definition */
120
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 */
134
135         /* The offset into one of the RRS tables, -1 if not used */
136         long    jmpslot_offset;
137         long    gotslot_offset;
138
139         long                    flags;
140
141 #define GS_DEFINED              0x1     /* Symbol has definition (notyetused)*/
142 #define GS_REFERENCED           0x2     /* Symbol is referred to by something
143                                            interesting */
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 */
150
151 };
152 #ifndef __symbol_defined__
153 #define __symbol_defined__
154 typedef struct glosym symbol;
155 #endif
156
157 /* The symbol hash table: a vector of SYMTABSIZE pointers to struct glosym. */
158 extern symbol *symtab[];
159 #define FOR_EACH_SYMBOL(i,sp) {                                 \
160         int i;                                                  \
161         for (i = 0; i < SYMTABSIZE; i++) {                              \
162                 register symbol *sp;                            \
163                 for (sp = symtab[i]; sp; sp = sp->link)
164
165 #define END_EACH_SYMBOL }}
166
167 extern symbol   *got_symbol;            /* the symbol __GLOBAL_OFFSET_TABLE_ */
168 extern symbol   *dynamic_symbol;        /* the symbol __DYNAMIC */
169
170 /*
171  * Each input file, and each library member ("subfile") being loaded, has a
172  * `file_entry' structure for it.
173  *
174  * For files specified by command args, these are contained in the vector which
175  * `file_table' points to.
176  *
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.
181  *
182  * Rewritten version to support extra info for dynamic linking.
183  */
184
185 struct file_entry {
186         char    *filename;      /* Name of this file.  */
187         /*
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.
191          */
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) */
201         /*
202          * Next two used only if `relocatable_output' or if needed for
203          * output of undefined reference line numbers.
204          */
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 */
209
210         /*
211          * Relation of this file's segments to the output file.
212          */
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. */
230 #ifdef SUN_COMPAT
231         struct file_entry *silly_archive;/* For shared libraries which have
232                                             a .sa companion */
233 #endif
234         int     lib_major, lib_minor;   /* Version numbers of a shared object */
235
236         int     flags;
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 */
246 };
247
248 /*
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).
254  */
255 extern int              rrs_section_type;       /* What's in the RRS section */
256 #define RRS_NONE        0
257 #define RRS_PARTIAL     1
258 #define RRS_FULL        2
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 */
264
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
269 #endif
270
271 extern int              pc_relocation;          /* Current PC reloc value */
272
273 extern int              number_of_shobjs;       /* # of shared objects linked in */
274
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
280                                            of archives */
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 */
286
287 extern FILE             *outstream;     /* Output file. */
288 extern struct exec      outheader;      /* Output file header. */
289 extern int              magic;          /* Output file magic. */
290 extern int              oldmagic;
291 extern int              relocatable_output;
292 extern int              pic_type;
293 #define PIC_TYPE_NONE   0
294 #define PIC_TYPE_SMALL  1
295 #define PIC_TYPE_LARGE  2
296
297 void    read_header __P((int, struct file_entry *));
298 void    read_entry_symbols __P((int, struct file_entry *));
299 void    read_entry_strings __P((int, struct file_entry *));
300 void    read_entry_relocation __P((int, struct file_entry *));
301 void    enter_file_symbols __P((struct file_entry *));
302 void    read_file_symbols __P((struct file_entry *));
303 int     set_element_prefixed_p __P((char *));
304 int     text_offset __P((struct file_entry *));
305 int     file_open __P((struct file_entry *));
306 void    each_file __P((void (*)(), void *));
307 void    each_full_file __P((void (*)(), void *));
308 unsigned long   check_each_file __P((unsigned long (*)(), void *));
309 void    mywrite __P((void *, int, int, FILE *));
310 void    padfile __P((int, FILE *));
311
312 /* In warnings.c: */
313 void    perror_name __P((char *));
314 void    perror_file __P((struct file_entry *));
315 void    print_symbols __P((FILE *));
316 char    *get_file_name __P((struct file_entry *));
317 void    print_file_name __P((struct file_entry *, FILE *));
318 void    prline_file_name __P((struct file_entry *, FILE *));
319 int     do_warnings __P((FILE *));
320
321 /* In etc.c: */
322 #include "support.h"
323
324 /* In symbol.c: */
325 void    symtab_init __P((int));
326 symbol  *getsym __P((char *)), *getsym_soft __P((char *));
327
328 /* In lib.c: */
329 void    search_library __P((int, struct file_entry *));
330 void    read_shared_object __P((int, struct file_entry *));
331 int     findlib __P((struct file_entry *));
332
333 /* In shlib.c: */
334 #include "shlib.h"
335
336 /* In rrs.c: */
337 void    init_rrs __P((void));
338 int     rrs_add_shobj __P((struct file_entry *));
339 void    alloc_rrs_reloc __P((struct file_entry *, symbol *));
340 void    alloc_rrs_segment_reloc __P((struct file_entry *, struct relocation_info  *));
341 void    alloc_rrs_jmpslot __P((struct file_entry *, symbol *));
342 void    alloc_rrs_gotslot __P((struct file_entry *, struct relocation_info  *, localsymbol_t *));
343 void    alloc_rrs_cpy_reloc __P((struct file_entry *, symbol *));
344
345 int     claim_rrs_reloc __P((struct file_entry *, struct relocation_info *, symbol *, long *));
346 long    claim_rrs_jmpslot __P((struct file_entry *, struct relocation_info *, symbol *, long));
347 long    claim_rrs_gotslot __P((struct file_entry *, struct relocation_info *, struct localsymbol *, long));
348 long    claim_rrs_internal_gotslot __P((struct file_entry *, struct relocation_info *, struct localsymbol *, long));
349 void    claim_rrs_cpy_reloc __P((struct file_entry *, struct relocation_info *, symbol *));
350 void    claim_rrs_segment_reloc __P((struct file_entry *, struct relocation_info *));
351 void    consider_rrs_section_lengths __P((void));
352 void    relocate_rrs_addresses __P((void));
353 void    write_rrs __P((void));
354
355 /* In <md>.c */
356 void    md_init_header __P((struct exec *, int, int));
357 long    md_get_addend __P((struct relocation_info *, unsigned char *));
358 void    md_relocate __P((struct relocation_info *, long, unsigned char *, int));
359 void    md_make_jmpslot __P((jmpslot_t *, long, long));
360 void    md_fix_jmpslot __P((jmpslot_t *, long, u_long));
361 int     md_make_reloc __P((struct relocation_info *, struct relocation_info *, int));
362 void    md_make_jmpreloc __P((struct relocation_info *, struct relocation_info *, int));
363 void    md_make_gotreloc __P((struct relocation_info *, struct relocation_info *, int));
364 void    md_make_copyreloc __P((struct relocation_info *, struct relocation_info *));
365 void    md_set_breakpoint __P((long, long *));
366
367 #ifdef NEED_SWAP
368 /* In xbits.c: */
369 void    swap_longs __P((long *, int));
370 void    swap_symbols __P((struct nlist *, int));
371 void    swap_zsymbols __P((struct nzlist *, int));
372 void    swap_ranlib_hdr __P((struct ranlib *, int));
373 void    swap__dynamic __P((struct link_dynamic *));
374 void    swap_section_dispatch_table __P((struct section_dispatch_table *));
375 void    swap_so_debug __P((struct so_debug *));
376 void    swapin_sod __P((struct sod *, int));
377 void    swapout_sod __P((struct sod *, int));
378 void    swapout_fshash __P((struct fshash *, int));
379 #endif
380
381 #endif /* __DYNAMIC_H__ */