Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame^] | 1 | /* Copyright (C) 2001, 2002, 2003, 2005 Red Hat, Inc. |
| 2 | Written by Ulrich Drepper <drepper@redhat.com>, 2001. |
| 3 | |
| 4 | This program is Open Source software; you can redistribute it and/or |
| 5 | modify it under the terms of the Open Software License version 1.0 as |
| 6 | published by the Open Source Initiative. |
| 7 | |
| 8 | You should have received a copy of the Open Software License along |
| 9 | with this program; if not, you may obtain a copy of the Open Software |
| 10 | License version 1.0 from http://www.opensource.org/licenses/osl.php or |
| 11 | by writing the Open Source Initiative c/o Lawrence Rosen, Esq., |
| 12 | 3001 King Ranch Road, Ukiah, CA 95482. */ |
| 13 | |
| 14 | #ifndef LD_H |
| 15 | #define LD_H 1 |
| 16 | |
| 17 | #include <dlfcn.h> |
| 18 | #include <obstack.h> |
| 19 | #include <stdbool.h> |
| 20 | #include <stdio.h> |
| 21 | #include "xelf.h" |
| 22 | |
| 23 | |
| 24 | /* Recommended size of the buffer passed to ld_strerror. */ |
| 25 | #define ERRBUFSIZE (512) |
| 26 | |
| 27 | /* Character used to introduce version name after symbol. */ |
| 28 | #define VER_CHR '@' |
| 29 | |
| 30 | |
| 31 | /* Methods for handling archives. */ |
| 32 | enum extract_rule |
| 33 | { |
| 34 | defaultextract, /* Weak references don't cause archive member to |
| 35 | be used. */ |
| 36 | weakextract, /* Weak references cause archive member to be |
| 37 | extracted. */ |
| 38 | allextract /* Extract all archive members regardless of |
| 39 | references (aka whole-archive). */ |
| 40 | }; |
| 41 | |
| 42 | |
| 43 | /* Type of output file. */ |
| 44 | enum file_type |
| 45 | { |
| 46 | no_file_type = 0, /* None selected so far. */ |
| 47 | executable_file_type, /* Executable. */ |
| 48 | dso_file_type, /* DSO. */ |
| 49 | dso_needed_file_type, /* DSO introduced by DT_NEEDED. */ |
| 50 | relocatable_file_type, /* Relocatable object file. */ |
| 51 | archive_file_type /* Archive (input only). */ |
| 52 | }; |
| 53 | |
| 54 | |
| 55 | struct usedfiles |
| 56 | { |
| 57 | /* The next file given at the command line. */ |
| 58 | struct usedfiles *next; |
| 59 | /* Nonzero if this file is the beginning of a group. */ |
| 60 | bool group_start; |
| 61 | /* Nonzero if this file is the end of a group. */ |
| 62 | bool group_end; |
| 63 | /* Pointer to the beginning of the group. It is necessary to |
| 64 | explain why we cannot simply use the 'next' pointer and have a |
| 65 | circular single-linked list like in many cases. The problem is |
| 66 | that the last archive of the group, if it is the last file of the |
| 67 | group, contains the only existing pointer to the next file we |
| 68 | have to look at. All files are initially connected via the |
| 69 | 'next' pointer in a single-linked list. Therefore we cannot |
| 70 | overwrite this value. It instead will be used once the group is |
| 71 | handled and we go on processing the rest of the files. */ |
| 72 | struct usedfiles *group_backref; |
| 73 | |
| 74 | /* Name/path of the file. */ |
| 75 | const char *fname; |
| 76 | /* Resolved file name. */ |
| 77 | const char *rfname; |
| 78 | /* Name used as reference in DT_NEEDED entries. This is normally |
| 79 | the SONAME. If it is missing it's normally the fname above. */ |
| 80 | const char *soname; |
| 81 | /* Handle for the SONAME in the string table. */ |
| 82 | struct Ebl_Strent *sonameent; |
| 83 | |
| 84 | /* Help to identify duplicates. */ |
| 85 | dev_t dev; |
| 86 | ino_t ino; |
| 87 | |
| 88 | enum |
| 89 | { |
| 90 | not_opened, |
| 91 | opened, |
| 92 | in_archive, |
| 93 | closed |
| 94 | } status; |
| 95 | |
| 96 | /* How to extract elements from archives. */ |
| 97 | enum extract_rule extract_rule; |
| 98 | |
| 99 | /* Lazy-loading rule. */ |
| 100 | bool lazyload; |
| 101 | |
| 102 | /* If this is a DSO the flag indicates whether the file is directly |
| 103 | used in a reference. */ |
| 104 | bool used; |
| 105 | |
| 106 | /* If nonzero this is the archive sequence number which can be used to |
| 107 | determine whether back refernces from -( -) or GROUP statements |
| 108 | have to be followed. */ |
| 109 | int archive_seq; |
| 110 | |
| 111 | /* Pointer to the record for the archive containing this file. */ |
| 112 | struct usedfiles *archive_file; |
| 113 | |
| 114 | /* Type of file. We have to distinguish these types since they |
| 115 | are searched for differently. */ |
| 116 | enum file_type file_type; |
| 117 | /* This is the ELF library handle for this file. */ |
| 118 | Elf *elf; |
| 119 | |
| 120 | /* The ELF header. */ |
| 121 | #if NATIVE_ELF != 0 |
| 122 | XElf_Ehdr *ehdr; |
| 123 | # define FILEINFO_EHDR(fi) (*(fi)) |
| 124 | #else |
| 125 | XElf_Ehdr ehdr; |
| 126 | # define FILEINFO_EHDR(fi) (fi) |
| 127 | #endif |
| 128 | |
| 129 | /* Index of the section header string table section. We use a |
| 130 | separate field and not the e_shstrndx field in the ELF header |
| 131 | since in case of a file with more than 64000 sections the index |
| 132 | might be stored in the section header of section zero. The |
| 133 | elf_getshstrndx() function can find the value but it is too |
| 134 | costly to repeat this call over and over. */ |
| 135 | size_t shstrndx; |
| 136 | |
| 137 | /* Info about the sections of the file. */ |
| 138 | struct scninfo |
| 139 | { |
| 140 | /* Handle for the section. Note that we can store a section |
| 141 | handle here because the file is not changing. This together |
| 142 | with the knowledge about the libelf library is enough for us to |
| 143 | assume the section reference remains valid at all times. */ |
| 144 | Elf_Scn *scn; |
| 145 | /* Section header. */ |
| 146 | #if NATIVE_ELF != 0 |
| 147 | XElf_Shdr *shdr; |
| 148 | # define SCNINFO_SHDR(si) (*(si)) |
| 149 | #else |
| 150 | XElf_Shdr shdr; |
| 151 | # define SCNINFO_SHDR(si) (si) |
| 152 | #endif |
| 153 | /* Offset of this files section in the combined section. */ |
| 154 | XElf_Off offset; |
| 155 | /* Index of the section in the output file. */ |
| 156 | Elf32_Word outscnndx; |
| 157 | /* Index of the output section in the 'allsection' array. */ |
| 158 | Elf32_Word allsectionsidx; |
| 159 | /* True if the section is used. */ |
| 160 | bool used; |
| 161 | /* Section group number. This is the index of the SHT_GROUP section. */ |
| 162 | Elf32_Word grpid; |
| 163 | /* Pointer back to the containing file information structure. */ |
| 164 | struct usedfiles *fileinfo; |
| 165 | /* List of symbols in this section (set only for merge-able sections). */ |
| 166 | struct symbol *symbols; |
| 167 | /* Size of relocations in this section. Only used for relocation |
| 168 | sections. */ |
| 169 | size_t relsize; |
| 170 | /* Pointer to next section which is put in the given output |
| 171 | section. */ |
| 172 | struct scninfo *next; |
| 173 | } *scninfo; |
| 174 | |
| 175 | /* List of section group sections. */ |
| 176 | struct scninfo *groups; |
| 177 | |
| 178 | /* The symbol table section. |
| 179 | |
| 180 | XXX Maybe support for more than one symbol table is needed. */ |
| 181 | Elf_Data *symtabdata; |
| 182 | /* Extra section index table section. */ |
| 183 | Elf_Data *xndxdata; |
| 184 | /* Dynamic symbol table section. */ |
| 185 | Elf_Data *dynsymtabdata; |
| 186 | /* The version number section. */ |
| 187 | Elf_Data *versymdata; |
| 188 | /* The defined versions. */ |
| 189 | Elf_Data *verdefdata; |
| 190 | /* Number of versions defined. */ |
| 191 | size_t nverdef; |
| 192 | /* True if the version with the given index number is used in the |
| 193 | output. */ |
| 194 | XElf_Versym *verdefused; |
| 195 | /* How many versions are used. */ |
| 196 | size_t nverdefused; |
| 197 | /* Handle for name of the version. */ |
| 198 | struct Ebl_Strent **verdefent; |
| 199 | /* The needed versions. */ |
| 200 | Elf_Data *verneeddata; |
| 201 | /* String table section associated with the symbol table. */ |
| 202 | Elf32_Word symstridx; |
| 203 | /* String table section associated with the dynamic symbol table. */ |
| 204 | Elf32_Word dynsymstridx; |
| 205 | /* Number of entries in the symbol table. */ |
| 206 | size_t nsymtab; |
| 207 | size_t nlocalsymbols; |
| 208 | size_t ndynsymtab; |
| 209 | /* Dynamic section. */ |
| 210 | Elf_Scn *dynscn; |
| 211 | |
| 212 | /* Indirection table for the symbols defined here. */ |
| 213 | Elf32_Word *symindirect; |
| 214 | Elf32_Word *dynsymindirect; |
| 215 | /* For undefined or common symbols we need a reference to the symbol |
| 216 | record. */ |
| 217 | struct symbol **symref; |
| 218 | struct symbol **dynsymref; |
| 219 | |
| 220 | /* This is the file descriptor. The value is -1 if the descriptor |
| 221 | was already closed. This can happen if we needed file descriptors |
| 222 | to open new files. */ |
| 223 | int fd; |
| 224 | /* This flag is true if the descriptor was passed to the generic |
| 225 | functions from somewhere else. This is an implementation detail; |
| 226 | no machine-specific code must use this flag. */ |
| 227 | bool fd_passed; |
| 228 | |
| 229 | /* True if any of the sections is merge-able. */ |
| 230 | bool has_merge_sections; |
| 231 | }; |
| 232 | |
| 233 | |
| 234 | /* Functions to test for the various types of files we handle. */ |
| 235 | static inline int |
| 236 | ld_file_rel_p (struct usedfiles *file) |
| 237 | { |
| 238 | return (elf_kind (file->elf) == ELF_K_ELF |
| 239 | && FILEINFO_EHDR (file->ehdr).e_type == ET_REL); |
| 240 | } |
| 241 | |
| 242 | static inline int |
| 243 | ld_file_dso_p (struct usedfiles *file) |
| 244 | { |
| 245 | return (elf_kind (file->elf) == ELF_K_ELF |
| 246 | && FILEINFO_EHDR (file->ehdr).e_type == ET_DYN); |
| 247 | } |
| 248 | |
| 249 | static inline int |
| 250 | ld_file_ar_p (struct usedfiles *file) |
| 251 | { |
| 252 | return elf_kind (file->elf) == ELF_K_AR; |
| 253 | } |
| 254 | |
| 255 | |
| 256 | struct pathelement |
| 257 | { |
| 258 | /* The next path to search. */ |
| 259 | struct pathelement *next; |
| 260 | /* The path name. */ |
| 261 | const char *pname; |
| 262 | /* Larger than zero if the directory exists, smaller than zero if not, |
| 263 | zero if it is not yet known. */ |
| 264 | int exist; |
| 265 | }; |
| 266 | |
| 267 | |
| 268 | /* Forward declaration. */ |
| 269 | struct ld_state; |
| 270 | |
| 271 | |
| 272 | /* Callback functions. */ |
| 273 | struct callbacks |
| 274 | { |
| 275 | /* Library names passed to the linker as -lXXX represent files named |
| 276 | libXXX.YY. The YY part can have different forms, depending on the |
| 277 | architecture. The generic set is .so and .a (in this order). */ |
| 278 | const char **(*lib_extensions) (struct ld_state *) |
| 279 | __attribute__ ((__const__)); |
| 280 | #define LIB_EXTENSION(state) \ |
| 281 | DL_CALL_FCT ((state)->callbacks.lib_extensions, (state)) |
| 282 | |
| 283 | /* Process the given file. If the file is not yet open, open it. |
| 284 | The first parameter is a file descriptor for the file which can |
| 285 | be -1 to indicate the file has not yet been found. The second |
| 286 | parameter describes the file to be opened, the last one is the |
| 287 | state of the linker which among other information contain the |
| 288 | paths we look at.*/ |
| 289 | int (*file_process) (int fd, struct usedfiles *, struct ld_state *, |
| 290 | struct usedfiles **); |
| 291 | #define FILE_PROCESS(fd, file, state, nextp) \ |
| 292 | DL_CALL_FCT ((state)->callbacks.file_process, (fd, file, state, nextp)) |
| 293 | |
| 294 | /* Close the given file. */ |
| 295 | int (*file_close) (struct usedfiles *, struct ld_state *); |
| 296 | #define FILE_CLOSE(file, state) \ |
| 297 | DL_CALL_FCT ((state)->callbacks.file_close, (file, state)) |
| 298 | |
| 299 | /* Create the output sections now. This requires knowledge about |
| 300 | all the sections we will need. It may be necessary to sort the |
| 301 | sections in the order they are supposed to appear in the |
| 302 | executable. The sorting use many different kinds of information |
| 303 | to optimize the resulting binary. Important is to respect |
| 304 | segment boundaries and the needed alignment. The mode of the |
| 305 | segments will be determined afterwards automatically by the |
| 306 | output routines. */ |
| 307 | void (*create_sections) (struct ld_state *); |
| 308 | #define CREATE_SECTIONS(state) \ |
| 309 | DL_CALL_FCT ((state)->callbacks.create_sections, (state)) |
| 310 | |
| 311 | /* Determine whether we have any non-weak unresolved references left. */ |
| 312 | int (*flag_unresolved) (struct ld_state *); |
| 313 | #define FLAG_UNRESOLVED(state) \ |
| 314 | DL_CALL_FCT ((state)->callbacks.flag_unresolved, (state)) |
| 315 | |
| 316 | /* Create the sections which are generated by the linker and are not |
| 317 | present in the input file. */ |
| 318 | void (*generate_sections) (struct ld_state *); |
| 319 | #define GENERATE_SECTIONS(state) \ |
| 320 | DL_CALL_FCT ((state)->callbacks.generate_sections, (state)) |
| 321 | |
| 322 | /* Open the output file. The file name is given or "a.out". We |
| 323 | create as much of the ELF structure as possible. */ |
| 324 | int (*open_outfile) (struct ld_state *, int, int, int); |
| 325 | #define OPEN_OUTFILE(state, machine, class, data) \ |
| 326 | DL_CALL_FCT ((state)->callbacks.open_outfile, (state, machine, class, data)) |
| 327 | |
| 328 | /* Create the data for the output file. */ |
| 329 | int (*create_outfile) (struct ld_state *); |
| 330 | #define CREATE_OUTFILE(state) \ |
| 331 | DL_CALL_FCT ((state)->callbacks.create_outfile, (state)) |
| 332 | |
| 333 | /* Process a relocation section. */ |
| 334 | void (*relocate_section) (struct ld_state *, Elf_Scn *, struct scninfo *, |
| 335 | const Elf32_Word *); |
| 336 | #define RELOCATE_SECTION(state, outscn, first, dblindirect) \ |
| 337 | DL_CALL_FCT ((state)->callbacks.relocate_section, (state, outscn, first, \ |
| 338 | dblindirect)) |
| 339 | |
| 340 | /* Allocate a data buffer for the relocations of the given output |
| 341 | section. */ |
| 342 | void (*count_relocations) (struct ld_state *, struct scninfo *); |
| 343 | #define COUNT_RELOCATIONS(state, scninfo) \ |
| 344 | DL_CALL_FCT ((state)->callbacks.count_relocations, (state, scninfo)) |
| 345 | |
| 346 | /* Create relocations for executable or DSO. */ |
| 347 | void (*create_relocations) (struct ld_state *, const Elf32_Word *); |
| 348 | #define CREATE_RELOCATIONS(state, dlbindirect) \ |
| 349 | DL_CALL_FCT ((state)->callbacks.create_relocations, (state, dblindirect)) |
| 350 | |
| 351 | /* Finalize the output file. */ |
| 352 | int (*finalize) (struct ld_state *); |
| 353 | #define FINALIZE(state) \ |
| 354 | DL_CALL_FCT ((state)->callbacks.finalize, (state)) |
| 355 | |
| 356 | /* Check whether special section number is known. */ |
| 357 | bool (*special_section_number_p) (struct ld_state *, size_t); |
| 358 | #define SPECIAL_SECTION_NUMBER_P(state, number) \ |
| 359 | DL_CALL_FCT ((state)->callbacks.special_section_number_p, (state, number)) |
| 360 | |
| 361 | /* Check whether section type is known. */ |
| 362 | bool (*section_type_p) (struct ld_state *, XElf_Word); |
| 363 | #define SECTION_TYPE_P(state, type) \ |
| 364 | DL_CALL_FCT ((state)->callbacks.section_type_p, (state, type)) |
| 365 | |
| 366 | /* Return section flags for .dynamic section. */ |
| 367 | XElf_Xword (*dynamic_section_flags) (struct ld_state *); |
| 368 | #define DYNAMIC_SECTION_FLAGS(state) \ |
| 369 | DL_CALL_FCT ((state)->callbacks.dynamic_section_flags, (state)) |
| 370 | |
| 371 | /* Create the data structures for the .plt section and initialize it. */ |
| 372 | void (*initialize_plt) (struct ld_state *, Elf_Scn *scn); |
| 373 | #define INITIALIZE_PLT(state, scn) \ |
| 374 | DL_CALL_FCT ((state)->callbacks.initialize_plt, (state, scn)) |
| 375 | |
| 376 | /* Create the data structures for the .rel.plt section and initialize it. */ |
| 377 | void (*initialize_pltrel) (struct ld_state *, Elf_Scn *scn); |
| 378 | #define INITIALIZE_PLTREL(state, scn) \ |
| 379 | DL_CALL_FCT ((state)->callbacks.initialize_pltrel, (state, scn)) |
| 380 | |
| 381 | /* Finalize the .plt section the what belongs to them. */ |
| 382 | void (*finalize_plt) (struct ld_state *, size_t, size_t); |
| 383 | #define FINALIZE_PLT(state, nsym, nsym_dyn) \ |
| 384 | DL_CALL_FCT ((state)->callbacks.finalize_plt, (state, nsym, nsym_dyn)) |
| 385 | |
| 386 | /* Create the data structures for the .got section and initialize it. */ |
| 387 | void (*initialize_got) (struct ld_state *, Elf_Scn *scn); |
| 388 | #define INITIALIZE_GOT(state, scn) \ |
| 389 | DL_CALL_FCT ((state)->callbacks.initialize_got, (state, scn)) |
| 390 | |
| 391 | /* Return the tag corresponding to the native relocation type for |
| 392 | the platform. */ |
| 393 | int (*rel_type) (struct ld_state *); |
| 394 | #define REL_TYPE(state) \ |
| 395 | DL_CALL_FCT ((state)->callbacks.rel_type, (state)) |
| 396 | }; |
| 397 | |
| 398 | |
| 399 | /* Structure for symbol representation. This data structure is used a |
| 400 | lot, so size is important. */ |
| 401 | struct symbol |
| 402 | { |
| 403 | /* Symbol name. */ |
| 404 | const char *name; |
| 405 | /* Size of the object. */ |
| 406 | XElf_Xword size; |
| 407 | /* Index of the symbol in the symbol table of the object. */ |
| 408 | size_t symidx; |
| 409 | /* Index of the symbol in the symbol table of the output file. */ |
| 410 | size_t outsymidx; |
| 411 | |
| 412 | /* Description where the symbol is found/needed. */ |
| 413 | size_t scndx; |
| 414 | struct usedfiles *file; |
| 415 | /* Index of the symbol table. */ |
| 416 | Elf32_Word symscndx; |
| 417 | |
| 418 | /* Index of the symbol in the dynamic symbol table of the output |
| 419 | file. Note that the value only needs to be 16 bit wide since |
| 420 | there cannot be more sections in an executable or DSO. */ |
| 421 | unsigned int outdynsymidx:16; |
| 422 | |
| 423 | /* Type of the symbol. */ |
| 424 | unsigned int type:4; |
| 425 | /* Various flags. */ |
| 426 | unsigned int defined:1; |
| 427 | unsigned int common:1; |
| 428 | unsigned int weak:1; |
| 429 | unsigned int added:1; |
| 430 | unsigned int merged:1; |
| 431 | /* Nonzero if the symbol is on the from_dso list. */ |
| 432 | unsigned int on_dsolist:1; |
| 433 | /* Nonzero if symbol needs copy relocation, reset when the |
| 434 | relocation has been created. */ |
| 435 | unsigned int need_copy:1; |
| 436 | unsigned int in_dso:1; |
| 437 | |
| 438 | union |
| 439 | { |
| 440 | /* Pointer to the handle created by the functions which create |
| 441 | merged section contents. We use 'void *' because there are |
| 442 | different implementations used. */ |
| 443 | void *handle; |
| 444 | XElf_Addr value; |
| 445 | } merge; |
| 446 | |
| 447 | /* Pointer to next/previous symbol on whatever list the symbol is. */ |
| 448 | struct symbol *next; |
| 449 | struct symbol *previous; |
| 450 | /* Pointer to next symbol of the same section (only set for merge-able |
| 451 | sections). */ |
| 452 | struct symbol *next_in_scn; |
| 453 | }; |
| 454 | |
| 455 | |
| 456 | /* Get the definition for the symbol table. */ |
| 457 | #include <symbolhash.h> |
| 458 | |
| 459 | /* Simple single linked list of file names. */ |
| 460 | struct filename_list |
| 461 | { |
| 462 | const char *name; |
| 463 | struct usedfiles *real; |
| 464 | struct filename_list *next; |
| 465 | bool group_start; |
| 466 | bool group_end; |
| 467 | bool as_needed; |
| 468 | }; |
| 469 | |
| 470 | |
| 471 | /* Data structure to describe expression in linker script. */ |
| 472 | struct expression |
| 473 | { |
| 474 | enum expression_tag |
| 475 | { |
| 476 | exp_num, |
| 477 | exp_sizeof_headers, |
| 478 | exp_pagesize, |
| 479 | exp_id, |
| 480 | exp_mult, |
| 481 | exp_div, |
| 482 | exp_mod, |
| 483 | exp_plus, |
| 484 | exp_minus, |
| 485 | exp_and, |
| 486 | exp_or, |
| 487 | exp_align |
| 488 | } tag; |
| 489 | |
| 490 | union |
| 491 | { |
| 492 | uintmax_t num; |
| 493 | struct expression *child; |
| 494 | struct |
| 495 | { |
| 496 | struct expression *left; |
| 497 | struct expression *right; |
| 498 | } binary; |
| 499 | const char *str; |
| 500 | } val; |
| 501 | }; |
| 502 | |
| 503 | |
| 504 | /* Data structure for section name with flags. */ |
| 505 | struct input_section_name |
| 506 | { |
| 507 | const char *name; |
| 508 | bool sort_flag; |
| 509 | }; |
| 510 | |
| 511 | /* File name mask with section name. */ |
| 512 | struct filemask_section_name |
| 513 | { |
| 514 | const char *filemask; |
| 515 | const char *excludemask; |
| 516 | struct input_section_name *section_name; |
| 517 | bool keep_flag; |
| 518 | }; |
| 519 | |
| 520 | /* Data structure for assignments. */ |
| 521 | struct assignment |
| 522 | { |
| 523 | const char *variable; |
| 524 | struct expression *expression; |
| 525 | struct symbol *sym; |
| 526 | bool provide_flag; |
| 527 | }; |
| 528 | |
| 529 | |
| 530 | /* Data structure describing input for an output section. */ |
| 531 | struct input_rule |
| 532 | { |
| 533 | enum |
| 534 | { |
| 535 | input_section, |
| 536 | input_assignment |
| 537 | } tag; |
| 538 | |
| 539 | union |
| 540 | { |
| 541 | struct assignment *assignment; |
| 542 | struct filemask_section_name *section; |
| 543 | } val; |
| 544 | |
| 545 | struct input_rule *next; |
| 546 | }; |
| 547 | |
| 548 | |
| 549 | /* Data structure to describe output section. */ |
| 550 | struct output_section |
| 551 | { |
| 552 | const char *name; |
| 553 | struct input_rule *input; |
| 554 | XElf_Addr max_alignment; |
| 555 | bool ignored; |
| 556 | }; |
| 557 | |
| 558 | |
| 559 | /* Data structure to describe output file format. */ |
| 560 | struct output_rule |
| 561 | { |
| 562 | enum |
| 563 | { |
| 564 | output_section, |
| 565 | output_assignment |
| 566 | } tag; |
| 567 | |
| 568 | union |
| 569 | { |
| 570 | struct assignment *assignment; |
| 571 | struct output_section section; |
| 572 | } val; |
| 573 | |
| 574 | struct output_rule *next; |
| 575 | }; |
| 576 | |
| 577 | |
| 578 | /* List of all the segments the linker script describes. */ |
| 579 | struct output_segment |
| 580 | { |
| 581 | int mode; |
| 582 | struct output_rule *output_rules; |
| 583 | struct output_segment *next; |
| 584 | |
| 585 | XElf_Off offset; |
| 586 | XElf_Addr addr; |
| 587 | XElf_Xword align; |
| 588 | }; |
| 589 | |
| 590 | |
| 591 | /* List of identifiers. */ |
| 592 | struct id_list |
| 593 | { |
| 594 | union |
| 595 | { |
| 596 | enum id_type |
| 597 | { |
| 598 | id_str, /* Normal string. */ |
| 599 | id_all, /* "*", matches all. */ |
| 600 | id_wild /* Globbing wildcard string. */ |
| 601 | } id_type; |
| 602 | struct |
| 603 | { |
| 604 | bool local; |
| 605 | const char *versionname; |
| 606 | } s; |
| 607 | } u; |
| 608 | const char *id; |
| 609 | struct id_list *next; |
| 610 | }; |
| 611 | |
| 612 | |
| 613 | /* Version information. */ |
| 614 | struct version |
| 615 | { |
| 616 | struct version *next; |
| 617 | struct id_list *local_names; |
| 618 | struct id_list *global_names; |
| 619 | const char *versionname; |
| 620 | const char *parentname; |
| 621 | }; |
| 622 | |
| 623 | |
| 624 | /* Head for list of sections. */ |
| 625 | struct scnhead |
| 626 | { |
| 627 | /* Name of the sections. */ |
| 628 | const char *name; |
| 629 | |
| 630 | /* Accumulated flags for the sections. */ |
| 631 | XElf_Xword flags; |
| 632 | |
| 633 | /* Type of the sections. */ |
| 634 | XElf_Word type; |
| 635 | |
| 636 | /* Entry size. If there are differencs between the sections with |
| 637 | the same name this field contains 1. */ |
| 638 | XElf_Word entsize; |
| 639 | |
| 640 | /* If non-NULL pointer to group signature. */ |
| 641 | const char *grp_signature; |
| 642 | |
| 643 | /* Maximum alignment for all sections. */ |
| 644 | XElf_Word align; |
| 645 | |
| 646 | /* Distinguish between normal sections coming from the input file |
| 647 | and sections generated by the linker. */ |
| 648 | enum scn_kind |
| 649 | { |
| 650 | scn_normal, /* Section from the input file(s). */ |
| 651 | scn_dot_interp, /* Generated .interp section. */ |
| 652 | scn_dot_got, /* Generated .got section. */ |
| 653 | scn_dot_dynrel, /* Generated .rel.dyn section. */ |
| 654 | scn_dot_dynamic, /* Generated .dynamic section. */ |
| 655 | scn_dot_dynsym, /* Generated .dynsym section. */ |
| 656 | scn_dot_dynstr, /* Generated .dynstr section. */ |
| 657 | scn_dot_hash, /* Generated .hash section. */ |
| 658 | scn_dot_plt, /* Generated .plt section. */ |
| 659 | scn_dot_pltrel, /* Generated .rel.plt section. */ |
| 660 | scn_dot_version, /* Generated .gnu.version section. */ |
| 661 | scn_dot_version_r /* Generated .gnu.version_r section. */ |
| 662 | } kind; |
| 663 | |
| 664 | /* True is the section is used in the output. */ |
| 665 | bool used; |
| 666 | |
| 667 | /* Total size (only determined this way for relocation sections). */ |
| 668 | size_t relsize; |
| 669 | |
| 670 | /* Filled in by the section sorting to indicate which segment the |
| 671 | section goes in. */ |
| 672 | int segment_nr; |
| 673 | |
| 674 | /* Index of the output section. We cannot store the section handle |
| 675 | directly here since the handle is a pointer in a dynamically |
| 676 | allocated table which might move if it becomes too small for all |
| 677 | the sections. Using the index the correct value can be found at |
| 678 | all times. */ |
| 679 | XElf_Word scnidx; |
| 680 | |
| 681 | /* Index of the STT_SECTION entry for this section in the symbol |
| 682 | table. */ |
| 683 | XElf_Word scnsymidx; |
| 684 | |
| 685 | /* Address of the section in the output file. */ |
| 686 | XElf_Addr addr; |
| 687 | |
| 688 | /* Handle for the section name in the output file's section header |
| 689 | string table. */ |
| 690 | struct Ebl_Strent *nameent; |
| 691 | |
| 692 | /* Tail of list of symbols for this section. Only set if the |
| 693 | section is merge-able. */ |
| 694 | struct symbol *symbols; |
| 695 | |
| 696 | /* Pointer to last section. */ |
| 697 | struct scninfo *last; |
| 698 | }; |
| 699 | |
| 700 | |
| 701 | /* Define hash table for sections. */ |
| 702 | #include <sectionhash.h> |
| 703 | |
| 704 | /* Define hash table for version symbols. */ |
| 705 | #include <versionhash.h> |
| 706 | |
| 707 | |
| 708 | /* State of the linker. */ |
| 709 | struct ld_state |
| 710 | { |
| 711 | /* ELF backend library handle. */ |
| 712 | Ebl *ebl; |
| 713 | |
| 714 | /* List of all archives participating, in this order. */ |
| 715 | struct usedfiles *archives; |
| 716 | /* End of the list. */ |
| 717 | struct usedfiles *tailarchives; |
| 718 | /* If nonzero we are looking for the beginning of a group. */ |
| 719 | bool group_start_requested; |
| 720 | /* Pointer to the archive starting the group. */ |
| 721 | struct usedfiles *group_start_archive; |
| 722 | |
| 723 | /* List of the DSOs we found. */ |
| 724 | struct usedfiles *dsofiles; |
| 725 | /* Number of DSO files. */ |
| 726 | size_t ndsofiles; |
| 727 | /* Ultimate list of object files which are linked in. */ |
| 728 | struct usedfiles *relfiles; |
| 729 | |
| 730 | /* List the DT_NEEDED DSOs. */ |
| 731 | struct usedfiles *needed; |
| 732 | |
| 733 | /* Temporary storage for the parser. */ |
| 734 | struct filename_list *srcfiles; |
| 735 | |
| 736 | /* List of all the paths to look at. */ |
| 737 | struct pathelement *paths; |
| 738 | /* Tail of the list. */ |
| 739 | struct pathelement *tailpaths; |
| 740 | |
| 741 | /* User provided paths for lookup of DSOs. */ |
| 742 | struct pathelement *rpath; |
| 743 | struct pathelement *rpath_link; |
| 744 | struct pathelement *runpath; |
| 745 | struct pathelement *runpath_link; |
| 746 | struct Ebl_Strent *rxxpath_strent; |
| 747 | int rxxpath_tag; |
| 748 | |
| 749 | /* From the environment variable LD_LIBRARY_PATH. */ |
| 750 | struct pathelement *ld_library_path1; |
| 751 | struct pathelement *ld_library_path2; |
| 752 | |
| 753 | /* Name of the output file. */ |
| 754 | const char *outfname; |
| 755 | /* Name of the temporary file we initially create. */ |
| 756 | const char *tempfname; |
| 757 | /* File descriptor opened for the output file. */ |
| 758 | int outfd; |
| 759 | /* The ELF descriptor for the output file. */ |
| 760 | Elf *outelf; |
| 761 | |
| 762 | /* Type of output file. */ |
| 763 | enum file_type file_type; |
| 764 | |
| 765 | /* Is this a system library or not. */ |
| 766 | bool is_system_library; |
| 767 | |
| 768 | /* Page size to be assumed for the binary. */ |
| 769 | size_t pagesize; |
| 770 | |
| 771 | /* Name of the interpreter for dynamically linked objects. */ |
| 772 | const char *interp; |
| 773 | /* Index of the .interp section. */ |
| 774 | Elf32_Word interpscnidx; |
| 775 | |
| 776 | /* Optimization level. */ |
| 777 | unsigned long int optlevel; |
| 778 | |
| 779 | /* If true static linking is requested. */ |
| 780 | bool statically; |
| 781 | |
| 782 | /* How to extract elements from archives. */ |
| 783 | enum extract_rule extract_rule; |
| 784 | |
| 785 | /* Sequence number of the last archive we used. */ |
| 786 | int last_archive_used; |
| 787 | |
| 788 | /* If true print to stdout information about the files we are |
| 789 | trying to open. */ |
| 790 | bool trace_files; |
| 791 | |
| 792 | /* If true multiple definitions are not considered an error; the |
| 793 | first is used. */ |
| 794 | bool muldefs; |
| 795 | |
| 796 | /* If true undefined symbols when building DSOs are not fatal. */ |
| 797 | bool nodefs; |
| 798 | |
| 799 | /* If true add line indentifying link-editor to .comment section. */ |
| 800 | bool add_ld_comment; |
| 801 | |
| 802 | /* Stripping while linking. */ |
| 803 | enum |
| 804 | { |
| 805 | strip_none, |
| 806 | strip_debug, |
| 807 | strip_all, |
| 808 | strip_everything |
| 809 | } strip; |
| 810 | |
| 811 | /* The callback function vector. */ |
| 812 | struct callbacks callbacks; |
| 813 | |
| 814 | /* Name of the entry symbol. Can also be a numeric value. */ |
| 815 | const char *entry; |
| 816 | |
| 817 | /* The description of the segments in the output file. */ |
| 818 | struct output_segment *output_segments; |
| 819 | |
| 820 | /* List of the symbols we created from linker script definitions. */ |
| 821 | struct symbol *lscript_syms; |
| 822 | size_t nlscript_syms; |
| 823 | |
| 824 | /* Table with known symbols. */ |
| 825 | ld_symbol_tab symbol_tab; |
| 826 | |
| 827 | /* Table with used sections. */ |
| 828 | ld_section_tab section_tab; |
| 829 | |
| 830 | /* The list of sections once we collected them. */ |
| 831 | struct scnhead **allsections; |
| 832 | size_t nallsections; |
| 833 | size_t nusedsections; |
| 834 | size_t nnotesections; |
| 835 | |
| 836 | /* Beginning of the list of symbols which are still unresolved. */ |
| 837 | struct symbol *unresolved; |
| 838 | /* Number of truely unresolved entries in the list. */ |
| 839 | size_t nunresolved; |
| 840 | /* Number of truely unresolved, non-weak entries in the list. */ |
| 841 | size_t nunresolved_nonweak; |
| 842 | |
| 843 | /* List of common symbols. */ |
| 844 | struct symbol *common_syms; |
| 845 | /* Section for the common symbols. */ |
| 846 | struct scninfo *common_section; |
| 847 | |
| 848 | /* List of symbols defined in DSOs and used in a relocatable file. |
| 849 | DSO symbols not referenced in the relocatable files are not on |
| 850 | the list. If a symbol is on the list the on_dsolist field in the |
| 851 | 'struct symbol' is nonzero. */ |
| 852 | struct symbol *from_dso; |
| 853 | /* Number of entries in from_dso. */ |
| 854 | size_t nfrom_dso; |
| 855 | /* Number of entries in the dynamic symbol table. */ |
| 856 | size_t ndynsym; |
| 857 | /* Number of PLT entries from DSO references. */ |
| 858 | size_t nplt; |
| 859 | /* Number of PLT entries from DSO references. */ |
| 860 | size_t ngot; |
| 861 | /* Number of copy relocations. */ |
| 862 | size_t ncopy; |
| 863 | /* Section for copy relocations. */ |
| 864 | struct scninfo *copy_section; |
| 865 | |
| 866 | /* Keeping track of the number of symbols in the output file. */ |
| 867 | size_t nsymtab; |
| 868 | size_t nlocalsymbols; |
| 869 | |
| 870 | /* Special symbols. */ |
| 871 | struct symbol *init_symbol; |
| 872 | struct symbol *fini_symbol; |
| 873 | |
| 874 | /* The description of the segments in the output file as described |
| 875 | in the default linker script. This information will be used in |
| 876 | addition to the user-provided information. */ |
| 877 | struct output_segment *default_output_segments; |
| 878 | /* Search paths added by the default linker script. */ |
| 879 | struct pathelement *default_paths; |
| 880 | |
| 881 | #ifndef BASE_ELF_NAME |
| 882 | /* The handle of the ld backend library. */ |
| 883 | void *ldlib; |
| 884 | #endif |
| 885 | |
| 886 | /* String table for the section headers. */ |
| 887 | struct Ebl_Strtab *shstrtab; |
| 888 | |
| 889 | /* True if output file should contain symbol table. */ |
| 890 | bool need_symtab; |
| 891 | /* Symbol table section. */ |
| 892 | Elf32_Word symscnidx; |
| 893 | /* Extended section table section. */ |
| 894 | Elf32_Word xndxscnidx; |
| 895 | /* Symbol string table section. */ |
| 896 | Elf32_Word strscnidx; |
| 897 | |
| 898 | /* True if output file should contain dynamic symbol table. */ |
| 899 | bool need_dynsym; |
| 900 | /* Dynamic symbol table section. */ |
| 901 | Elf32_Word dynsymscnidx; |
| 902 | /* Dynamic symbol string table section. */ |
| 903 | Elf32_Word dynstrscnidx; |
| 904 | /* Dynamic symbol hash table. */ |
| 905 | size_t hashscnidx; |
| 906 | |
| 907 | /* Procedure linkage table section. */ |
| 908 | Elf32_Word pltscnidx; |
| 909 | /* Number of entries already in the PLT section. */ |
| 910 | size_t nplt_used; |
| 911 | /* Relocation for procedure linkage table section. */ |
| 912 | Elf32_Word pltrelscnidx; |
| 913 | |
| 914 | /* Global offset table section. */ |
| 915 | Elf32_Word gotscnidx; |
| 916 | |
| 917 | /* This section will hole all non-PLT relocations. */ |
| 918 | Elf32_Word reldynscnidx; |
| 919 | |
| 920 | /* Index of the sections to handle versioning. */ |
| 921 | Elf32_Word versymscnidx; |
| 922 | Elf32_Word verneedscnidx; |
| 923 | /* XXX Should the following names be verneed...? */ |
| 924 | /* Number of version definitions in input DSOs used. */ |
| 925 | int nverdefused; |
| 926 | /* Number of input DSOs using versioning. */ |
| 927 | int nverdeffile; |
| 928 | /* Index of next version. */ |
| 929 | int nextveridx; |
| 930 | |
| 931 | /* Hash table for version symbol strings. Only strings without |
| 932 | special characters are hashed here. */ |
| 933 | ld_version_str_tab version_str_tab; |
| 934 | /* At most one of the following two variables is set to true if either |
| 935 | global or local symbol binding is selected as the default. */ |
| 936 | bool default_bind_local; |
| 937 | bool default_bind_global; |
| 938 | |
| 939 | /* True if only used sections are used. */ |
| 940 | bool gc_sections; |
| 941 | |
| 942 | /* Array to determine final index of symbol. */ |
| 943 | Elf32_Word *dblindirect; |
| 944 | |
| 945 | /* Section group handling. */ |
| 946 | struct scngroup |
| 947 | { |
| 948 | Elf32_Word outscnidx; |
| 949 | int nscns; |
| 950 | struct member |
| 951 | { |
| 952 | struct scnhead *scn; |
| 953 | struct member *next; |
| 954 | } *member; |
| 955 | struct Ebl_Strent *nameent; |
| 956 | struct symbol *symbol; |
| 957 | struct scngroup *next; |
| 958 | } *groups; |
| 959 | |
| 960 | /* True if the output file needs a .got section. */ |
| 961 | bool need_got; |
| 962 | /* Number of relocations for GOT section caused. */ |
| 963 | size_t nrel_got; |
| 964 | |
| 965 | /* Number of entries needed in the .dynamic section. */ |
| 966 | int ndynamic; |
| 967 | /* To keep track of added entries. */ |
| 968 | int ndynamic_filled; |
| 969 | /* Index for the dynamic section. */ |
| 970 | Elf32_Word dynamicscnidx; |
| 971 | |
| 972 | /* Flags set in the DT_FLAGS word. */ |
| 973 | Elf32_Word dt_flags; |
| 974 | /* Flags set in the DT_FLAGS_1 word. */ |
| 975 | Elf32_Word dt_flags_1; |
| 976 | /* Flags set in the DT_FEATURE_1 word. */ |
| 977 | Elf32_Word dt_feature_1; |
| 978 | |
| 979 | /* Lazy-loading state for dependencies. */ |
| 980 | bool lazyload; |
| 981 | |
| 982 | /* True is DSOs which are not used in the linking process are not |
| 983 | recorded. */ |
| 984 | bool ignore_unused_dsos; |
| 985 | |
| 986 | |
| 987 | /* True if in executables all global symbols should be exported in |
| 988 | the dynamic symbol table. */ |
| 989 | bool export_all_dynamic; |
| 990 | |
| 991 | /* If DSO is generated, this is the SONAME. */ |
| 992 | const char *soname; |
| 993 | |
| 994 | /* List of all relocation sections. */ |
| 995 | struct scninfo *rellist; |
| 996 | /* Total size of non-PLT relocations. */ |
| 997 | size_t relsize_total; |
| 998 | |
| 999 | /* Record for the GOT symbol, if known. */ |
| 1000 | struct symbol *got_symbol; |
| 1001 | /* Record for the dynamic section symbol, if known. */ |
| 1002 | struct symbol *dyn_symbol; |
| 1003 | |
| 1004 | /* Obstack used for small objects which will not be deleted. */ |
| 1005 | struct obstack smem; |
| 1006 | }; |
| 1007 | |
| 1008 | |
| 1009 | /* The interface to the scanner. */ |
| 1010 | |
| 1011 | /* Parser entry point. */ |
| 1012 | extern int ldparse (void); |
| 1013 | |
| 1014 | /* The input file. */ |
| 1015 | extern FILE *ldin; |
| 1016 | |
| 1017 | /* Name of the input file. */ |
| 1018 | extern const char *ldin_fname; |
| 1019 | |
| 1020 | /* Current line number. Must be reset for a new file. */ |
| 1021 | extern int ldlineno; |
| 1022 | |
| 1023 | /* If nonzero we are currently parsing a version script. */ |
| 1024 | extern int ld_scan_version_script; |
| 1025 | |
| 1026 | /* Flags defined in ld.c. */ |
| 1027 | extern int verbose; |
| 1028 | extern int conserve_memory; |
| 1029 | |
| 1030 | |
| 1031 | /* Linker state. This contains all global information. */ |
| 1032 | extern struct ld_state ld_state; |
| 1033 | |
| 1034 | |
| 1035 | /* Generic ld helper functions. */ |
| 1036 | |
| 1037 | /* Append a new directory to search libraries in. */ |
| 1038 | extern void ld_new_searchdir (const char *dir); |
| 1039 | |
| 1040 | /* Append a new file to the list of input files. */ |
| 1041 | extern struct usedfiles *ld_new_inputfile (const char *fname, |
| 1042 | enum file_type type); |
| 1043 | |
| 1044 | |
| 1045 | /* These are the generic implementations for the callbacks used by ld. */ |
| 1046 | |
| 1047 | /* Initialize state object. This callback function is called after the |
| 1048 | parameters are parsed but before any file is searched for. */ |
| 1049 | extern int ld_prepare_state (const char *emulation); |
| 1050 | |
| 1051 | |
| 1052 | /* Function to determine whether an object will be dynamically linked. */ |
| 1053 | extern bool dynamically_linked_p (void); |
| 1054 | |
| 1055 | /* Helper functions for the architecture specific code. */ |
| 1056 | |
| 1057 | /* Checked whether the symbol is undefined and referenced from a DSO. */ |
| 1058 | extern bool linked_from_dso_p (struct scninfo *scninfo, size_t symidx); |
| 1059 | extern inline bool |
| 1060 | linked_from_dso_p (struct scninfo *scninfo, size_t symidx) |
| 1061 | { |
| 1062 | struct usedfiles *file = scninfo->fileinfo; |
| 1063 | |
| 1064 | /* If this symbol is not undefined in this file it cannot come from |
| 1065 | a DSO. */ |
| 1066 | if (symidx < file->nlocalsymbols) |
| 1067 | return false; |
| 1068 | |
| 1069 | struct symbol *sym = file->symref[symidx]; |
| 1070 | |
| 1071 | return sym->defined && sym->in_dso; |
| 1072 | } |
| 1073 | |
| 1074 | #endif /* ld.h */ |