📄 linker.c
字号:
This linked list is used to hold the elements. */struct archive_list{ struct archive_list *next; int indx;};/* An entry in an archive hash table. */struct archive_hash_entry{ struct bfd_hash_entry root; /* Where the symbol is defined. */ struct archive_list *defs;};/* An archive hash table itself. */struct archive_hash_table{ struct bfd_hash_table table;};static struct bfd_hash_entry *archive_hash_newfunc PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));static boolean archive_hash_table_init PARAMS ((struct archive_hash_table *, struct bfd_hash_entry *(*) (struct bfd_hash_entry *, struct bfd_hash_table *, const char *)));/* Create a new entry for an archive hash table. */static struct bfd_hash_entry *archive_hash_newfunc (entry, table, string) struct bfd_hash_entry *entry; struct bfd_hash_table *table; const char *string;{ struct archive_hash_entry *ret = (struct archive_hash_entry *) entry; /* Allocate the structure if it has not already been allocated by a subclass. */ if (ret == (struct archive_hash_entry *) NULL) ret = ((struct archive_hash_entry *) bfd_hash_allocate (table, sizeof (struct archive_hash_entry))); if (ret == (struct archive_hash_entry *) NULL) return NULL; /* Call the allocation method of the superclass. */ ret = ((struct archive_hash_entry *) bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string)); if (ret) { /* Initialize the local fields. */ ret->defs = (struct archive_list *) NULL; } return (struct bfd_hash_entry *) ret;}/* Initialize an archive hash table. */static booleanarchive_hash_table_init (table, newfunc) struct archive_hash_table *table; struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));{ return bfd_hash_table_init (&table->table, newfunc);}/* Look up an entry in an archive hash table. */#define archive_hash_lookup(t, string, create, copy) \ ((struct archive_hash_entry *) \ bfd_hash_lookup (&(t)->table, (string), (create), (copy)))/* Allocate space in an archive hash table. */#define archive_hash_allocate(t, size) bfd_hash_allocate (&(t)->table, (size))/* Free an archive hash table. */#define archive_hash_table_free(t) bfd_hash_table_free (&(t)->table)/* Generic function to add symbols from an archive file to the global hash file. This function presumes that the archive symbol table has already been read in (this is normally done by the bfd_check_format entry point). It looks through the undefined and common symbols and searches the archive symbol table for them. If it finds an entry, it includes the associated object file in the link. The old linker looked through the archive symbol table for undefined symbols. We do it the other way around, looking through undefined symbols for symbols defined in the archive. The advantage of the newer scheme is that we only have to look through the list of undefined symbols once, whereas the old method had to re-search the symbol table each time a new object file was added. The CHECKFN argument is used to see if an object file should be included. CHECKFN should set *PNEEDED to true if the object file should be included, and must also call the bfd_link_info add_archive_element callback function and handle adding the symbols to the global hash table. CHECKFN should only return false if some sort of error occurs. For some formats, such as a.out, it is possible to look through an object file but not actually include it in the link. The archive_pass field in a BFD is used to avoid checking the symbols of an object files too many times. When an object is included in the link, archive_pass is set to -1. If an object is scanned but not included, archive_pass is set to the pass number. The pass number is incremented each time a new object file is included. The pass number is used because when a new object file is included it may create new undefined symbols which cause a previously examined object file to be included. */boolean_bfd_generic_link_add_archive_symbols (abfd, info, checkfn) bfd *abfd; struct bfd_link_info *info; boolean (*checkfn) PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));{ carsym *arsyms; carsym *arsym_end; register carsym *arsym; int pass; struct archive_hash_table arsym_hash; int indx; struct bfd_link_hash_entry **pundef; if (! bfd_has_map (abfd)) { /* An empty archive is a special case. */ if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL) return true; bfd_set_error (bfd_error_no_armap); return false; } arsyms = bfd_ardata (abfd)->symdefs; arsym_end = arsyms + bfd_ardata (abfd)->symdef_count; /* In order to quickly determine whether an symbol is defined in this archive, we build a hash table of the symbols. */ if (! archive_hash_table_init (&arsym_hash, archive_hash_newfunc)) return false; for (arsym = arsyms, indx = 0; arsym < arsym_end; arsym++, indx++) { struct archive_hash_entry *arh; struct archive_list *l, **pp; arh = archive_hash_lookup (&arsym_hash, arsym->name, true, false); if (arh == (struct archive_hash_entry *) NULL) goto error_return; l = ((struct archive_list *) archive_hash_allocate (&arsym_hash, sizeof (struct archive_list))); if (l == NULL) goto error_return; l->indx = indx; for (pp = &arh->defs; *pp != (struct archive_list *) NULL; pp = &(*pp)->next) ; *pp = l; l->next = NULL; } /* The archive_pass field in the archive itself is used to initialize PASS, sine we may search the same archive multiple times. */ pass = abfd->archive_pass + 1; /* New undefined symbols are added to the end of the list, so we only need to look through it once. */ pundef = &info->hash->undefs; while (*pundef != (struct bfd_link_hash_entry *) NULL) { struct bfd_link_hash_entry *h; struct archive_hash_entry *arh; struct archive_list *l; h = *pundef; /* When a symbol is defined, it is not necessarily removed from the list. */ if (h->type != bfd_link_hash_undefined && h->type != bfd_link_hash_common) { /* Remove this entry from the list, for general cleanliness and because we are going to look through the list again if we search any more libraries. We can't remove the entry if it is the tail, because that would lose any entries we add to the list later on (it would also cause us to lose track of whether the symbol has been referenced). */ if (*pundef != info->hash->undefs_tail) *pundef = (*pundef)->next; else pundef = &(*pundef)->next; continue; } /* Look for this symbol in the archive symbol map. */ arh = archive_hash_lookup (&arsym_hash, h->root.string, false, false); if (arh == (struct archive_hash_entry *) NULL) { pundef = &(*pundef)->next; continue; } /* Look at all the objects which define this symbol. */ for (l = arh->defs; l != (struct archive_list *) NULL; l = l->next) { bfd *element; boolean needed; /* If the symbol has gotten defined along the way, quit. */ if (h->type != bfd_link_hash_undefined && h->type != bfd_link_hash_common) break; element = bfd_get_elt_at_index (abfd, l->indx); if (element == (bfd *) NULL) goto error_return; /* If we've already included this element, or if we've already checked it on this pass, continue. */ if (element->archive_pass == -1 || element->archive_pass == pass) continue; /* If we can't figure this element out, just ignore it. */ if (! bfd_check_format (element, bfd_object)) { element->archive_pass = -1; continue; } /* CHECKFN will see if this element should be included, and go ahead and include it if appropriate. */ if (! (*checkfn) (element, info, &needed)) goto error_return; if (! needed) element->archive_pass = pass; else { element->archive_pass = -1; /* Increment the pass count to show that we may need to recheck object files which were already checked. */ ++pass; } } pundef = &(*pundef)->next; } archive_hash_table_free (&arsym_hash); /* Save PASS in case we are called again. */ abfd->archive_pass = pass; return true; error_return: archive_hash_table_free (&arsym_hash); return false;}/* See if we should include an archive element. This version is used when we do not want to automatically collect constructors based on the symbol name, presumably because we have some other mechanism for finding them. */static booleangeneric_link_check_archive_element_no_collect (abfd, info, pneeded) bfd *abfd; struct bfd_link_info *info; boolean *pneeded;{ return generic_link_check_archive_element (abfd, info, pneeded, false);}/* See if we should include an archive element. This version is used when we want to automatically collect constructors based on the symbol name, as collect2 does. */static booleangeneric_link_check_archive_element_collect (abfd, info, pneeded) bfd *abfd; struct bfd_link_info *info; boolean *pneeded;{ return generic_link_check_archive_element (abfd, info, pneeded, true);}/* See if we should include an archive element. Optionally collect constructors. */static booleangeneric_link_check_archive_element (abfd, info, pneeded, collect) bfd *abfd; struct bfd_link_info *info; boolean *pneeded; boolean collect;{ asymbol **pp, **ppend; *pneeded = false; if (! generic_link_read_symbols (abfd)) return false; pp = _bfd_generic_link_get_symbols (abfd); ppend = pp + _bfd_generic_link_get_symcount (abfd); for (; pp < ppend; pp++) { asymbol *p; struct bfd_link_hash_entry *h; p = *pp; /* We are only interested in globally visible symbols. */ if (! bfd_is_com_section (p->section) && (p->flags & (BSF_GLOBAL | BSF_INDIRECT | BSF_WEAK)) == 0) continue; /* We are only interested if we know something about this symbol, and it is undefined or common. An undefined weak symbol (type bfd_link_hash_undefweak) is not considered to be a reference when pulling files out of an archive. See the SVR4 ABI, p. 4-27. */ h = bfd_link_hash_lookup (info->hash, bfd_asymbol_name (p), false, false, true); if (h == (struct bfd_link_hash_entry *) NULL || (h->type != bfd_link_hash_undefined && h->type != bfd_link_hash_common)) continue; /* P is a symbol we are looking for. */ if (! bfd_is_com_section (p->section)) { bfd_size_type symcount; asymbol **symbols; /* This object file defines this symbol, so pull it in. */ if (! (*info->callbacks->add_archive_element) (info, abfd, bfd_asymbol_name (p))) return false; symcount = _bfd_generic_link_get_symcount (abfd); symbols = _bfd_generic_link_get_symbols (abfd); if (! generic_link_add_symbol_list (abfd, info, symcount, symbols, collect)) return false; *pneeded = true; return true; } /* P is a common symbol. */ if (h->type == bfd_link_hash_undefined) { bfd *symbfd; bfd_vma size; unsigned int power; symbfd = h->u.undef.abfd; if (symbfd == (bfd *) NULL) { /* This symbol was created as undefined from outside BFD. We assume that we should link in the object file. This is for the -u option in the linker. */ if (! (*info->callbacks->add_archive_element) (info, abfd, bfd_asymbol_name (p))) return false; *pneeded = true; return true; } /* Turn the symbol into a common symbol but do not link in the object file. This is how a.out works. Object formats that require different semantics must implement this function differently. This symbol is already on the undefs list. We add the section to a common section attached to symbfd to ensure that it is in a BFD which will be linked in. */ h->type = bfd_link_hash_common;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -