Home | History | Annotate | Line # | Download | only in bfd
      1   1.1  christos /* linker.c -- BFD linker routines
      2  1.10  christos    Copyright (C) 1993-2025 Free Software Foundation, Inc.
      3   1.1  christos    Written by Steve Chamberlain and Ian Lance Taylor, Cygnus Support
      4   1.1  christos 
      5   1.1  christos    This file is part of BFD, the Binary File Descriptor library.
      6   1.1  christos 
      7   1.1  christos    This program is free software; you can redistribute it and/or modify
      8   1.1  christos    it under the terms of the GNU General Public License as published by
      9   1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10   1.1  christos    (at your option) any later version.
     11   1.1  christos 
     12   1.1  christos    This program is distributed in the hope that it will be useful,
     13   1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14   1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15   1.1  christos    GNU General Public License for more details.
     16   1.1  christos 
     17   1.1  christos    You should have received a copy of the GNU General Public License
     18   1.1  christos    along with this program; if not, write to the Free Software
     19   1.1  christos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     20   1.1  christos    MA 02110-1301, USA.  */
     21   1.1  christos 
     22   1.1  christos #include "sysdep.h"
     23   1.1  christos #include "bfd.h"
     24   1.1  christos #include "libbfd.h"
     25   1.1  christos #include "bfdlink.h"
     26   1.1  christos #include "genlink.h"
     27   1.1  christos 
     28   1.1  christos /*
     29   1.1  christos SECTION
     30   1.1  christos 	Linker Functions
     31   1.1  christos 
     32   1.1  christos @cindex Linker
     33   1.1  christos 	The linker uses three special entry points in the BFD target
     34   1.1  christos 	vector.  It is not necessary to write special routines for
     35   1.1  christos 	these entry points when creating a new BFD back end, since
     36   1.1  christos 	generic versions are provided.  However, writing them can
     37   1.1  christos 	speed up linking and make it use significantly less runtime
     38   1.1  christos 	memory.
     39   1.1  christos 
     40   1.1  christos 	The first routine creates a hash table used by the other
     41   1.1  christos 	routines.  The second routine adds the symbols from an object
     42   1.1  christos 	file to the hash table.  The third routine takes all the
     43   1.1  christos 	object files and links them together to create the output
     44   1.1  christos 	file.  These routines are designed so that the linker proper
     45   1.1  christos 	does not need to know anything about the symbols in the object
     46   1.1  christos 	files that it is linking.  The linker merely arranges the
     47   1.1  christos 	sections as directed by the linker script and lets BFD handle
     48   1.1  christos 	the details of symbols and relocs.
     49   1.1  christos 
     50   1.1  christos 	The second routine and third routines are passed a pointer to
     51   1.1  christos 	a <<struct bfd_link_info>> structure (defined in
     52   1.1  christos 	<<bfdlink.h>>) which holds information relevant to the link,
     53   1.1  christos 	including the linker hash table (which was created by the
     54   1.1  christos 	first routine) and a set of callback functions to the linker
     55   1.1  christos 	proper.
     56   1.1  christos 
     57   1.1  christos 	The generic linker routines are in <<linker.c>>, and use the
     58   1.1  christos 	header file <<genlink.h>>.  As of this writing, the only back
     59   1.1  christos 	ends which have implemented versions of these routines are
     60   1.1  christos 	a.out (in <<aoutx.h>>) and ECOFF (in <<ecoff.c>>).  The a.out
     61   1.1  christos 	routines are used as examples throughout this section.
     62   1.1  christos 
     63   1.1  christos @menu
     64   1.1  christos @* Creating a Linker Hash Table::
     65   1.1  christos @* Adding Symbols to the Hash Table::
     66   1.1  christos @* Performing the Final Link::
     67   1.1  christos @end menu
     68   1.1  christos 
     69   1.1  christos INODE
     70   1.1  christos Creating a Linker Hash Table, Adding Symbols to the Hash Table, Linker Functions, Linker Functions
     71   1.1  christos SUBSECTION
     72   1.1  christos 	Creating a linker hash table
     73   1.1  christos 
     74   1.1  christos @cindex _bfd_link_hash_table_create in target vector
     75   1.1  christos @cindex target vector (_bfd_link_hash_table_create)
     76   1.1  christos 	The linker routines must create a hash table, which must be
     77   1.1  christos 	derived from <<struct bfd_link_hash_table>> described in
     78   1.1  christos 	<<bfdlink.c>>.  @xref{Hash Tables}, for information on how to
     79   1.1  christos 	create a derived hash table.  This entry point is called using
     80   1.1  christos 	the target vector of the linker output file.
     81   1.1  christos 
     82   1.1  christos 	The <<_bfd_link_hash_table_create>> entry point must allocate
     83   1.1  christos 	and initialize an instance of the desired hash table.  If the
     84   1.1  christos 	back end does not require any additional information to be
     85   1.1  christos 	stored with the entries in the hash table, the entry point may
     86   1.1  christos 	simply create a <<struct bfd_link_hash_table>>.  Most likely,
     87   1.1  christos 	however, some additional information will be needed.
     88   1.1  christos 
     89   1.1  christos 	For example, with each entry in the hash table the a.out
     90   1.1  christos 	linker keeps the index the symbol has in the final output file
     91   1.1  christos 	(this index number is used so that when doing a relocatable
     92   1.1  christos 	link the symbol index used in the output file can be quickly
     93   1.1  christos 	filled in when copying over a reloc).  The a.out linker code
     94   1.1  christos 	defines the required structures and functions for a hash table
     95   1.1  christos 	derived from <<struct bfd_link_hash_table>>.  The a.out linker
     96   1.1  christos 	hash table is created by the function
     97   1.1  christos 	<<NAME(aout,link_hash_table_create)>>; it simply allocates
     98   1.1  christos 	space for the hash table, initializes it, and returns a
     99   1.1  christos 	pointer to it.
    100   1.1  christos 
    101   1.1  christos 	When writing the linker routines for a new back end, you will
    102   1.1  christos 	generally not know exactly which fields will be required until
    103   1.1  christos 	you have finished.  You should simply create a new hash table
    104   1.1  christos 	which defines no additional fields, and then simply add fields
    105   1.1  christos 	as they become necessary.
    106   1.1  christos 
    107   1.1  christos INODE
    108   1.1  christos Adding Symbols to the Hash Table, Performing the Final Link, Creating a Linker Hash Table, Linker Functions
    109   1.1  christos SUBSECTION
    110   1.1  christos 	Adding symbols to the hash table
    111   1.1  christos 
    112   1.1  christos @cindex _bfd_link_add_symbols in target vector
    113   1.1  christos @cindex target vector (_bfd_link_add_symbols)
    114   1.1  christos 	The linker proper will call the <<_bfd_link_add_symbols>>
    115   1.1  christos 	entry point for each object file or archive which is to be
    116   1.1  christos 	linked (typically these are the files named on the command
    117   1.1  christos 	line, but some may also come from the linker script).  The
    118   1.1  christos 	entry point is responsible for examining the file.  For an
    119   1.1  christos 	object file, BFD must add any relevant symbol information to
    120   1.1  christos 	the hash table.  For an archive, BFD must determine which
    121   1.1  christos 	elements of the archive should be used and adding them to the
    122   1.1  christos 	link.
    123   1.1  christos 
    124   1.1  christos 	The a.out version of this entry point is
    125   1.1  christos 	<<NAME(aout,link_add_symbols)>>.
    126   1.1  christos 
    127   1.1  christos @menu
    128   1.1  christos @* Differing file formats::
    129   1.1  christos @* Adding symbols from an object file::
    130   1.1  christos @* Adding symbols from an archive::
    131   1.1  christos @end menu
    132   1.1  christos 
    133   1.1  christos INODE
    134   1.1  christos Differing file formats, Adding symbols from an object file, Adding Symbols to the Hash Table, Adding Symbols to the Hash Table
    135   1.1  christos SUBSUBSECTION
    136   1.1  christos 	Differing file formats
    137   1.1  christos 
    138   1.1  christos 	Normally all the files involved in a link will be of the same
    139   1.1  christos 	format, but it is also possible to link together different
    140   1.1  christos 	format object files, and the back end must support that.  The
    141   1.1  christos 	<<_bfd_link_add_symbols>> entry point is called via the target
    142   1.1  christos 	vector of the file to be added.  This has an important
    143   1.1  christos 	consequence: the function may not assume that the hash table
    144   1.1  christos 	is the type created by the corresponding
    145   1.1  christos 	<<_bfd_link_hash_table_create>> vector.  All the
    146   1.1  christos 	<<_bfd_link_add_symbols>> function can assume about the hash
    147   1.1  christos 	table is that it is derived from <<struct
    148   1.1  christos 	bfd_link_hash_table>>.
    149   1.1  christos 
    150   1.1  christos 	Sometimes the <<_bfd_link_add_symbols>> function must store
    151   1.1  christos 	some information in the hash table entry to be used by the
    152   1.1  christos 	<<_bfd_final_link>> function.  In such a case the output bfd
    153   1.1  christos 	xvec must be checked to make sure that the hash table was
    154   1.1  christos 	created by an object file of the same format.
    155   1.1  christos 
    156   1.1  christos 	The <<_bfd_final_link>> routine must be prepared to handle a
    157   1.1  christos 	hash entry without any extra information added by the
    158   1.1  christos 	<<_bfd_link_add_symbols>> function.  A hash entry without
    159   1.1  christos 	extra information will also occur when the linker script
    160   1.1  christos 	directs the linker to create a symbol.  Note that, regardless
    161   1.1  christos 	of how a hash table entry is added, all the fields will be
    162   1.1  christos 	initialized to some sort of null value by the hash table entry
    163   1.1  christos 	initialization function.
    164   1.1  christos 
    165   1.1  christos 	See <<ecoff_link_add_externals>> for an example of how to
    166   1.1  christos 	check the output bfd before saving information (in this
    167   1.1  christos 	case, the ECOFF external symbol debugging information) in a
    168   1.1  christos 	hash table entry.
    169   1.1  christos 
    170   1.1  christos INODE
    171   1.1  christos Adding symbols from an object file, Adding symbols from an archive, Differing file formats, Adding Symbols to the Hash Table
    172   1.1  christos SUBSUBSECTION
    173   1.1  christos 	Adding symbols from an object file
    174   1.1  christos 
    175   1.1  christos 	When the <<_bfd_link_add_symbols>> routine is passed an object
    176   1.1  christos 	file, it must add all externally visible symbols in that
    177   1.1  christos 	object file to the hash table.  The actual work of adding the
    178   1.1  christos 	symbol to the hash table is normally handled by the function
    179   1.1  christos 	<<_bfd_generic_link_add_one_symbol>>.  The
    180   1.1  christos 	<<_bfd_link_add_symbols>> routine is responsible for reading
    181   1.1  christos 	all the symbols from the object file and passing the correct
    182   1.1  christos 	information to <<_bfd_generic_link_add_one_symbol>>.
    183   1.1  christos 
    184   1.1  christos 	The <<_bfd_link_add_symbols>> routine should not use
    185   1.1  christos 	<<bfd_canonicalize_symtab>> to read the symbols.  The point of
    186   1.1  christos 	providing this routine is to avoid the overhead of converting
    187   1.1  christos 	the symbols into generic <<asymbol>> structures.
    188   1.1  christos 
    189   1.1  christos @findex _bfd_generic_link_add_one_symbol
    190   1.1  christos 	<<_bfd_generic_link_add_one_symbol>> handles the details of
    191   1.1  christos 	combining common symbols, warning about multiple definitions,
    192   1.1  christos 	and so forth.  It takes arguments which describe the symbol to
    193   1.1  christos 	add, notably symbol flags, a section, and an offset.  The
    194   1.1  christos 	symbol flags include such things as <<BSF_WEAK>> or
    195   1.1  christos 	<<BSF_INDIRECT>>.  The section is a section in the object
    196   1.1  christos 	file, or something like <<bfd_und_section_ptr>> for an undefined
    197   1.1  christos 	symbol or <<bfd_com_section_ptr>> for a common symbol.
    198   1.1  christos 
    199   1.1  christos 	If the <<_bfd_final_link>> routine is also going to need to
    200   1.1  christos 	read the symbol information, the <<_bfd_link_add_symbols>>
    201   1.1  christos 	routine should save it somewhere attached to the object file
    202   1.1  christos 	BFD.  However, the information should only be saved if the
    203   1.1  christos 	<<keep_memory>> field of the <<info>> argument is TRUE, so
    204   1.1  christos 	that the <<-no-keep-memory>> linker switch is effective.
    205   1.1  christos 
    206   1.1  christos 	The a.out function which adds symbols from an object file is
    207   1.1  christos 	<<aout_link_add_object_symbols>>, and most of the interesting
    208   1.1  christos 	work is in <<aout_link_add_symbols>>.  The latter saves
    209   1.1  christos 	pointers to the hash tables entries created by
    210   1.1  christos 	<<_bfd_generic_link_add_one_symbol>> indexed by symbol number,
    211   1.1  christos 	so that the <<_bfd_final_link>> routine does not have to call
    212   1.1  christos 	the hash table lookup routine to locate the entry.
    213   1.1  christos 
    214   1.1  christos INODE
    215   1.1  christos Adding symbols from an archive, , Adding symbols from an object file, Adding Symbols to the Hash Table
    216   1.1  christos SUBSUBSECTION
    217   1.1  christos 	Adding symbols from an archive
    218   1.1  christos 
    219   1.1  christos 	When the <<_bfd_link_add_symbols>> routine is passed an
    220   1.1  christos 	archive, it must look through the symbols defined by the
    221   1.1  christos 	archive and decide which elements of the archive should be
    222   1.1  christos 	included in the link.  For each such element it must call the
    223   1.1  christos 	<<add_archive_element>> linker callback, and it must add the
    224   1.1  christos 	symbols from the object file to the linker hash table.  (The
    225   1.1  christos 	callback may in fact indicate that a replacement BFD should be
    226   1.1  christos 	used, in which case the symbols from that BFD should be added
    227   1.1  christos 	to the linker hash table instead.)
    228   1.1  christos 
    229   1.1  christos @findex _bfd_generic_link_add_archive_symbols
    230   1.1  christos 	In most cases the work of looking through the symbols in the
    231   1.1  christos 	archive should be done by the
    232   1.3  christos 	<<_bfd_generic_link_add_archive_symbols>> function.
    233   1.1  christos 	<<_bfd_generic_link_add_archive_symbols>> is passed a function
    234   1.1  christos 	to call to make the final decision about adding an archive
    235   1.1  christos 	element to the link and to do the actual work of adding the
    236   1.3  christos 	symbols to the linker hash table.  If the element is to
    237   1.1  christos 	be included, the <<add_archive_element>> linker callback
    238   1.1  christos 	routine must be called with the element as an argument, and
    239   1.1  christos 	the element's symbols must be added to the linker hash table
    240   1.1  christos 	just as though the element had itself been passed to the
    241   1.3  christos 	<<_bfd_link_add_symbols>> function.
    242   1.1  christos 
    243   1.1  christos 	When the a.out <<_bfd_link_add_symbols>> function receives an
    244   1.1  christos 	archive, it calls <<_bfd_generic_link_add_archive_symbols>>
    245   1.1  christos 	passing <<aout_link_check_archive_element>> as the function
    246   1.1  christos 	argument. <<aout_link_check_archive_element>> calls
    247   1.1  christos 	<<aout_link_check_ar_symbols>>.  If the latter decides to add
    248   1.1  christos 	the element (an element is only added if it provides a real,
    249   1.1  christos 	non-common, definition for a previously undefined or common
    250   1.1  christos 	symbol) it calls the <<add_archive_element>> callback and then
    251   1.1  christos 	<<aout_link_check_archive_element>> calls
    252   1.1  christos 	<<aout_link_add_symbols>> to actually add the symbols to the
    253   1.1  christos 	linker hash table - possibly those of a substitute BFD, if the
    254   1.1  christos 	<<add_archive_element>> callback avails itself of that option.
    255   1.1  christos 
    256   1.1  christos 	The ECOFF back end is unusual in that it does not normally
    257   1.1  christos 	call <<_bfd_generic_link_add_archive_symbols>>, because ECOFF
    258   1.1  christos 	archives already contain a hash table of symbols.  The ECOFF
    259   1.1  christos 	back end searches the archive itself to avoid the overhead of
    260   1.1  christos 	creating a new hash table.
    261   1.1  christos 
    262   1.1  christos INODE
    263   1.1  christos Performing the Final Link, , Adding Symbols to the Hash Table, Linker Functions
    264   1.1  christos SUBSECTION
    265   1.1  christos 	Performing the final link
    266   1.1  christos 
    267   1.1  christos @cindex _bfd_link_final_link in target vector
    268   1.1  christos @cindex target vector (_bfd_final_link)
    269   1.1  christos 	When all the input files have been processed, the linker calls
    270   1.1  christos 	the <<_bfd_final_link>> entry point of the output BFD.  This
    271   1.1  christos 	routine is responsible for producing the final output file,
    272   1.1  christos 	which has several aspects.  It must relocate the contents of
    273   1.1  christos 	the input sections and copy the data into the output sections.
    274   1.1  christos 	It must build an output symbol table including any local
    275   1.1  christos 	symbols from the input files and the global symbols from the
    276   1.1  christos 	hash table.  When producing relocatable output, it must
    277   1.1  christos 	modify the input relocs and write them into the output file.
    278   1.1  christos 	There may also be object format dependent work to be done.
    279   1.1  christos 
    280   1.1  christos 	The linker will also call the <<write_object_contents>> entry
    281   1.1  christos 	point when the BFD is closed.  The two entry points must work
    282   1.1  christos 	together in order to produce the correct output file.
    283   1.1  christos 
    284   1.1  christos 	The details of how this works are inevitably dependent upon
    285   1.1  christos 	the specific object file format.  The a.out
    286   1.1  christos 	<<_bfd_final_link>> routine is <<NAME(aout,final_link)>>.
    287   1.1  christos 
    288   1.1  christos @menu
    289   1.1  christos @* Information provided by the linker::
    290   1.1  christos @* Relocating the section contents::
    291   1.1  christos @* Writing the symbol table::
    292   1.1  christos @end menu
    293   1.1  christos 
    294   1.1  christos INODE
    295   1.1  christos Information provided by the linker, Relocating the section contents, Performing the Final Link, Performing the Final Link
    296   1.1  christos SUBSUBSECTION
    297   1.1  christos 	Information provided by the linker
    298   1.1  christos 
    299   1.1  christos 	Before the linker calls the <<_bfd_final_link>> entry point,
    300   1.1  christos 	it sets up some data structures for the function to use.
    301   1.1  christos 
    302   1.1  christos 	The <<input_bfds>> field of the <<bfd_link_info>> structure
    303   1.1  christos 	will point to a list of all the input files included in the
    304   1.3  christos 	link.  These files are linked through the <<link.next>> field
    305   1.1  christos 	of the <<bfd>> structure.
    306   1.1  christos 
    307   1.1  christos 	Each section in the output file will have a list of
    308   1.1  christos 	<<link_order>> structures attached to the <<map_head.link_order>>
    309   1.1  christos 	field (the <<link_order>> structure is defined in
    310   1.1  christos 	<<bfdlink.h>>).  These structures describe how to create the
    311   1.1  christos 	contents of the output section in terms of the contents of
    312   1.1  christos 	various input sections, fill constants, and, eventually, other
    313   1.1  christos 	types of information.  They also describe relocs that must be
    314   1.1  christos 	created by the BFD backend, but do not correspond to any input
    315   1.1  christos 	file; this is used to support -Ur, which builds constructors
    316   1.1  christos 	while generating a relocatable object file.
    317   1.1  christos 
    318   1.1  christos INODE
    319   1.1  christos Relocating the section contents, Writing the symbol table, Information provided by the linker, Performing the Final Link
    320   1.1  christos SUBSUBSECTION
    321   1.1  christos 	Relocating the section contents
    322   1.1  christos 
    323   1.1  christos 	The <<_bfd_final_link>> function should look through the
    324   1.1  christos 	<<link_order>> structures attached to each section of the
    325   1.1  christos 	output file.  Each <<link_order>> structure should either be
    326   1.1  christos 	handled specially, or it should be passed to the function
    327   1.1  christos 	<<_bfd_default_link_order>> which will do the right thing
    328   1.1  christos 	(<<_bfd_default_link_order>> is defined in <<linker.c>>).
    329   1.1  christos 
    330   1.1  christos 	For efficiency, a <<link_order>> of type
    331   1.1  christos 	<<bfd_indirect_link_order>> whose associated section belongs
    332   1.1  christos 	to a BFD of the same format as the output BFD must be handled
    333   1.1  christos 	specially.  This type of <<link_order>> describes part of an
    334   1.1  christos 	output section in terms of a section belonging to one of the
    335   1.1  christos 	input files.  The <<_bfd_final_link>> function should read the
    336   1.1  christos 	contents of the section and any associated relocs, apply the
    337   1.1  christos 	relocs to the section contents, and write out the modified
    338   1.1  christos 	section contents.  If performing a relocatable link, the
    339   1.1  christos 	relocs themselves must also be modified and written out.
    340   1.1  christos 
    341   1.1  christos @findex _bfd_relocate_contents
    342   1.1  christos @findex _bfd_final_link_relocate
    343   1.1  christos 	The functions <<_bfd_relocate_contents>> and
    344   1.1  christos 	<<_bfd_final_link_relocate>> provide some general support for
    345   1.1  christos 	performing the actual relocations, notably overflow checking.
    346   1.1  christos 	Their arguments include information about the symbol the
    347   1.1  christos 	relocation is against and a <<reloc_howto_type>> argument
    348   1.1  christos 	which describes the relocation to perform.  These functions
    349   1.1  christos 	are defined in <<reloc.c>>.
    350   1.1  christos 
    351   1.1  christos 	The a.out function which handles reading, relocating, and
    352   1.1  christos 	writing section contents is <<aout_link_input_section>>.  The
    353   1.1  christos 	actual relocation is done in <<aout_link_input_section_std>>
    354   1.1  christos 	and <<aout_link_input_section_ext>>.
    355   1.1  christos 
    356   1.1  christos INODE
    357   1.1  christos Writing the symbol table, , Relocating the section contents, Performing the Final Link
    358   1.1  christos SUBSUBSECTION
    359   1.1  christos 	Writing the symbol table
    360   1.1  christos 
    361   1.1  christos 	The <<_bfd_final_link>> function must gather all the symbols
    362   1.1  christos 	in the input files and write them out.  It must also write out
    363   1.1  christos 	all the symbols in the global hash table.  This must be
    364   1.1  christos 	controlled by the <<strip>> and <<discard>> fields of the
    365   1.1  christos 	<<bfd_link_info>> structure.
    366   1.1  christos 
    367   1.1  christos 	The local symbols of the input files will not have been
    368   1.1  christos 	entered into the linker hash table.  The <<_bfd_final_link>>
    369   1.1  christos 	routine must consider each input file and include the symbols
    370   1.1  christos 	in the output file.  It may be convenient to do this when
    371   1.1  christos 	looking through the <<link_order>> structures, or it may be
    372   1.1  christos 	done by stepping through the <<input_bfds>> list.
    373   1.1  christos 
    374   1.1  christos 	The <<_bfd_final_link>> routine must also traverse the global
    375   1.1  christos 	hash table to gather all the externally visible symbols.  It
    376   1.1  christos 	is possible that most of the externally visible symbols may be
    377   1.1  christos 	written out when considering the symbols of each input file,
    378   1.1  christos 	but it is still necessary to traverse the hash table since the
    379   1.1  christos 	linker script may have defined some symbols that are not in
    380   1.1  christos 	any of the input files.
    381   1.1  christos 
    382   1.1  christos 	The <<strip>> field of the <<bfd_link_info>> structure
    383   1.1  christos 	controls which symbols are written out.  The possible values
    384   1.1  christos 	are listed in <<bfdlink.h>>.  If the value is <<strip_some>>,
    385   1.1  christos 	then the <<keep_hash>> field of the <<bfd_link_info>>
    386   1.1  christos 	structure is a hash table of symbols to keep; each symbol
    387   1.1  christos 	should be looked up in this hash table, and only symbols which
    388   1.1  christos 	are present should be included in the output file.
    389   1.1  christos 
    390   1.1  christos 	If the <<strip>> field of the <<bfd_link_info>> structure
    391   1.1  christos 	permits local symbols to be written out, the <<discard>> field
    392   1.1  christos 	is used to further controls which local symbols are included
    393   1.1  christos 	in the output file.  If the value is <<discard_l>>, then all
    394   1.1  christos 	local symbols which begin with a certain prefix are discarded;
    395   1.1  christos 	this is controlled by the <<bfd_is_local_label_name>> entry point.
    396   1.1  christos 
    397   1.1  christos 	The a.out backend handles symbols by calling
    398   1.1  christos 	<<aout_link_write_symbols>> on each input BFD and then
    399   1.1  christos 	traversing the global hash table with the function
    400   1.1  christos 	<<aout_link_write_other_symbol>>.  It builds a string table
    401   1.1  christos 	while writing out the symbols, which is written to the output
    402   1.1  christos 	file at the end of <<NAME(aout,final_link)>>.
    403   1.1  christos */
    404   1.1  christos 
    405  1.10  christos /* This structure is used to pass information to
    406  1.10  christos    _bfd_generic_link_write_global_symbol, which may be called via
    407  1.10  christos    _bfd_generic_link_hash_traverse.  */
    408  1.10  christos 
    409  1.10  christos struct generic_write_global_symbol_info
    410  1.10  christos {
    411  1.10  christos   struct bfd_link_info *info;
    412  1.10  christos   bfd *output_bfd;
    413  1.10  christos   size_t *psymalloc;
    414  1.10  christos   bool failed;
    415  1.10  christos };
    416  1.10  christos 
    417   1.8  christos static bool generic_link_add_object_symbols
    418   1.6  christos   (bfd *, struct bfd_link_info *);
    419   1.8  christos static bool generic_link_check_archive_element
    420   1.3  christos   (bfd *, struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
    421   1.8  christos    bool *);
    422   1.8  christos static bool generic_link_add_symbol_list
    423   1.6  christos   (bfd *, struct bfd_link_info *, bfd_size_type count, asymbol **);
    424   1.8  christos static bool generic_add_output_symbol
    425   1.1  christos   (bfd *, size_t *psymalloc, asymbol *);
    426   1.8  christos static bool default_data_link_order
    427   1.1  christos   (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
    428   1.8  christos static bool default_indirect_link_order
    429   1.1  christos   (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *,
    430   1.8  christos    bool);
    431  1.10  christos static bool _bfd_generic_link_output_symbols
    432  1.10  christos   (bfd *, bfd *, struct bfd_link_info *, size_t *);
    433  1.10  christos static bool _bfd_generic_link_write_global_symbol
    434  1.10  christos   (struct generic_link_hash_entry *, void *);
    435   1.1  christos 
    436   1.1  christos /* The link hash table structure is defined in bfdlink.h.  It provides
    437   1.1  christos    a base hash table which the backend specific hash tables are built
    438   1.1  christos    upon.  */
    439   1.1  christos 
    440   1.1  christos /* Routine to create an entry in the link hash table.  */
    441   1.1  christos 
    442   1.1  christos struct bfd_hash_entry *
    443   1.1  christos _bfd_link_hash_newfunc (struct bfd_hash_entry *entry,
    444   1.1  christos 			struct bfd_hash_table *table,
    445   1.1  christos 			const char *string)
    446   1.1  christos {
    447   1.1  christos   /* Allocate the structure if it has not already been allocated by a
    448   1.1  christos      subclass.  */
    449   1.1  christos   if (entry == NULL)
    450   1.1  christos     {
    451   1.1  christos       entry = (struct bfd_hash_entry *)
    452   1.6  christos 	  bfd_hash_allocate (table, sizeof (struct bfd_link_hash_entry));
    453   1.1  christos       if (entry == NULL)
    454   1.1  christos 	return entry;
    455   1.1  christos     }
    456   1.1  christos 
    457   1.1  christos   /* Call the allocation method of the superclass.  */
    458   1.1  christos   entry = bfd_hash_newfunc (entry, table, string);
    459   1.1  christos   if (entry)
    460   1.1  christos     {
    461   1.1  christos       struct bfd_link_hash_entry *h = (struct bfd_link_hash_entry *) entry;
    462   1.1  christos 
    463   1.1  christos       /* Initialize the local fields.  */
    464   1.1  christos       memset ((char *) &h->root + sizeof (h->root), 0,
    465   1.1  christos 	      sizeof (*h) - sizeof (h->root));
    466   1.1  christos     }
    467   1.1  christos 
    468   1.1  christos   return entry;
    469   1.1  christos }
    470   1.1  christos 
    471   1.1  christos /* Initialize a link hash table.  The BFD argument is the one
    472   1.1  christos    responsible for creating this table.  */
    473   1.1  christos 
    474   1.8  christos bool
    475   1.1  christos _bfd_link_hash_table_init
    476   1.1  christos   (struct bfd_link_hash_table *table,
    477   1.1  christos    bfd *abfd ATTRIBUTE_UNUSED,
    478   1.1  christos    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
    479   1.1  christos 				      struct bfd_hash_table *,
    480   1.1  christos 				      const char *),
    481   1.1  christos    unsigned int entsize)
    482   1.1  christos {
    483   1.8  christos   bool ret;
    484   1.3  christos 
    485   1.3  christos   BFD_ASSERT (!abfd->is_linker_output && !abfd->link.hash);
    486   1.1  christos   table->undefs = NULL;
    487   1.1  christos   table->undefs_tail = NULL;
    488   1.1  christos   table->type = bfd_link_generic_hash_table;
    489   1.1  christos 
    490   1.3  christos   ret = bfd_hash_table_init (&table->table, newfunc, entsize);
    491   1.3  christos   if (ret)
    492   1.3  christos     {
    493   1.3  christos       /* Arrange for destruction of this hash table on closing ABFD.  */
    494   1.3  christos       table->hash_table_free = _bfd_generic_link_hash_table_free;
    495   1.3  christos       abfd->link.hash = table;
    496   1.8  christos       abfd->is_linker_output = true;
    497   1.3  christos     }
    498   1.3  christos   return ret;
    499   1.1  christos }
    500   1.1  christos 
    501   1.1  christos /* Look up a symbol in a link hash table.  If follow is TRUE, we
    502   1.1  christos    follow bfd_link_hash_indirect and bfd_link_hash_warning links to
    503   1.7  christos    the real symbol.
    504   1.7  christos 
    505   1.7  christos .{* Return TRUE if the symbol described by a linker hash entry H
    506   1.7  christos .   is going to be absolute.  Linker-script defined symbols can be
    507   1.7  christos .   converted from absolute to section-relative ones late in the
    508   1.7  christos .   link.  Use this macro to correctly determine whether the symbol
    509   1.7  christos .   will actually end up absolute in output.  *}
    510   1.7  christos .#define bfd_is_abs_symbol(H) \
    511   1.7  christos .  (((H)->type == bfd_link_hash_defined \
    512   1.7  christos .    || (H)->type == bfd_link_hash_defweak) \
    513   1.7  christos .   && bfd_is_abs_section ((H)->u.def.section) \
    514   1.7  christos .   && !(H)->rel_from_abs)
    515   1.7  christos .
    516   1.7  christos */
    517   1.1  christos 
    518   1.1  christos struct bfd_link_hash_entry *
    519   1.1  christos bfd_link_hash_lookup (struct bfd_link_hash_table *table,
    520   1.1  christos 		      const char *string,
    521   1.8  christos 		      bool create,
    522   1.8  christos 		      bool copy,
    523   1.8  christos 		      bool follow)
    524   1.1  christos {
    525   1.1  christos   struct bfd_link_hash_entry *ret;
    526   1.1  christos 
    527   1.6  christos   if (table == NULL || string == NULL)
    528   1.6  christos     return NULL;
    529   1.6  christos 
    530   1.1  christos   ret = ((struct bfd_link_hash_entry *)
    531   1.1  christos 	 bfd_hash_lookup (&table->table, string, create, copy));
    532   1.1  christos 
    533   1.1  christos   if (follow && ret != NULL)
    534   1.1  christos     {
    535   1.1  christos       while (ret->type == bfd_link_hash_indirect
    536   1.1  christos 	     || ret->type == bfd_link_hash_warning)
    537   1.1  christos 	ret = ret->u.i.link;
    538   1.1  christos     }
    539   1.1  christos 
    540   1.1  christos   return ret;
    541   1.1  christos }
    542   1.1  christos 
    543   1.1  christos /* Look up a symbol in the main linker hash table if the symbol might
    544   1.1  christos    be wrapped.  This should only be used for references to an
    545   1.1  christos    undefined symbol, not for definitions of a symbol.  */
    546   1.1  christos 
    547   1.1  christos struct bfd_link_hash_entry *
    548   1.1  christos bfd_wrapped_link_hash_lookup (bfd *abfd,
    549   1.1  christos 			      struct bfd_link_info *info,
    550   1.1  christos 			      const char *string,
    551   1.8  christos 			      bool create,
    552   1.8  christos 			      bool copy,
    553   1.8  christos 			      bool follow)
    554   1.1  christos {
    555   1.8  christos   size_t amt;
    556   1.1  christos 
    557   1.1  christos   if (info->wrap_hash != NULL)
    558   1.1  christos     {
    559   1.1  christos       const char *l;
    560   1.1  christos       char prefix = '\0';
    561   1.1  christos 
    562   1.1  christos       l = string;
    563   1.9  christos       if (*l
    564   1.9  christos 	  && (*l == bfd_get_symbol_leading_char (abfd)
    565   1.9  christos 	      || *l == info->wrap_char))
    566   1.1  christos 	{
    567   1.1  christos 	  prefix = *l;
    568   1.1  christos 	  ++l;
    569   1.1  christos 	}
    570   1.1  christos 
    571   1.1  christos #undef WRAP
    572   1.1  christos #define WRAP "__wrap_"
    573   1.1  christos 
    574   1.8  christos       if (bfd_hash_lookup (info->wrap_hash, l, false, false) != NULL)
    575   1.1  christos 	{
    576   1.1  christos 	  char *n;
    577   1.1  christos 	  struct bfd_link_hash_entry *h;
    578   1.1  christos 
    579   1.1  christos 	  /* This symbol is being wrapped.  We want to replace all
    580   1.6  christos 	     references to SYM with references to __wrap_SYM.  */
    581   1.1  christos 
    582   1.1  christos 	  amt = strlen (l) + sizeof WRAP + 1;
    583   1.1  christos 	  n = (char *) bfd_malloc (amt);
    584   1.1  christos 	  if (n == NULL)
    585   1.1  christos 	    return NULL;
    586   1.1  christos 
    587   1.1  christos 	  n[0] = prefix;
    588   1.1  christos 	  n[1] = '\0';
    589   1.1  christos 	  strcat (n, WRAP);
    590   1.1  christos 	  strcat (n, l);
    591   1.8  christos 	  h = bfd_link_hash_lookup (info->hash, n, create, true, follow);
    592  1.10  christos 	  if (h != NULL)
    593  1.10  christos 	    h->wrapper_symbol = true;
    594   1.1  christos 	  free (n);
    595   1.1  christos 	  return h;
    596   1.1  christos 	}
    597   1.1  christos 
    598   1.1  christos #undef  REAL
    599   1.1  christos #define REAL "__real_"
    600   1.1  christos 
    601   1.1  christos       if (*l == '_'
    602   1.8  christos 	  && startswith (l, REAL)
    603   1.1  christos 	  && bfd_hash_lookup (info->wrap_hash, l + sizeof REAL - 1,
    604   1.8  christos 			      false, false) != NULL)
    605   1.1  christos 	{
    606   1.1  christos 	  char *n;
    607   1.1  christos 	  struct bfd_link_hash_entry *h;
    608   1.1  christos 
    609   1.1  christos 	  /* This is a reference to __real_SYM, where SYM is being
    610   1.6  christos 	     wrapped.  We want to replace all references to __real_SYM
    611   1.6  christos 	     with references to SYM.  */
    612   1.1  christos 
    613   1.1  christos 	  amt = strlen (l + sizeof REAL - 1) + 2;
    614   1.1  christos 	  n = (char *) bfd_malloc (amt);
    615   1.1  christos 	  if (n == NULL)
    616   1.1  christos 	    return NULL;
    617   1.1  christos 
    618   1.1  christos 	  n[0] = prefix;
    619   1.1  christos 	  n[1] = '\0';
    620   1.1  christos 	  strcat (n, l + sizeof REAL - 1);
    621   1.8  christos 	  h = bfd_link_hash_lookup (info->hash, n, create, true, follow);
    622   1.8  christos 	  if (h != NULL)
    623   1.8  christos 	    h->ref_real = 1;
    624   1.1  christos 	  free (n);
    625   1.1  christos 	  return h;
    626   1.1  christos 	}
    627   1.1  christos 
    628   1.1  christos #undef REAL
    629   1.1  christos     }
    630   1.1  christos 
    631   1.1  christos   return bfd_link_hash_lookup (info->hash, string, create, copy, follow);
    632   1.1  christos }
    633   1.1  christos 
    634   1.3  christos /* If H is a wrapped symbol, ie. the symbol name starts with "__wrap_"
    635   1.3  christos    and the remainder is found in wrap_hash, return the real symbol.  */
    636   1.3  christos 
    637   1.3  christos struct bfd_link_hash_entry *
    638   1.3  christos unwrap_hash_lookup (struct bfd_link_info *info,
    639   1.3  christos 		    bfd *input_bfd,
    640   1.3  christos 		    struct bfd_link_hash_entry *h)
    641   1.3  christos {
    642   1.3  christos   const char *l = h->root.string;
    643   1.3  christos 
    644   1.9  christos   if (*l
    645   1.9  christos       && (*l == bfd_get_symbol_leading_char (input_bfd)
    646   1.9  christos 	  || *l == info->wrap_char))
    647   1.3  christos     ++l;
    648   1.3  christos 
    649   1.8  christos   if (startswith (l, WRAP))
    650   1.3  christos     {
    651   1.3  christos       l += sizeof WRAP - 1;
    652   1.3  christos 
    653   1.8  christos       if (bfd_hash_lookup (info->wrap_hash, l, false, false) != NULL)
    654   1.3  christos 	{
    655   1.3  christos 	  char save = 0;
    656   1.3  christos 	  if (l - (sizeof WRAP - 1) != h->root.string)
    657   1.3  christos 	    {
    658   1.3  christos 	      --l;
    659   1.3  christos 	      save = *l;
    660   1.3  christos 	      *(char *) l = *h->root.string;
    661   1.3  christos 	    }
    662   1.8  christos 	  h = bfd_link_hash_lookup (info->hash, l, false, false, false);
    663   1.3  christos 	  if (save)
    664   1.3  christos 	    *(char *) l = save;
    665   1.3  christos 	}
    666   1.3  christos     }
    667   1.3  christos   return h;
    668   1.3  christos }
    669   1.3  christos #undef WRAP
    670   1.3  christos 
    671   1.1  christos /* Traverse a generic link hash table.  Differs from bfd_hash_traverse
    672   1.1  christos    in the treatment of warning symbols.  When warning symbols are
    673   1.1  christos    created they replace the real symbol, so you don't get to see the
    674   1.6  christos    real symbol in a bfd_hash_traverse.  This traversal calls func with
    675   1.1  christos    the real symbol.  */
    676   1.1  christos 
    677   1.1  christos void
    678   1.1  christos bfd_link_hash_traverse
    679   1.1  christos   (struct bfd_link_hash_table *htab,
    680   1.8  christos    bool (*func) (struct bfd_link_hash_entry *, void *),
    681   1.1  christos    void *info)
    682   1.1  christos {
    683   1.1  christos   unsigned int i;
    684   1.1  christos 
    685   1.1  christos   htab->table.frozen = 1;
    686   1.1  christos   for (i = 0; i < htab->table.size; i++)
    687   1.1  christos     {
    688   1.1  christos       struct bfd_link_hash_entry *p;
    689   1.1  christos 
    690   1.1  christos       p = (struct bfd_link_hash_entry *) htab->table.table[i];
    691   1.1  christos       for (; p != NULL; p = (struct bfd_link_hash_entry *) p->root.next)
    692   1.1  christos 	if (!(*func) (p->type == bfd_link_hash_warning ? p->u.i.link : p, info))
    693   1.1  christos 	  goto out;
    694   1.1  christos     }
    695   1.1  christos  out:
    696   1.1  christos   htab->table.frozen = 0;
    697   1.1  christos }
    698   1.1  christos 
    699   1.1  christos /* Add a symbol to the linker hash table undefs list.  */
    700   1.1  christos 
    701   1.1  christos void
    702   1.1  christos bfd_link_add_undef (struct bfd_link_hash_table *table,
    703   1.1  christos 		    struct bfd_link_hash_entry *h)
    704   1.1  christos {
    705   1.1  christos   BFD_ASSERT (h->u.undef.next == NULL);
    706   1.1  christos   if (table->undefs_tail != NULL)
    707   1.1  christos     table->undefs_tail->u.undef.next = h;
    708   1.1  christos   if (table->undefs == NULL)
    709   1.1  christos     table->undefs = h;
    710   1.1  christos   table->undefs_tail = h;
    711   1.1  christos }
    712   1.1  christos 
    713   1.1  christos /* The undefs list was designed so that in normal use we don't need to
    714   1.1  christos    remove entries.  However, if symbols on the list are changed from
    715   1.1  christos    bfd_link_hash_undefined to either bfd_link_hash_undefweak or
    716   1.1  christos    bfd_link_hash_new for some reason, then they must be removed from the
    717   1.1  christos    list.  Failure to do so might result in the linker attempting to add
    718   1.1  christos    the symbol to the list again at a later stage.  */
    719   1.1  christos 
    720   1.1  christos void
    721   1.1  christos bfd_link_repair_undef_list (struct bfd_link_hash_table *table)
    722   1.1  christos {
    723   1.1  christos   struct bfd_link_hash_entry **pun;
    724   1.1  christos 
    725   1.1  christos   pun = &table->undefs;
    726   1.1  christos   while (*pun != NULL)
    727   1.1  christos     {
    728   1.1  christos       struct bfd_link_hash_entry *h = *pun;
    729   1.1  christos 
    730   1.1  christos       if (h->type == bfd_link_hash_new
    731   1.1  christos 	  || h->type == bfd_link_hash_undefweak)
    732   1.1  christos 	{
    733   1.1  christos 	  *pun = h->u.undef.next;
    734   1.1  christos 	  h->u.undef.next = NULL;
    735   1.1  christos 	  if (h == table->undefs_tail)
    736   1.1  christos 	    {
    737   1.1  christos 	      if (pun == &table->undefs)
    738   1.1  christos 		table->undefs_tail = NULL;
    739   1.1  christos 	      else
    740   1.1  christos 		/* pun points at an u.undef.next field.  Go back to
    741   1.1  christos 		   the start of the link_hash_entry.  */
    742   1.1  christos 		table->undefs_tail = (struct bfd_link_hash_entry *)
    743   1.1  christos 		  ((char *) pun - ((char *) &h->u.undef.next - (char *) h));
    744   1.1  christos 	      break;
    745   1.1  christos 	    }
    746   1.1  christos 	}
    747   1.1  christos       else
    748   1.1  christos 	pun = &h->u.undef.next;
    749   1.1  christos     }
    750   1.1  christos }
    751   1.1  christos 
    752   1.1  christos /* Routine to create an entry in a generic link hash table.  */
    754  1.10  christos 
    755   1.1  christos static struct bfd_hash_entry *
    756   1.1  christos _bfd_generic_link_hash_newfunc (struct bfd_hash_entry *entry,
    757   1.1  christos 				struct bfd_hash_table *table,
    758   1.1  christos 				const char *string)
    759   1.1  christos {
    760   1.1  christos   /* Allocate the structure if it has not already been allocated by a
    761   1.1  christos      subclass.  */
    762   1.1  christos   if (entry == NULL)
    763   1.1  christos     {
    764   1.1  christos       entry = (struct bfd_hash_entry *)
    765   1.1  christos 	bfd_hash_allocate (table, sizeof (struct generic_link_hash_entry));
    766   1.1  christos       if (entry == NULL)
    767   1.1  christos 	return entry;
    768   1.1  christos     }
    769   1.1  christos 
    770   1.1  christos   /* Call the allocation method of the superclass.  */
    771   1.1  christos   entry = _bfd_link_hash_newfunc (entry, table, string);
    772   1.1  christos   if (entry)
    773   1.1  christos     {
    774   1.1  christos       struct generic_link_hash_entry *ret;
    775   1.1  christos 
    776   1.1  christos       /* Set local fields.  */
    777   1.8  christos       ret = (struct generic_link_hash_entry *) entry;
    778   1.1  christos       ret->written = false;
    779   1.1  christos       ret->sym = NULL;
    780   1.1  christos     }
    781   1.1  christos 
    782   1.1  christos   return entry;
    783   1.1  christos }
    784   1.1  christos 
    785   1.1  christos /* Create a generic link hash table.  */
    786   1.1  christos 
    787   1.1  christos struct bfd_link_hash_table *
    788   1.1  christos _bfd_generic_link_hash_table_create (bfd *abfd)
    789   1.1  christos {
    790   1.8  christos   struct generic_link_hash_table *ret;
    791   1.1  christos   size_t amt = sizeof (struct generic_link_hash_table);
    792   1.1  christos 
    793   1.1  christos   ret = (struct generic_link_hash_table *) bfd_malloc (amt);
    794   1.1  christos   if (ret == NULL)
    795   1.1  christos     return NULL;
    796   1.1  christos   if (! _bfd_link_hash_table_init (&ret->root, abfd,
    797   1.1  christos 				   _bfd_generic_link_hash_newfunc,
    798   1.1  christos 				   sizeof (struct generic_link_hash_entry)))
    799   1.1  christos     {
    800   1.1  christos       free (ret);
    801   1.1  christos       return NULL;
    802   1.1  christos     }
    803   1.1  christos   return &ret->root;
    804   1.1  christos }
    805   1.1  christos 
    806   1.3  christos void
    807   1.1  christos _bfd_generic_link_hash_table_free (bfd *obfd)
    808   1.3  christos {
    809   1.1  christos   struct generic_link_hash_table *ret;
    810   1.3  christos 
    811   1.3  christos   BFD_ASSERT (obfd->is_linker_output && obfd->link.hash);
    812   1.1  christos   ret = (struct generic_link_hash_table *) obfd->link.hash;
    813   1.1  christos   bfd_hash_table_free (&ret->root.table);
    814   1.3  christos   free (ret);
    815   1.8  christos   obfd->link.hash = NULL;
    816   1.1  christos   obfd->is_linker_output = false;
    817   1.1  christos }
    818   1.1  christos 
    819   1.1  christos /* Grab the symbols for an object file when doing a generic link.  We
    820   1.1  christos    store the symbols in the outsymbols field.  We need to keep them
    821   1.1  christos    around for the entire link to ensure that we only read them once.
    822   1.1  christos    If we read them multiple times, we might wind up with relocs and
    823   1.1  christos    the hash table pointing to different instances of the symbol
    824   1.1  christos    structure.  */
    825   1.8  christos 
    826   1.1  christos bool
    827   1.1  christos bfd_generic_link_read_symbols (bfd *abfd)
    828   1.1  christos {
    829   1.1  christos   if (bfd_get_outsymbols (abfd) == NULL)
    830   1.1  christos     {
    831   1.1  christos       long symsize;
    832   1.1  christos       long symcount;
    833   1.1  christos 
    834   1.1  christos       symsize = bfd_get_symtab_upper_bound (abfd);
    835   1.8  christos       if (symsize < 0)
    836   1.7  christos 	return false;
    837   1.1  christos       abfd->outsymbols = bfd_alloc (abfd, symsize);
    838   1.8  christos       if (bfd_get_outsymbols (abfd) == NULL && symsize != 0)
    839   1.1  christos 	return false;
    840   1.1  christos       symcount = bfd_canonicalize_symtab (abfd, bfd_get_outsymbols (abfd));
    841   1.8  christos       if (symcount < 0)
    842   1.7  christos 	return false;
    843   1.1  christos       abfd->symcount = symcount;
    844   1.1  christos     }
    845   1.8  christos 
    846   1.1  christos   return true;
    847   1.1  christos }
    848   1.1  christos 
    849   1.1  christos /* Indicate that we are only retrieving symbol values from this
    851   1.1  christos    section.  We want the symbols to act as though the values in the
    852   1.1  christos    file are absolute.  */
    853   1.1  christos 
    854   1.1  christos void
    855   1.1  christos _bfd_generic_link_just_syms (asection *sec,
    856   1.1  christos 			     struct bfd_link_info *info ATTRIBUTE_UNUSED)
    857   1.1  christos {
    858   1.1  christos   sec->sec_info_type = SEC_INFO_TYPE_JUST_SYMS;
    859   1.1  christos   sec->output_section = bfd_abs_section_ptr;
    860   1.1  christos   sec->output_offset = sec->vma;
    861   1.3  christos }
    862   1.3  christos 
    863   1.1  christos /* Copy the symbol type and other attributes for a linker script
    864   1.1  christos    assignment from HSRC to HDEST.
    865   1.1  christos    The default implementation does nothing.  */
    866   1.3  christos void
    867   1.3  christos _bfd_generic_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
    868   1.1  christos     struct bfd_link_hash_entry *hdest ATTRIBUTE_UNUSED,
    869   1.1  christos     struct bfd_link_hash_entry *hsrc ATTRIBUTE_UNUSED)
    870   1.1  christos {
    871   1.6  christos }
    872   1.6  christos 
    873   1.1  christos /* Generic function to add symbols from an object file to the
    874   1.8  christos    global hash table.  */
    875   1.6  christos 
    876   1.1  christos bool
    877   1.8  christos _bfd_generic_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
    878   1.1  christos {
    879   1.1  christos   bool ret;
    880   1.1  christos 
    881   1.1  christos   switch (bfd_get_format (abfd))
    882   1.6  christos     {
    883   1.1  christos     case bfd_object:
    884   1.1  christos       ret = generic_link_add_object_symbols (abfd, info);
    885   1.1  christos       break;
    886   1.6  christos     case bfd_archive:
    887   1.1  christos       ret = (_bfd_generic_link_add_archive_symbols
    888   1.1  christos 	     (abfd, info, generic_link_check_archive_element));
    889   1.1  christos       break;
    890   1.8  christos     default:
    891   1.1  christos       bfd_set_error (bfd_error_wrong_format);
    892   1.1  christos       ret = false;
    893   1.1  christos     }
    894   1.1  christos 
    895   1.1  christos   return ret;
    896   1.1  christos }
    897   1.1  christos 
    898   1.8  christos /* Add symbols from an object file to the global hash table.  */
    899   1.1  christos 
    900   1.6  christos static bool
    901   1.1  christos generic_link_add_object_symbols (bfd *abfd,
    902   1.1  christos 				 struct bfd_link_info *info)
    903   1.1  christos {
    904   1.1  christos   bfd_size_type symcount;
    905   1.1  christos   struct bfd_symbol **outsyms;
    906   1.8  christos 
    907   1.1  christos   if (!bfd_generic_link_read_symbols (abfd))
    908   1.1  christos     return false;
    909   1.6  christos   symcount = _bfd_generic_link_get_symcount (abfd);
    910   1.1  christos   outsyms = _bfd_generic_link_get_symbols (abfd);
    911   1.1  christos   return generic_link_add_symbol_list (abfd, info, symcount, outsyms);
    912   1.1  christos }
    913   1.1  christos 
    914   1.1  christos /* Generic function to add symbols from an archive file to the global
    916   1.3  christos    hash file.  This function presumes that the archive symbol table
    917   1.3  christos    has already been read in (this is normally done by the
    918   1.3  christos    bfd_check_format entry point).  It looks through the archive symbol
    919   1.3  christos    table for symbols that are undefined or common in the linker global
    920   1.3  christos    symbol hash table.  When one is found, the CHECKFN argument is used
    921   1.3  christos    to see if an object file should be included.  This allows targets
    922   1.3  christos    to customize common symbol behaviour.  CHECKFN should set *PNEEDED
    923   1.3  christos    to TRUE if the object file should be included, and must also call
    924   1.3  christos    the bfd_link_info add_archive_element callback function and handle
    925   1.3  christos    adding the symbols to the global hash table.  CHECKFN must notice
    926   1.1  christos    if the callback indicates a substitute BFD, and arrange to add
    927   1.8  christos    those symbols instead if it does so.  CHECKFN should only return
    928   1.1  christos    FALSE if some sort of error occurs.  */
    929   1.1  christos 
    930   1.1  christos bool
    931   1.8  christos _bfd_generic_link_add_archive_symbols
    932   1.8  christos   (bfd *abfd,
    933   1.1  christos    struct bfd_link_info *info,
    934   1.8  christos    bool (*checkfn) (bfd *, struct bfd_link_info *,
    935   1.3  christos 		    struct bfd_link_hash_entry *, const char *, bool *))
    936   1.3  christos {
    937   1.1  christos   bool loop;
    938   1.1  christos   bfd_size_type amt;
    939   1.1  christos   unsigned char *included;
    940   1.1  christos 
    941   1.1  christos   if (! bfd_has_map (abfd))
    942   1.8  christos     {
    943   1.1  christos       /* An empty archive is a special case.  */
    944   1.8  christos       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
    945   1.1  christos 	return true;
    946   1.1  christos       bfd_set_error (bfd_error_no_armap);
    947   1.3  christos       return false;
    948   1.3  christos     }
    949   1.8  christos 
    950   1.3  christos   amt = bfd_ardata (abfd)->symdef_count;
    951   1.3  christos   if (amt == 0)
    952   1.3  christos     return true;
    953   1.8  christos   amt *= sizeof (*included);
    954   1.1  christos   included = (unsigned char *) bfd_zmalloc (amt);
    955   1.3  christos   if (included == NULL)
    956   1.1  christos     return false;
    957   1.3  christos 
    958   1.3  christos   do
    959   1.3  christos     {
    960   1.3  christos       carsym *arsyms;
    961   1.3  christos       carsym *arsym_end;
    962   1.8  christos       carsym *arsym;
    963   1.3  christos       unsigned int indx;
    964   1.3  christos       file_ptr last_ar_offset = -1;
    965   1.8  christos       bool needed = false;
    966   1.3  christos       bfd *element = NULL;
    967   1.3  christos 
    968   1.3  christos       loop = false;
    969   1.1  christos       arsyms = bfd_ardata (abfd)->symdefs;
    970   1.3  christos       arsym_end = arsyms + bfd_ardata (abfd)->symdef_count;
    971   1.3  christos       for (arsym = arsyms, indx = 0; arsym < arsym_end; arsym++, indx++)
    972   1.1  christos 	{
    973   1.3  christos 	  struct bfd_link_hash_entry *h;
    974   1.3  christos 	  struct bfd_link_hash_entry *undefs_tail;
    975   1.3  christos 
    976   1.1  christos 	  if (included[indx])
    977   1.3  christos 	    continue;
    978   1.1  christos 	  if (needed && arsym->file_offset == last_ar_offset)
    979   1.1  christos 	    {
    980   1.1  christos 	      included[indx] = 1;
    981   1.6  christos 	      continue;
    982   1.6  christos 	    }
    983   1.7  christos 
    984   1.3  christos 	  if (arsym->name == NULL)
    985   1.8  christos 	    goto error_return;
    986   1.1  christos 
    987   1.3  christos 	  h = bfd_link_hash_lookup (info->hash, arsym->name,
    988   1.3  christos 				    false, false, true);
    989   1.8  christos 
    990   1.3  christos 	  if (h == NULL
    991   1.8  christos 	      && info->pei386_auto_import
    992   1.3  christos 	      && startswith (arsym->name, "__imp_"))
    993   1.1  christos 	    h = bfd_link_hash_lookup (info->hash, arsym->name + 6,
    994   1.1  christos 				      false, false, true);
    995   1.3  christos 	  if (h == NULL)
    996   1.3  christos 	    continue;
    997   1.1  christos 
    998   1.3  christos 	  if (h->type != bfd_link_hash_undefined
    999   1.3  christos 	      && h->type != bfd_link_hash_common)
   1000   1.3  christos 	    {
   1001   1.1  christos 	      if (h->type != bfd_link_hash_undefweak)
   1002   1.1  christos 		/* Symbol must be defined.  Don't check it again.  */
   1003   1.1  christos 		included[indx] = 1;
   1004   1.3  christos 	      continue;
   1005   1.3  christos 	    }
   1006   1.3  christos 
   1007   1.8  christos 	  if (last_ar_offset != arsym->file_offset)
   1008   1.8  christos 	    {
   1009   1.3  christos 	      last_ar_offset = arsym->file_offset;
   1010   1.3  christos 	      element = _bfd_get_elt_at_filepos (abfd, last_ar_offset,
   1011   1.3  christos 						 info);
   1012   1.3  christos 	      if (element == NULL
   1013   1.3  christos 		  || !bfd_check_format (element, bfd_object))
   1014   1.3  christos 		goto error_return;
   1015   1.3  christos 	    }
   1016   1.1  christos 
   1017   1.1  christos 	  undefs_tail = info->hash->undefs_tail;
   1018   1.3  christos 
   1019   1.1  christos 	  /* CHECKFN will see if this element should be included, and
   1020   1.1  christos 	     go ahead and include it if appropriate.  */
   1021   1.3  christos 	  if (! (*checkfn) (element, info, h, arsym->name, &needed))
   1022   1.1  christos 	    goto error_return;
   1023   1.3  christos 
   1024   1.1  christos 	  if (needed)
   1025   1.3  christos 	    {
   1026   1.3  christos 	      unsigned int mark;
   1027   1.3  christos 
   1028   1.3  christos 	      /* Look backward to mark all symbols from this object file
   1029   1.3  christos 		 which we have already seen in this pass.  */
   1030   1.3  christos 	      mark = indx;
   1031   1.3  christos 	      do
   1032   1.3  christos 		{
   1033   1.3  christos 		  included[mark] = 1;
   1034   1.3  christos 		  if (mark == 0)
   1035   1.3  christos 		    break;
   1036   1.3  christos 		  --mark;
   1037   1.3  christos 		}
   1038   1.8  christos 	      while (arsyms[mark].file_offset == last_ar_offset);
   1039   1.1  christos 
   1040   1.1  christos 	      if (undefs_tail != info->hash->undefs_tail)
   1041   1.3  christos 		loop = true;
   1042   1.1  christos 	    }
   1043   1.3  christos 	}
   1044   1.8  christos     } while (loop);
   1045   1.1  christos 
   1046   1.1  christos   free (included);
   1047   1.3  christos   return true;
   1048   1.8  christos 
   1049   1.1  christos  error_return:
   1050   1.1  christos   free (included);
   1051   1.6  christos   return false;
   1052   1.1  christos }
   1053   1.8  christos 
   1054   1.1  christos /* See if we should include an archive element.  */
   1056   1.3  christos 
   1057   1.3  christos static bool
   1058   1.8  christos generic_link_check_archive_element (bfd *abfd,
   1059   1.1  christos 				    struct bfd_link_info *info,
   1060   1.1  christos 				    struct bfd_link_hash_entry *h,
   1061   1.1  christos 				    const char *name ATTRIBUTE_UNUSED,
   1062   1.8  christos 				    bool *pneeded)
   1063   1.1  christos {
   1064   1.1  christos   asymbol **pp, **ppend;
   1065   1.8  christos 
   1066   1.1  christos   *pneeded = false;
   1067   1.1  christos 
   1068   1.1  christos   if (!bfd_generic_link_read_symbols (abfd))
   1069   1.1  christos     return false;
   1070   1.1  christos 
   1071   1.1  christos   pp = _bfd_generic_link_get_symbols (abfd);
   1072   1.1  christos   ppend = pp + _bfd_generic_link_get_symcount (abfd);
   1073   1.1  christos   for (; pp < ppend; pp++)
   1074   1.1  christos     {
   1075   1.1  christos       asymbol *p;
   1076   1.1  christos 
   1077   1.1  christos       p = *pp;
   1078   1.1  christos 
   1079   1.1  christos       /* We are only interested in globally visible symbols.  */
   1080   1.1  christos       if (! bfd_is_com_section (p->section)
   1081   1.1  christos 	  && (p->flags & (BSF_GLOBAL | BSF_INDIRECT | BSF_WEAK)) == 0)
   1082   1.1  christos 	continue;
   1083   1.1  christos 
   1084   1.1  christos       /* We are only interested if we know something about this
   1085   1.8  christos 	 symbol, and it is undefined or common.  An undefined weak
   1086   1.8  christos 	 symbol (type bfd_link_hash_undefweak) is not considered to be
   1087   1.1  christos 	 a reference when pulling files out of an archive.  See the
   1088   1.1  christos 	 SVR4 ABI, p. 4-27.  */
   1089   1.1  christos       h = bfd_link_hash_lookup (info->hash, bfd_asymbol_name (p), false,
   1090   1.1  christos 				false, true);
   1091   1.1  christos       if (h == NULL
   1092   1.1  christos 	  || (h->type != bfd_link_hash_undefined
   1093   1.1  christos 	      && h->type != bfd_link_hash_common))
   1094   1.3  christos 	continue;
   1095   1.3  christos 
   1096   1.3  christos       /* P is a symbol we are looking for.  */
   1097   1.1  christos 
   1098   1.3  christos       if (! bfd_is_com_section (p->section)
   1099   1.3  christos 	  || (h->type == bfd_link_hash_undefined
   1100   1.3  christos 	      && h->u.undef.abfd == NULL))
   1101   1.8  christos 	{
   1102   1.1  christos 	  /* P is not a common symbol, or an undefined reference was
   1103   1.1  christos 	     created from outside BFD such as from a linker -u option.
   1104   1.1  christos 	     This object file defines the symbol, so pull it in.  */
   1105   1.8  christos 	  *pneeded = true;
   1106   1.1  christos 	  if (!(*info->callbacks
   1107   1.1  christos 		->add_archive_element) (info, abfd, bfd_asymbol_name (p),
   1108   1.6  christos 					&abfd))
   1109   1.1  christos 	    return false;
   1110   1.1  christos 	  /* Potentially, the add_archive_element hook may have set a
   1111   1.1  christos 	     substitute BFD for us.  */
   1112   1.1  christos 	  return bfd_link_add_symbols (abfd, info);
   1113   1.1  christos 	}
   1114   1.1  christos 
   1115   1.1  christos       /* P is a common symbol.  */
   1116   1.1  christos 
   1117   1.1  christos       if (h->type == bfd_link_hash_undefined)
   1118   1.1  christos 	{
   1119   1.1  christos 	  bfd *symbfd;
   1120   1.1  christos 	  bfd_vma size;
   1121   1.1  christos 	  unsigned int power;
   1122   1.1  christos 
   1123   1.1  christos 	  /* Turn the symbol into a common symbol but do not link in
   1124   1.1  christos 	     the object file.  This is how a.out works.  Object
   1125   1.1  christos 	     formats that require different semantics must implement
   1126   1.3  christos 	     this function differently.  This symbol is already on the
   1127   1.1  christos 	     undefs list.  We add the section to a common section
   1128   1.1  christos 	     attached to symbfd to ensure that it is in a BFD which
   1129   1.1  christos 	     will be linked in.  */
   1130   1.1  christos 	  symbfd = h->u.undef.abfd;
   1131   1.1  christos 	  h->type = bfd_link_hash_common;
   1132   1.8  christos 	  h->u.c.p = (struct bfd_link_hash_common_entry *)
   1133   1.1  christos 	    bfd_hash_allocate (&info->hash->table,
   1134   1.1  christos 			       sizeof (struct bfd_link_hash_common_entry));
   1135   1.1  christos 	  if (h->u.c.p == NULL)
   1136   1.1  christos 	    return false;
   1137   1.1  christos 
   1138   1.1  christos 	  size = bfd_asymbol_value (p);
   1139   1.1  christos 	  h->u.c.size = size;
   1140   1.1  christos 
   1141   1.1  christos 	  power = bfd_log2 (size);
   1142   1.1  christos 	  if (power > 4)
   1143   1.1  christos 	    power = 4;
   1144   1.1  christos 	  h->u.c.p->alignment_power = power;
   1145   1.1  christos 
   1146   1.1  christos 	  if (p->section == bfd_com_section_ptr)
   1147   1.1  christos 	    h->u.c.p->section = bfd_make_section_old_way (symbfd, "COMMON");
   1148   1.1  christos 	  else
   1149   1.1  christos 	    h->u.c.p->section = bfd_make_section_old_way (symbfd,
   1150   1.1  christos 							  p->section->name);
   1151   1.1  christos 	  h->u.c.p->section->flags |= SEC_ALLOC;
   1152   1.1  christos 	}
   1153   1.1  christos       else
   1154   1.1  christos 	{
   1155   1.1  christos 	  /* Adjust the size of the common symbol if necessary.  This
   1156   1.1  christos 	     is how a.out works.  Object formats that require
   1157   1.1  christos 	     different semantics must implement this function
   1158   1.1  christos 	     differently.  */
   1159   1.1  christos 	  if (bfd_asymbol_value (p) > h->u.c.size)
   1160   1.1  christos 	    h->u.c.size = bfd_asymbol_value (p);
   1161   1.8  christos 	}
   1162   1.1  christos     }
   1163   1.1  christos 
   1164   1.1  christos   /* This archive element is not needed.  */
   1165   1.1  christos   return true;
   1166   1.6  christos }
   1167   1.1  christos 
   1168   1.8  christos /* Add the symbols from an object file to the global hash table.  ABFD
   1169   1.1  christos    is the object file.  INFO is the linker information.  SYMBOL_COUNT
   1170   1.1  christos    is the number of symbols.  SYMBOLS is the list of symbols.  */
   1171   1.1  christos 
   1172   1.6  christos static bool
   1173   1.1  christos generic_link_add_symbol_list (bfd *abfd,
   1174   1.1  christos 			      struct bfd_link_info *info,
   1175   1.1  christos 			      bfd_size_type symbol_count,
   1176   1.1  christos 			      asymbol **symbols)
   1177   1.1  christos {
   1178   1.1  christos   asymbol **pp, **ppend;
   1179   1.1  christos 
   1180   1.1  christos   pp = symbols;
   1181   1.1  christos   ppend = symbols + symbol_count;
   1182   1.1  christos   for (; pp < ppend; pp++)
   1183   1.1  christos     {
   1184   1.1  christos       asymbol *p;
   1185   1.1  christos 
   1186   1.1  christos       p = *pp;
   1187   1.1  christos 
   1188   1.1  christos       if ((p->flags & (BSF_INDIRECT
   1189   1.7  christos 		       | BSF_WARNING
   1190   1.7  christos 		       | BSF_GLOBAL
   1191   1.7  christos 		       | BSF_CONSTRUCTOR
   1192   1.1  christos 		       | BSF_WEAK)) != 0
   1193   1.1  christos 	  || bfd_is_und_section (bfd_asymbol_section (p))
   1194   1.1  christos 	  || bfd_is_com_section (bfd_asymbol_section (p))
   1195   1.1  christos 	  || bfd_is_ind_section (bfd_asymbol_section (p)))
   1196   1.1  christos 	{
   1197   1.1  christos 	  const char *name;
   1198   1.1  christos 	  const char *string;
   1199   1.1  christos 	  struct generic_link_hash_entry *h;
   1200   1.1  christos 	  struct bfd_link_hash_entry *bh;
   1201   1.1  christos 
   1202   1.1  christos 	  string = name = bfd_asymbol_name (p);
   1203   1.1  christos 	  if (((p->flags & BSF_INDIRECT) != 0
   1204   1.1  christos 	       || bfd_is_ind_section (p->section))
   1205   1.1  christos 	      && pp + 1 < ppend)
   1206   1.1  christos 	    {
   1207   1.1  christos 	      pp++;
   1208   1.1  christos 	      string = bfd_asymbol_name (*pp);
   1209   1.1  christos 	    }
   1210   1.1  christos 	  else if ((p->flags & BSF_WARNING) != 0
   1211   1.1  christos 		   && pp + 1 < ppend)
   1212   1.1  christos 	    {
   1213   1.1  christos 	      /* The name of P is actually the warning string, and the
   1214   1.1  christos 		 next symbol is the one to warn about.  */
   1215   1.1  christos 	      pp++;
   1216   1.1  christos 	      name = bfd_asymbol_name (*pp);
   1217   1.7  christos 	    }
   1218   1.8  christos 
   1219   1.8  christos 	  bh = NULL;
   1220   1.1  christos 	  if (! (_bfd_generic_link_add_one_symbol
   1221   1.1  christos 		 (info, abfd, name, p->flags, bfd_asymbol_section (p),
   1222   1.1  christos 		  p->value, string, false, false, &bh)))
   1223   1.6  christos 	    return false;
   1224   1.6  christos 	  h = (struct generic_link_hash_entry *) bh;
   1225   1.6  christos 
   1226   1.1  christos 	  /* If this is a constructor symbol, and the linker didn't do
   1227   1.1  christos 	     anything with it, then we want to just pass the symbol
   1228   1.1  christos 	     through to the output file.  This will happen when
   1229   1.1  christos 	     linking with -r.  */
   1230   1.1  christos 	  if ((p->flags & BSF_CONSTRUCTOR) != 0
   1231   1.1  christos 	      && (h == NULL || h->root.type == bfd_link_hash_new))
   1232   1.1  christos 	    {
   1233   1.1  christos 	      p->udata.p = NULL;
   1234   1.1  christos 	      continue;
   1235   1.1  christos 	    }
   1236   1.1  christos 
   1237   1.1  christos 	  /* Save the BFD symbol so that we don't lose any backend
   1238   1.1  christos 	     specific information that may be attached to it.  We only
   1239   1.1  christos 	     want this one if it gives more information than the
   1240   1.1  christos 	     existing one; we don't want to replace a defined symbol
   1241   1.1  christos 	     with an undefined one.  This routine may be called with a
   1242   1.1  christos 	     hash table other than the generic hash table, so we only
   1243   1.1  christos 	     do this if we are certain that the hash table is a
   1244   1.7  christos 	     generic one.  */
   1245   1.7  christos 	  if (info->output_bfd->xvec == abfd->xvec)
   1246   1.7  christos 	    {
   1247   1.1  christos 	      if (h->sym == NULL
   1248   1.1  christos 		  || (! bfd_is_und_section (bfd_asymbol_section (p))
   1249   1.1  christos 		      && (! bfd_is_com_section (bfd_asymbol_section (p))
   1250   1.1  christos 			  || bfd_is_und_section (bfd_asymbol_section (h->sym)))))
   1251   1.1  christos 		{
   1252   1.7  christos 		  h->sym = p;
   1253   1.1  christos 		  /* BSF_OLD_COMMON is a hack to support COFF reloc
   1254   1.1  christos 		     reading, and it should go away when the COFF
   1255   1.1  christos 		     linker is switched to the new version.  */
   1256   1.1  christos 		  if (bfd_is_com_section (bfd_asymbol_section (p)))
   1257   1.1  christos 		    p->flags |= BSF_OLD_COMMON;
   1258   1.1  christos 		}
   1259   1.1  christos 	    }
   1260   1.1  christos 
   1261   1.1  christos 	  /* Store a back pointer from the symbol to the hash
   1262   1.1  christos 	     table entry for the benefit of relaxation code until
   1263   1.1  christos 	     it gets rewritten to not use asymbol structures.
   1264   1.1  christos 	     Setting this is also used to check whether these
   1265   1.1  christos 	     symbols were set up by the generic linker.  */
   1266   1.8  christos 	  p->udata.p = h;
   1267   1.1  christos 	}
   1268   1.1  christos     }
   1269   1.1  christos 
   1270   1.1  christos   return true;
   1271   1.1  christos }
   1272   1.1  christos 
   1273   1.1  christos /* We use a state table to deal with adding symbols from an object
   1275   1.1  christos    file.  The first index into the state table describes the symbol
   1276   1.1  christos    from the object file.  The second index into the state table is the
   1277   1.1  christos    type of the symbol in the hash table.  */
   1278   1.1  christos 
   1279   1.1  christos /* The symbol from the object file is turned into one of these row
   1280   1.1  christos    values.  */
   1281   1.1  christos 
   1282   1.1  christos enum link_row
   1283   1.1  christos {
   1284   1.1  christos   UNDEF_ROW,		/* Undefined.  */
   1285   1.1  christos   UNDEFW_ROW,		/* Weak undefined.  */
   1286   1.1  christos   DEF_ROW,		/* Defined.  */
   1287   1.1  christos   DEFW_ROW,		/* Weak defined.  */
   1288   1.1  christos   COMMON_ROW,		/* Common.  */
   1289   1.1  christos   INDR_ROW,		/* Indirect.  */
   1290   1.1  christos   WARN_ROW,		/* Warning.  */
   1291   1.1  christos   SET_ROW		/* Member of set.  */
   1292   1.1  christos };
   1293   1.1  christos 
   1294   1.1  christos /* apparently needed for Hitachi 3050R(HI-UX/WE2)? */
   1295   1.1  christos #undef FAIL
   1296   1.1  christos 
   1297   1.1  christos /* The actions to take in the state table.  */
   1298   1.1  christos 
   1299   1.1  christos enum link_action
   1300   1.1  christos {
   1301   1.1  christos   FAIL,		/* Abort.  */
   1302   1.1  christos   UND,		/* Mark symbol undefined.  */
   1303   1.1  christos   WEAK,		/* Mark symbol weak undefined.  */
   1304   1.1  christos   DEF,		/* Mark symbol defined.  */
   1305   1.1  christos   DEFW,		/* Mark symbol weak defined.  */
   1306   1.1  christos   COM,		/* Mark symbol common.  */
   1307   1.1  christos   REF,		/* Mark defined symbol referenced.  */
   1308   1.1  christos   CREF,		/* Possibly warn about common reference to defined symbol.  */
   1309   1.1  christos   CDEF,		/* Define existing common symbol.  */
   1310   1.1  christos   NOACT,	/* No action.  */
   1311   1.1  christos   BIG,		/* Mark symbol common using largest size.  */
   1312   1.1  christos   MDEF,		/* Multiple definition error.  */
   1313   1.3  christos   MIND,		/* Multiple indirect symbols.  */
   1314   1.1  christos   IND,		/* Make indirect symbol.  */
   1315   1.1  christos   CIND,		/* Make indirect symbol from existing common symbol.  */
   1316   1.1  christos   SET,		/* Add value to set.  */
   1317   1.1  christos   MWARN,	/* Make warning symbol.  */
   1318   1.1  christos   WARN,		/* Warn if referenced, else MWARN.  */
   1319   1.1  christos   CYCLE,	/* Repeat with symbol pointed to.  */
   1320   1.1  christos   REFC,		/* Mark indirect symbol referenced and then CYCLE.  */
   1321   1.1  christos   WARNC		/* Issue warning and then CYCLE.  */
   1322   1.1  christos };
   1323   1.1  christos 
   1324   1.1  christos /* The state table itself.  The first index is a link_row and the
   1325   1.6  christos    second index is a bfd_link_hash_type.  */
   1326   1.1  christos 
   1327   1.8  christos static const enum link_action link_action[8][8] =
   1328   1.6  christos {
   1329   1.1  christos   /* current\prev    new    undef  undefw def    defw   com    indr   warn  */
   1330   1.1  christos   /* UNDEF_ROW	*/  {UND,   NOACT, UND,   REF,   REF,   NOACT, REFC,  WARNC },
   1331   1.3  christos   /* UNDEFW_ROW	*/  {WEAK,  NOACT, NOACT, REF,   REF,   NOACT, REFC,  WARNC },
   1332   1.1  christos   /* DEF_ROW	*/  {DEF,   DEF,   DEF,   MDEF,  DEF,   CDEF,  MIND,  CYCLE },
   1333   1.1  christos   /* DEFW_ROW	*/  {DEFW,  DEFW,  DEFW,  NOACT, NOACT, NOACT, NOACT, CYCLE },
   1334   1.1  christos   /* COMMON_ROW	*/  {COM,   COM,   COM,   CREF,  COM,   BIG,   REFC,  WARNC },
   1335   1.1  christos   /* INDR_ROW	*/  {IND,   IND,   IND,   MDEF,  IND,   CIND,  MIND,  CYCLE },
   1336   1.1  christos   /* WARN_ROW   */  {MWARN, WARN,  WARN,  WARN,  WARN,  WARN,  WARN,  NOACT },
   1337   1.1  christos   /* SET_ROW	*/  {SET,   SET,   SET,   SET,   SET,   SET,   CYCLE, CYCLE }
   1338   1.1  christos };
   1339   1.1  christos 
   1340   1.1  christos /* Most of the entries in the LINK_ACTION table are straightforward,
   1341   1.1  christos    but a few are somewhat subtle.
   1342   1.1  christos 
   1343   1.1  christos    A reference to an indirect symbol (UNDEF_ROW/indr or
   1344   1.1  christos    UNDEFW_ROW/indr) is counted as a reference both to the indirect
   1345   1.1  christos    symbol and to the symbol the indirect symbol points to.
   1346   1.1  christos 
   1347   1.1  christos    A reference to a warning symbol (UNDEF_ROW/warn or UNDEFW_ROW/warn)
   1348   1.1  christos    causes the warning to be issued.
   1349   1.1  christos 
   1350   1.1  christos    A common definition of an indirect symbol (COMMON_ROW/indr) is
   1351   1.1  christos    treated as a multiple definition error.  Likewise for an indirect
   1352   1.1  christos    definition of a common symbol (INDR_ROW/com).
   1353   1.1  christos 
   1354   1.1  christos    An indirect definition of a warning (INDR_ROW/warn) does not cause
   1355   1.1  christos    the warning to be issued.
   1356   1.1  christos 
   1357   1.1  christos    If a warning is created for an indirect symbol (WARN_ROW/indr) no
   1358   1.1  christos    warning is created for the symbol the indirect symbol points to.
   1359   1.1  christos 
   1360   1.1  christos    Adding an entry to a set does not count as a reference to a set,
   1361   1.1  christos    and no warning is issued (SET_ROW/warn).  */
   1362   1.1  christos 
   1363   1.1  christos /* Return the BFD in which a hash entry has been defined, if known.  */
   1364   1.1  christos 
   1365   1.1  christos static bfd *
   1366   1.1  christos hash_entry_bfd (struct bfd_link_hash_entry *h)
   1367   1.1  christos {
   1368   1.1  christos   while (h->type == bfd_link_hash_warning)
   1369   1.1  christos     h = h->u.i.link;
   1370   1.1  christos   switch (h->type)
   1371   1.1  christos     {
   1372   1.1  christos     default:
   1373   1.1  christos       return NULL;
   1374   1.1  christos     case bfd_link_hash_undefined:
   1375   1.1  christos     case bfd_link_hash_undefweak:
   1376   1.1  christos       return h->u.undef.abfd;
   1377   1.1  christos     case bfd_link_hash_defined:
   1378   1.1  christos     case bfd_link_hash_defweak:
   1379   1.1  christos       return h->u.def.section->owner;
   1380   1.1  christos     case bfd_link_hash_common:
   1381  1.10  christos       return h->u.c.p->section->owner;
   1382  1.10  christos     }
   1383  1.10  christos   /*NOTREACHED*/
   1384  1.10  christos }
   1385  1.10  christos 
   1386  1.10  christos /*
   1387  1.10  christos FUNCTION
   1388  1.10  christos 	_bfd_generic_link_add_one_symbol
   1389  1.10  christos 
   1390  1.10  christos SYNOPSIS
   1391  1.10  christos 	bool _bfd_generic_link_add_one_symbol
   1392  1.10  christos 	  (struct bfd_link_info *info,
   1393  1.10  christos 	   bfd *abfd,
   1394  1.10  christos 	   const char *name,
   1395  1.10  christos 	   flagword flags,
   1396  1.10  christos 	   asection *section,
   1397  1.10  christos 	   bfd_vma value,
   1398  1.10  christos 	   const char *string,
   1399  1.10  christos 	   bool copy,
   1400   1.1  christos 	   bool collect,
   1401   1.1  christos 	   struct bfd_link_hash_entry **hashp);
   1402   1.1  christos 
   1403   1.1  christos DESCRIPTION
   1404   1.1  christos    Add a symbol to the global hash table.
   1405   1.1  christos    ABFD is the BFD the symbol comes from.
   1406   1.1  christos    NAME is the name of the symbol.
   1407   1.1  christos    FLAGS is the BSF_* bits associated with the symbol.
   1408   1.1  christos    SECTION is the section in which the symbol is defined; this may be
   1409   1.1  christos      bfd_und_section_ptr or bfd_com_section_ptr.
   1410   1.1  christos    VALUE is the value of the symbol, relative to the section.
   1411   1.1  christos    STRING is used for either an indirect symbol, in which case it is
   1412   1.1  christos      the name of the symbol to indirect to, or a warning symbol, in
   1413   1.1  christos      which case it is the warning string.
   1414   1.1  christos    COPY is TRUE if NAME or STRING must be copied into locally
   1415   1.1  christos      allocated memory if they need to be saved.
   1416   1.1  christos    COLLECT is TRUE if we should automatically collect gcc constructor
   1417   1.8  christos      or destructor names as collect2 does.
   1418   1.1  christos    HASHP, if not NULL, is a place to store the created hash table
   1419   1.1  christos      entry; if *HASHP is not NULL, the caller has already looked up
   1420   1.1  christos      the hash table entry, and stored it in *HASHP.  */
   1421   1.1  christos 
   1422   1.1  christos bool
   1423   1.1  christos _bfd_generic_link_add_one_symbol (struct bfd_link_info *info,
   1424   1.1  christos 				  bfd *abfd,
   1425   1.8  christos 				  const char *name,
   1426   1.8  christos 				  flagword flags,
   1427   1.1  christos 				  asection *section,
   1428   1.1  christos 				  bfd_vma value,
   1429   1.1  christos 				  const char *string,
   1430   1.1  christos 				  bool copy,
   1431   1.3  christos 				  bool collect,
   1432   1.8  christos 				  struct bfd_link_hash_entry **hashp)
   1433   1.1  christos {
   1434   1.1  christos   enum link_row row;
   1435   1.1  christos   struct bfd_link_hash_entry *h;
   1436   1.1  christos   struct bfd_link_hash_entry *inh = NULL;
   1437   1.1  christos   bool cycle;
   1438   1.3  christos 
   1439   1.3  christos   BFD_ASSERT (section != NULL);
   1440   1.3  christos 
   1441   1.3  christos   if (bfd_is_ind_section (section)
   1442   1.3  christos       || (flags & BSF_INDIRECT) != 0)
   1443   1.8  christos     {
   1444   1.8  christos       row = INDR_ROW;
   1445   1.3  christos       /* Create the indirect symbol here.  This is for the benefit of
   1446   1.8  christos 	 the plugin "notice" function.
   1447   1.3  christos 	 STRING is the name of the symbol we want to indirect to.  */
   1448   1.1  christos       inh = bfd_wrapped_link_hash_lookup (abfd, info, string, true,
   1449   1.1  christos 					  copy, false);
   1450   1.1  christos       if (inh == NULL)
   1451   1.1  christos 	return false;
   1452   1.1  christos     }
   1453   1.1  christos   else if ((flags & BSF_WARNING) != 0)
   1454   1.1  christos     row = WARN_ROW;
   1455   1.1  christos   else if ((flags & BSF_CONSTRUCTOR) != 0)
   1456   1.1  christos     row = SET_ROW;
   1457   1.1  christos   else if (bfd_is_und_section (section))
   1458   1.1  christos     {
   1459   1.1  christos       if ((flags & BSF_WEAK) != 0)
   1460   1.1  christos 	row = UNDEFW_ROW;
   1461   1.1  christos       else
   1462   1.3  christos 	row = UNDEF_ROW;
   1463   1.3  christos     }
   1464   1.5  christos   else if ((flags & BSF_WEAK) != 0)
   1465   1.8  christos     row = DEFW_ROW;
   1466   1.6  christos   else if (bfd_is_com_section (section))
   1467   1.6  christos     {
   1468   1.6  christos       row = COMMON_ROW;
   1469   1.6  christos       if (!bfd_link_relocatable (info)
   1470   1.6  christos 	  && name != NULL
   1471   1.3  christos 	  && name[0] == '_'
   1472   1.1  christos 	  && name[1] == '_'
   1473   1.1  christos 	  && strcmp (name + (name[2] == '_'), "__gnu_lto_slim") == 0)
   1474   1.1  christos 	_bfd_error_handler
   1475   1.1  christos 	  (_("%pB: plugin needed to handle lto object"), abfd);
   1476   1.1  christos     }
   1477   1.1  christos   else
   1478   1.1  christos     row = DEF_ROW;
   1479   1.1  christos 
   1480   1.8  christos   if (hashp != NULL && *hashp != NULL)
   1481   1.1  christos     h = *hashp;
   1482   1.8  christos   else
   1483   1.1  christos     {
   1484   1.1  christos       if (row == UNDEF_ROW || row == UNDEFW_ROW)
   1485   1.1  christos 	h = bfd_wrapped_link_hash_lookup (abfd, info, name, true, copy, false);
   1486   1.1  christos       else
   1487   1.8  christos 	h = bfd_link_hash_lookup (info->hash, name, true, copy, false);
   1488   1.1  christos       if (h == NULL)
   1489   1.1  christos 	{
   1490   1.1  christos 	  if (hashp != NULL)
   1491   1.1  christos 	    *hashp = NULL;
   1492   1.1  christos 	  return false;
   1493   1.8  christos 	}
   1494   1.1  christos     }
   1495   1.3  christos 
   1496   1.3  christos   if (info->notice_all
   1497   1.8  christos       || (info->notice_hash != NULL
   1498   1.1  christos 	  && bfd_hash_lookup (info->notice_hash, name, false, false) != NULL))
   1499   1.1  christos     {
   1500   1.1  christos       if (! (*info->callbacks->notice) (info, h, inh,
   1501   1.1  christos 					abfd, section, value, flags))
   1502   1.1  christos 	return false;
   1503   1.1  christos     }
   1504   1.1  christos 
   1505   1.1  christos   if (hashp != NULL)
   1506   1.6  christos     *hashp = h;
   1507   1.1  christos 
   1508   1.6  christos   do
   1509   1.6  christos     {
   1510   1.6  christos       enum link_action action;
   1511   1.6  christos       int prev;
   1512   1.8  christos 
   1513   1.6  christos       prev = h->type;
   1514   1.1  christos       /* Treat symbols defined by early linker script pass as undefined.  */
   1515   1.1  christos       if (h->ldscript_def)
   1516   1.1  christos 	prev = bfd_link_hash_undefined;
   1517   1.1  christos       cycle = false;
   1518   1.1  christos       action = link_action[(int) row][prev];
   1519   1.1  christos       switch (action)
   1520   1.1  christos 	{
   1521   1.1  christos 	case FAIL:
   1522   1.1  christos 	  abort ();
   1523   1.1  christos 
   1524   1.1  christos 	case NOACT:
   1525   1.1  christos 	  /* Do nothing.  */
   1526   1.1  christos 	  break;
   1527   1.1  christos 
   1528   1.1  christos 	case UND:
   1529   1.1  christos 	  /* Make a new undefined symbol.  */
   1530   1.1  christos 	  h->type = bfd_link_hash_undefined;
   1531   1.1  christos 	  h->u.undef.abfd = abfd;
   1532   1.1  christos 	  bfd_link_add_undef (info->hash, h);
   1533   1.1  christos 	  break;
   1534   1.1  christos 
   1535   1.1  christos 	case WEAK:
   1536   1.1  christos 	  /* Make a new weak undefined symbol.  */
   1537   1.1  christos 	  h->type = bfd_link_hash_undefweak;
   1538   1.1  christos 	  h->u.undef.abfd = abfd;
   1539   1.1  christos 	  break;
   1540   1.5  christos 
   1541   1.5  christos 	case CDEF:
   1542   1.1  christos 	  /* We have found a definition for a symbol which was
   1543   1.1  christos 	     previously common.  */
   1544   1.1  christos 	  BFD_ASSERT (h->type == bfd_link_hash_common);
   1545   1.1  christos 	  (*info->callbacks->multiple_common) (info, h, abfd,
   1546   1.1  christos 					       bfd_link_hash_defined, 0);
   1547   1.1  christos 	  /* Fall through.  */
   1548   1.1  christos 	case DEF:
   1549   1.1  christos 	case DEFW:
   1550   1.1  christos 	  {
   1551   1.1  christos 	    enum bfd_link_hash_type oldtype;
   1552   1.1  christos 
   1553   1.1  christos 	    /* Define a symbol.  */
   1554   1.1  christos 	    oldtype = h->type;
   1555   1.1  christos 	    if (action == DEFW)
   1556   1.3  christos 	      h->type = bfd_link_hash_defweak;
   1557   1.6  christos 	    else
   1558   1.1  christos 	      h->type = bfd_link_hash_defined;
   1559   1.1  christos 	    h->u.def.section = section;
   1560   1.1  christos 	    h->u.def.value = value;
   1561   1.1  christos 	    h->linker_def = 0;
   1562   1.1  christos 	    h->ldscript_def = 0;
   1563   1.1  christos 
   1564   1.1  christos 	    /* If we have been asked to, we act like collect2 and
   1565   1.1  christos 	       identify all functions that might be global
   1566   1.1  christos 	       constructors and destructors and pass them up in a
   1567   1.1  christos 	       callback.  We only do this for certain object file
   1568   1.1  christos 	       types, since many object file types can handle this
   1569   1.1  christos 	       automatically.  */
   1570   1.1  christos 	    if (collect && name[0] == '_')
   1571   1.1  christos 	      {
   1572   1.1  christos 		const char *s;
   1573   1.1  christos 
   1574   1.1  christos 		/* A constructor or destructor name starts like this:
   1575   1.1  christos 		   _+GLOBAL_[_.$][ID][_.$] where the first [_.$] and
   1576   1.1  christos 		   the second are the same character (we accept any
   1577   1.1  christos 		   character there, in case a new object file format
   1578   1.1  christos 		   comes along with even worse naming restrictions).  */
   1579   1.1  christos 
   1580   1.1  christos #define CONS_PREFIX "GLOBAL_"
   1581   1.8  christos #define CONS_PREFIX_LEN (sizeof CONS_PREFIX - 1)
   1582   1.1  christos 
   1583   1.1  christos 		s = name + 1;
   1584   1.1  christos 		while (*s == '_')
   1585   1.1  christos 		  ++s;
   1586   1.1  christos 		if (s[0] == 'G' && startswith (s, CONS_PREFIX))
   1587   1.1  christos 		  {
   1588   1.1  christos 		    char c;
   1589   1.1  christos 
   1590   1.6  christos 		    c = s[CONS_PREFIX_LEN + 1];
   1591   1.6  christos 		    if ((c == 'I' || c == 'D')
   1592   1.6  christos 			&& s[CONS_PREFIX_LEN] == s[CONS_PREFIX_LEN + 2])
   1593   1.6  christos 		      {
   1594   1.6  christos 			/* If this is a definition of a symbol which
   1595   1.6  christos 			   was previously weakly defined, we are in
   1596   1.1  christos 			   trouble.  We have already added a
   1597   1.1  christos 			   constructor entry for the weak defined
   1598   1.1  christos 			   symbol, and now we are trying to add one
   1599   1.5  christos 			   for the new symbol.  Fortunately, this case
   1600   1.5  christos 			   should never arise in practice.  */
   1601   1.5  christos 			if (oldtype == bfd_link_hash_defweak)
   1602   1.1  christos 			  abort ();
   1603   1.1  christos 
   1604   1.1  christos 			(*info->callbacks->constructor) (info, c == 'I',
   1605   1.1  christos 							 h->root.string, abfd,
   1606   1.1  christos 							 section, value);
   1607   1.1  christos 		      }
   1608   1.1  christos 		  }
   1609   1.1  christos 	      }
   1610   1.1  christos 	  }
   1611   1.1  christos 
   1612   1.1  christos 	  break;
   1613   1.1  christos 
   1614   1.1  christos 	case COM:
   1615   1.1  christos 	  /* We have found a common definition for a symbol.  */
   1616   1.1  christos 	  if (h->type == bfd_link_hash_new)
   1617   1.1  christos 	    bfd_link_add_undef (info->hash, h);
   1618   1.8  christos 	  h->type = bfd_link_hash_common;
   1619   1.1  christos 	  h->u.c.p = (struct bfd_link_hash_common_entry *)
   1620   1.1  christos 	    bfd_hash_allocate (&info->hash->table,
   1621   1.1  christos 			       sizeof (struct bfd_link_hash_common_entry));
   1622   1.1  christos 	  if (h->u.c.p == NULL)
   1623   1.6  christos 	    return false;
   1624   1.1  christos 
   1625   1.1  christos 	  h->u.c.size = value;
   1626   1.1  christos 
   1627   1.1  christos 	  /* Select a default alignment based on the size.  This may
   1628   1.1  christos 	     be overridden by the caller.  */
   1629   1.1  christos 	  {
   1630   1.1  christos 	    unsigned int power;
   1631   1.1  christos 
   1632   1.1  christos 	    power = bfd_log2 (value);
   1633   1.1  christos 	    if (power > 4)
   1634   1.6  christos 	      power = 4;
   1635   1.6  christos 	    h->u.c.p->alignment_power = power;
   1636   1.6  christos 	  }
   1637   1.6  christos 
   1638   1.6  christos 	  /* The section of a common symbol is only used if the common
   1639   1.6  christos 	     symbol is actually allocated.  It basically provides a
   1640   1.6  christos 	     hook for the linker script to decide which output section
   1641   1.6  christos 	     the common symbols should be put in.  In most cases, the
   1642   1.6  christos 	     section of a common symbol will be bfd_com_section_ptr,
   1643   1.1  christos 	     the code here will choose a common symbol section named
   1644   1.1  christos 	     "COMMON", and the linker script will contain *(COMMON) in
   1645   1.1  christos 	     the appropriate place.  A few targets use separate common
   1646   1.1  christos 	     sections for small symbols, and they require special
   1647   1.1  christos 	     handling.  */
   1648   1.1  christos 	  if (section == bfd_com_section_ptr)
   1649   1.1  christos 	    {
   1650   1.1  christos 	      h->u.c.p->section = bfd_make_section_old_way (abfd, "COMMON");
   1651   1.1  christos 	      h->u.c.p->section->flags |= SEC_ALLOC;
   1652   1.1  christos 	    }
   1653   1.1  christos 	  else if (section->owner != abfd)
   1654   1.1  christos 	    {
   1655   1.1  christos 	      h->u.c.p->section = bfd_make_section_old_way (abfd,
   1656   1.3  christos 							    section->name);
   1657   1.6  christos 	      h->u.c.p->section->flags |= SEC_ALLOC;
   1658   1.1  christos 	    }
   1659   1.1  christos 	  else
   1660   1.1  christos 	    h->u.c.p->section = section;
   1661   1.1  christos 	  h->linker_def = 0;
   1662   1.1  christos 	  h->ldscript_def = 0;
   1663   1.1  christos 	  break;
   1664   1.1  christos 
   1665   1.1  christos 	case REF:
   1666   1.1  christos 	  /* A reference to a defined symbol.  */
   1667   1.1  christos 	  if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
   1668   1.1  christos 	    h->u.undef.next = h;
   1669   1.1  christos 	  break;
   1670   1.1  christos 
   1671   1.5  christos 	case BIG:
   1672   1.5  christos 	  /* We have found a common definition for a symbol which
   1673   1.1  christos 	     already had a common definition.  Use the maximum of the
   1674   1.1  christos 	     two sizes, and use the section required by the larger symbol.  */
   1675   1.1  christos 	  BFD_ASSERT (h->type == bfd_link_hash_common);
   1676   1.1  christos 	  (*info->callbacks->multiple_common) (info, h, abfd,
   1677   1.1  christos 					       bfd_link_hash_common, value);
   1678   1.1  christos 	  if (value > h->u.c.size)
   1679   1.1  christos 	    {
   1680   1.1  christos 	      unsigned int power;
   1681   1.1  christos 
   1682   1.1  christos 	      h->u.c.size = value;
   1683   1.1  christos 
   1684   1.1  christos 	      /* Select a default alignment based on the size.  This may
   1685   1.1  christos 		 be overridden by the caller.  */
   1686   1.1  christos 	      power = bfd_log2 (value);
   1687   1.1  christos 	      if (power > 4)
   1688   1.1  christos 		power = 4;
   1689   1.1  christos 	      h->u.c.p->alignment_power = power;
   1690   1.1  christos 
   1691   1.1  christos 	      /* Some systems have special treatment for small commons,
   1692   1.1  christos 		 hence we want to select the section used by the larger
   1693   1.1  christos 		 symbol.  This makes sure the symbol does not go in a
   1694   1.1  christos 		 small common section if it is now too large.  */
   1695   1.1  christos 	      if (section == bfd_com_section_ptr)
   1696   1.1  christos 		{
   1697   1.1  christos 		  h->u.c.p->section
   1698   1.1  christos 		    = bfd_make_section_old_way (abfd, "COMMON");
   1699   1.1  christos 		  h->u.c.p->section->flags |= SEC_ALLOC;
   1700   1.1  christos 		}
   1701   1.1  christos 	      else if (section->owner != abfd)
   1702   1.1  christos 		{
   1703   1.1  christos 		  h->u.c.p->section
   1704   1.1  christos 		    = bfd_make_section_old_way (abfd, section->name);
   1705   1.1  christos 		  h->u.c.p->section->flags |= SEC_ALLOC;
   1706   1.1  christos 		}
   1707   1.1  christos 	      else
   1708   1.1  christos 		h->u.c.p->section = section;
   1709   1.1  christos 	    }
   1710   1.5  christos 	  break;
   1711   1.5  christos 
   1712   1.1  christos 	case CREF:
   1713   1.1  christos 	  /* We have found a common definition for a symbol which
   1714   1.1  christos 	     was already defined.  */
   1715   1.1  christos 	  (*info->callbacks->multiple_common) (info, h, abfd,
   1716   1.1  christos 					       bfd_link_hash_common, value);
   1717  1.10  christos 	  break;
   1718  1.10  christos 
   1719   1.8  christos 	case MIND:
   1720   1.8  christos 	  /* Multiple indirect symbols.  This is OK if they both point
   1721   1.8  christos 	     to the same symbol.  */
   1722   1.8  christos 	  if (h->u.i.link == inh)
   1723   1.8  christos 	    break;
   1724   1.8  christos 	  if (h->u.i.link->type == bfd_link_hash_defweak)
   1725   1.8  christos 	    {
   1726   1.8  christos 	      /* It is also OK to redefine a symbol that indirects to
   1727   1.8  christos 		 a weak definition.  So for sym@ver -> sym@@ver where
   1728   1.8  christos 		 sym@@ver is weak and we have a new strong sym@ver,
   1729   1.8  christos 		 redefine sym@@ver.  Of course if there exists
   1730   1.1  christos 		 sym -> sym@@ver then this also redefines sym.  */
   1731   1.1  christos 	      h = h->u.i.link;
   1732   1.1  christos 	      cycle = true;
   1733   1.5  christos 	      break;
   1734   1.5  christos 	    }
   1735   1.1  christos 	  /* Fall through.  */
   1736   1.1  christos 	case MDEF:
   1737   1.1  christos 	  /* Handle a multiple definition.  */
   1738   1.1  christos 	  (*info->callbacks->multiple_definition) (info, h,
   1739   1.1  christos 						   abfd, section, value);
   1740   1.5  christos 	  break;
   1741   1.5  christos 
   1742   1.1  christos 	case CIND:
   1743   1.1  christos 	  /* Create an indirect symbol from an existing common symbol.  */
   1744   1.3  christos 	  BFD_ASSERT (h->type == bfd_link_hash_common);
   1745   1.3  christos 	  (*info->callbacks->multiple_common) (info, h, abfd,
   1746   1.3  christos 					       bfd_link_hash_indirect, 0);
   1747   1.6  christos 	  /* Fall through.  */
   1748   1.6  christos 	case IND:
   1749   1.6  christos 	  if (inh->type == bfd_link_hash_indirect
   1750   1.3  christos 	      && inh->u.i.link == h)
   1751   1.3  christos 	    {
   1752   1.8  christos 	      _bfd_error_handler
   1753   1.3  christos 		/* xgettext:c-format */
   1754   1.3  christos 		(_("%pB: indirect symbol `%s' to `%s' is a loop"),
   1755   1.3  christos 		 abfd, name, string);
   1756   1.3  christos 	      bfd_set_error (bfd_error_invalid_operation);
   1757   1.3  christos 	      return false;
   1758   1.3  christos 	    }
   1759   1.3  christos 	  if (inh->type == bfd_link_hash_new)
   1760   1.3  christos 	    {
   1761   1.3  christos 	      inh->type = bfd_link_hash_undefined;
   1762   1.3  christos 	      inh->u.undef.abfd = abfd;
   1763   1.3  christos 	      bfd_link_add_undef (info->hash, inh);
   1764   1.3  christos 	    }
   1765   1.3  christos 
   1766   1.3  christos 	  /* If the indirect symbol has been referenced, we need to
   1767   1.3  christos 	     push the reference down to the symbol we are referencing.  */
   1768   1.8  christos 	  if (h->type != bfd_link_hash_new)
   1769   1.3  christos 	    {
   1770   1.3  christos 	      /* ??? If inh->type == bfd_link_hash_undefweak this
   1771   1.3  christos 		 converts inh to bfd_link_hash_undefined.  */
   1772   1.3  christos 	      row = UNDEF_ROW;
   1773   1.3  christos 	      cycle = true;
   1774   1.3  christos 	    }
   1775   1.3  christos 
   1776   1.3  christos 	  h->type = bfd_link_hash_indirect;
   1777   1.3  christos 	  h->u.i.link = inh;
   1778   1.3  christos 	  /* Not setting h = h->u.i.link here means that when cycle is
   1779   1.1  christos 	     set above we'll always go to REFC, and then cycle again
   1780   1.1  christos 	     to the indirected symbol.  This means that any successful
   1781   1.1  christos 	     change of an existing symbol to indirect counts as a
   1782   1.1  christos 	     reference.  ??? That may not be correct when the existing
   1783   1.5  christos 	     symbol was defweak.  */
   1784   1.5  christos 	  break;
   1785   1.1  christos 
   1786   1.1  christos 	case SET:
   1787   1.1  christos 	  /* Add an entry to a set.  */
   1788   1.3  christos 	  (*info->callbacks->add_to_set) (info, h, BFD_RELOC_CTOR,
   1789   1.3  christos 					  abfd, section, value);
   1790   1.3  christos 	  break;
   1791   1.3  christos 
   1792   1.1  christos 	case WARNC:
   1793   1.5  christos 	  /* Issue a warning and cycle, except when the reference is
   1794   1.5  christos 	     in LTO IR.  */
   1795   1.1  christos 	  if (h->u.i.warning != NULL
   1796   1.1  christos 	      && (abfd->flags & BFD_PLUGIN) == 0)
   1797   1.1  christos 	    {
   1798   1.1  christos 	      (*info->callbacks->warning) (info, h->u.i.warning,
   1799   1.1  christos 					   h->root.string, abfd, NULL, 0);
   1800   1.1  christos 	      /* Only issue a warning once.  */
   1801   1.1  christos 	      h->u.i.warning = NULL;
   1802   1.8  christos 	    }
   1803   1.1  christos 	  /* Fall through.  */
   1804   1.1  christos 	case CYCLE:
   1805   1.1  christos 	  /* Try again with the referenced symbol.  */
   1806   1.1  christos 	  h = h->u.i.link;
   1807   1.1  christos 	  cycle = true;
   1808   1.1  christos 	  break;
   1809   1.1  christos 
   1810   1.8  christos 	case REFC:
   1811   1.1  christos 	  /* A reference to an indirect symbol.  */
   1812   1.1  christos 	  if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
   1813   1.1  christos 	    h->u.undef.next = h;
   1814   1.3  christos 	  h = h->u.i.link;
   1815   1.3  christos 	  cycle = true;
   1816   1.3  christos 	  break;
   1817   1.3  christos 
   1818   1.6  christos 	case WARN:
   1819   1.6  christos 	  /* Warn if this symbol has been referenced already from non-IR,
   1820   1.1  christos 	     otherwise add a warning.  */
   1821   1.5  christos 	  if ((!info->lto_plugin_active
   1822   1.5  christos 	       && (h->u.undef.next != NULL || info->hash->undefs_tail == h))
   1823   1.9  christos 	      || h->non_ir_ref_regular
   1824   1.9  christos 	      || h->non_ir_ref_dynamic)
   1825   1.9  christos 	    {
   1826   1.9  christos 	      (*info->callbacks->warning) (info, string, h->root.string,
   1827   1.9  christos 					   hash_entry_bfd (h), NULL, 0);
   1828   1.9  christos 	      /* PR 31067: If garbage collection is enabled then the
   1829   1.9  christos 		 referenced symbol may actually be discarded later on.
   1830   1.9  christos 		 This could be very confusing to the user.  So give them
   1831   1.1  christos 		 a hint as to what might be happening.  */
   1832   1.1  christos 	      if (info->gc_sections)
   1833   1.1  christos 		(*info->callbacks->info)
   1834   1.1  christos 		  (_("%P: %pB: note: the message above does not take linker garbage collection into account\n"),
   1835   1.1  christos 		   hash_entry_bfd (h));
   1836   1.1  christos 	      break;
   1837   1.1  christos 	    }
   1838   1.1  christos 	  /* Fall through.  */
   1839   1.1  christos 	case MWARN:
   1840   1.1  christos 	  /* Make a warning symbol.  */
   1841   1.1  christos 	  {
   1842   1.1  christos 	    struct bfd_link_hash_entry *sub;
   1843   1.1  christos 
   1844   1.8  christos 	    /* STRING is the warning to give.  */
   1845   1.1  christos 	    sub = ((struct bfd_link_hash_entry *)
   1846   1.1  christos 		   ((*info->hash->table.newfunc)
   1847   1.1  christos 		    (NULL, &info->hash->table, h->root.string)));
   1848   1.1  christos 	    if (sub == NULL)
   1849   1.1  christos 	      return false;
   1850   1.1  christos 	    *sub = *h;
   1851   1.1  christos 	    sub->type = bfd_link_hash_warning;
   1852   1.1  christos 	    sub->u.i.link = h;
   1853   1.1  christos 	    if (! copy)
   1854   1.1  christos 	      sub->u.i.warning = string;
   1855   1.1  christos 	    else
   1856   1.1  christos 	      {
   1857   1.8  christos 		char *w;
   1858   1.1  christos 		size_t len = strlen (string) + 1;
   1859   1.1  christos 
   1860   1.1  christos 		w = (char *) bfd_hash_allocate (&info->hash->table, len);
   1861   1.1  christos 		if (w == NULL)
   1862   1.1  christos 		  return false;
   1863   1.1  christos 		memcpy (w, string, len);
   1864   1.1  christos 		sub->u.i.warning = w;
   1865   1.1  christos 	      }
   1866   1.1  christos 
   1867   1.1  christos 	    bfd_hash_replace (&info->hash->table,
   1868   1.1  christos 			      (struct bfd_hash_entry *) h,
   1869   1.1  christos 			      (struct bfd_hash_entry *) sub);
   1870   1.1  christos 	    if (hashp != NULL)
   1871   1.1  christos 	      *hashp = sub;
   1872   1.1  christos 	  }
   1873   1.8  christos 	  break;
   1874   1.1  christos 	}
   1875  1.10  christos     }
   1876  1.10  christos   while (cycle);
   1877  1.10  christos 
   1878  1.10  christos   return true;
   1879  1.10  christos }
   1880  1.10  christos 
   1881  1.10  christos /*
   1882  1.10  christos FUNCTION
   1883  1.10  christos 	bfd_link_align_section
   1884  1.10  christos 
   1885  1.10  christos SYNOPSIS
   1886  1.10  christos 	bool bfd_link_align_section (asection *, unsigned int);
   1887  1.10  christos 
   1888  1.10  christos DESCRIPTION
   1889  1.10  christos 	Increase section alignment if the current section alignment is
   1890  1.10  christos 	less than the requested value.  Adjust output section
   1891  1.10  christos 	alignment too, so that linker layout adjusts for alignment on
   1892  1.10  christos 	the current lang_size_sections pass.  This is important for
   1893  1.10  christos 	lang_size_relro_segment.  If the output section alignment
   1894  1.10  christos 	isn't adjusted, the linker will place the output section at an
   1895  1.10  christos 	address depending on its current alignment.  When sizing the
   1896  1.10  christos 	output section, input sections attached transfer any increase
   1897  1.10  christos 	in alignment to the output section, which will affect layout
   1898  1.10  christos 	for the next sizing pass.  Which is all well and good except
   1899  1.10  christos 	that lang_size_relro_segment for the current sizing pass uses
   1900  1.10  christos 	that possibly increased alignment with a layout that doesn't
   1901  1.10  christos 	suit.
   1902  1.10  christos */
   1903  1.10  christos 
   1904  1.10  christos bool
   1905  1.10  christos bfd_link_align_section (asection *sec, unsigned int align_p2)
   1906  1.10  christos {
   1907  1.10  christos   if (align_p2 > bfd_section_alignment (sec))
   1908  1.10  christos     {
   1909  1.10  christos       if (!bfd_set_section_alignment (sec, align_p2))
   1910  1.10  christos 	return false;
   1911  1.10  christos       asection *osec = sec->output_section;
   1912  1.10  christos       if (osec && align_p2 > bfd_section_alignment (osec))
   1913  1.10  christos 	{
   1914  1.10  christos 	  if (!bfd_set_section_alignment (osec, align_p2))
   1915  1.10  christos 	    return false;
   1916   1.1  christos 	}
   1917   1.1  christos     }
   1918   1.8  christos   return true;
   1919   1.1  christos }
   1920   1.1  christos 
   1921   1.1  christos /* Generic final link routine.  */
   1922   1.1  christos 
   1923   1.1  christos bool
   1924   1.1  christos _bfd_generic_final_link (bfd *abfd, struct bfd_link_info *info)
   1925   1.1  christos {
   1926   1.1  christos   bfd *sub;
   1927   1.7  christos   asection *o;
   1928   1.7  christos   struct bfd_link_order *p;
   1929   1.1  christos   size_t outsymalloc;
   1930   1.1  christos   struct generic_write_global_symbol_info wginfo;
   1931   1.1  christos 
   1932   1.1  christos   abfd->outsymbols = NULL;
   1933   1.1  christos   abfd->symcount = 0;
   1934   1.1  christos   outsymalloc = 0;
   1935   1.8  christos 
   1936   1.1  christos   /* Mark all sections which will be included in the output file.  */
   1937   1.1  christos   for (o = abfd->sections; o != NULL; o = o->next)
   1938   1.3  christos     for (p = o->map_head.link_order; p != NULL; p = p->next)
   1939   1.1  christos       if (p->type == bfd_indirect_link_order)
   1940   1.8  christos 	p->u.indirect.section->linker_mark = true;
   1941   1.1  christos 
   1942   1.1  christos   /* Build the output symbol table.  */
   1943   1.1  christos   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   1944   1.1  christos     if (! _bfd_generic_link_output_symbols (abfd, sub, info, &outsymalloc))
   1945   1.1  christos       return false;
   1946  1.10  christos 
   1947   1.1  christos   /* Accumulate the global symbols.  */
   1948   1.1  christos   wginfo.info = info;
   1949   1.1  christos   wginfo.output_bfd = abfd;
   1950  1.10  christos   wginfo.psymalloc = &outsymalloc;
   1951  1.10  christos   wginfo.failed = false;
   1952   1.1  christos   _bfd_generic_link_hash_traverse (_bfd_generic_hash_table (info),
   1953   1.1  christos 				   _bfd_generic_link_write_global_symbol,
   1954   1.1  christos 				   &wginfo);
   1955   1.1  christos   if (wginfo.failed)
   1956   1.1  christos     return false;
   1957   1.8  christos 
   1958   1.1  christos   /* Make sure we have a trailing NULL pointer on OUTSYMBOLS.  We
   1959   1.3  christos      shouldn't really need one, since we have SYMCOUNT, but some old
   1960   1.1  christos      code still expects one.  */
   1961   1.1  christos   if (! generic_add_output_symbol (abfd, &outsymalloc, NULL))
   1962   1.1  christos     return false;
   1963   1.1  christos 
   1964   1.1  christos   if (bfd_link_relocatable (info))
   1965   1.1  christos     {
   1966   1.1  christos       /* Allocate space for the output relocs for each section.  */
   1967   1.1  christos       for (o = abfd->sections; o != NULL; o = o->next)
   1968   1.1  christos 	{
   1969   1.1  christos 	  o->reloc_count = 0;
   1970   1.1  christos 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   1971   1.1  christos 	    {
   1972   1.1  christos 	      if (p->type == bfd_section_reloc_link_order
   1973   1.1  christos 		  || p->type == bfd_symbol_reloc_link_order)
   1974   1.1  christos 		++o->reloc_count;
   1975   1.1  christos 	      else if (p->type == bfd_indirect_link_order)
   1976   1.1  christos 		{
   1977   1.1  christos 		  asection *input_section;
   1978   1.1  christos 		  bfd *input_bfd;
   1979   1.1  christos 		  long relsize;
   1980   1.1  christos 		  arelent **relocs;
   1981   1.1  christos 		  asymbol **symbols;
   1982   1.1  christos 		  long reloc_count;
   1983   1.1  christos 
   1984   1.8  christos 		  input_section = p->u.indirect.section;
   1985   1.1  christos 		  input_bfd = input_section->owner;
   1986   1.1  christos 		  relsize = bfd_get_reloc_upper_bound (input_bfd,
   1987   1.8  christos 						       input_section);
   1988   1.1  christos 		  if (relsize < 0)
   1989   1.1  christos 		    return false;
   1990   1.1  christos 		  relocs = (arelent **) bfd_malloc (relsize);
   1991   1.1  christos 		  if (!relocs && relsize != 0)
   1992   1.1  christos 		    return false;
   1993   1.1  christos 		  symbols = _bfd_generic_link_get_symbols (input_bfd);
   1994   1.1  christos 		  reloc_count = bfd_canonicalize_reloc (input_bfd,
   1995   1.8  christos 							input_section,
   1996   1.1  christos 							relocs,
   1997   1.1  christos 							symbols);
   1998   1.1  christos 		  free (relocs);
   1999   1.1  christos 		  if (reloc_count < 0)
   2000   1.1  christos 		    return false;
   2001   1.1  christos 		  BFD_ASSERT ((unsigned long) reloc_count
   2002   1.1  christos 			      == input_section->reloc_count);
   2003   1.1  christos 		  o->reloc_count += reloc_count;
   2004   1.1  christos 		}
   2005   1.1  christos 	    }
   2006   1.1  christos 	  if (o->reloc_count > 0)
   2007   1.1  christos 	    {
   2008   1.1  christos 	      bfd_size_type amt;
   2009   1.8  christos 
   2010   1.1  christos 	      amt = o->reloc_count;
   2011   1.1  christos 	      amt *= sizeof (arelent *);
   2012   1.1  christos 	      o->orelocation = (struct reloc_cache_entry **) bfd_alloc (abfd, amt);
   2013   1.1  christos 	      if (!o->orelocation)
   2014   1.1  christos 		return false;
   2015   1.1  christos 	      o->flags |= SEC_RELOC;
   2016   1.1  christos 	      /* Reset the count so that it can be used as an index
   2017   1.1  christos 		 when putting in the output relocs.  */
   2018   1.1  christos 	      o->reloc_count = 0;
   2019   1.1  christos 	    }
   2020   1.1  christos 	}
   2021   1.1  christos     }
   2022   1.1  christos 
   2023   1.1  christos   /* Handle all the link order information for the sections.  */
   2024   1.1  christos   for (o = abfd->sections; o != NULL; o = o->next)
   2025   1.1  christos     {
   2026   1.1  christos       for (p = o->map_head.link_order; p != NULL; p = p->next)
   2027   1.1  christos 	{
   2028   1.8  christos 	  switch (p->type)
   2029   1.1  christos 	    {
   2030   1.1  christos 	    case bfd_section_reloc_link_order:
   2031   1.8  christos 	    case bfd_symbol_reloc_link_order:
   2032   1.8  christos 	      if (! _bfd_generic_reloc_link_order (abfd, info, o, p))
   2033   1.1  christos 		return false;
   2034   1.1  christos 	      break;
   2035   1.1  christos 	    case bfd_indirect_link_order:
   2036   1.8  christos 	      if (! default_indirect_link_order (abfd, info, o, p, true))
   2037   1.1  christos 		return false;
   2038   1.1  christos 	      break;
   2039   1.1  christos 	    default:
   2040   1.1  christos 	      if (! _bfd_default_link_order (abfd, info, o, p))
   2041   1.1  christos 		return false;
   2042   1.8  christos 	      break;
   2043   1.1  christos 	    }
   2044   1.1  christos 	}
   2045   1.1  christos     }
   2046   1.1  christos 
   2047   1.8  christos   return true;
   2048   1.1  christos }
   2049   1.1  christos 
   2050  1.10  christos /* Add an output symbol to the output BFD.  */
   2051  1.10  christos 
   2052  1.10  christos static bool
   2053   1.1  christos generic_add_output_symbol (bfd *output_bfd, size_t *psymalloc, asymbol *sym)
   2054   1.1  christos {
   2055   1.1  christos   if (!(bfd_applicable_file_flags (output_bfd) & HAS_SYMS))
   2056   1.1  christos     return true;
   2057   1.1  christos 
   2058   1.1  christos   if (bfd_get_symcount (output_bfd) >= *psymalloc)
   2059   1.1  christos     {
   2060   1.1  christos       asymbol **newsyms;
   2061   1.1  christos       bfd_size_type amt;
   2062   1.1  christos 
   2063   1.1  christos       if (*psymalloc == 0)
   2064   1.1  christos 	*psymalloc = 124;
   2065   1.1  christos       else
   2066   1.8  christos 	*psymalloc *= 2;
   2067   1.7  christos       amt = *psymalloc;
   2068   1.1  christos       amt *= sizeof (asymbol *);
   2069   1.1  christos       newsyms = (asymbol **) bfd_realloc (bfd_get_outsymbols (output_bfd), amt);
   2070   1.7  christos       if (newsyms == NULL)
   2071   1.1  christos 	return false;
   2072   1.7  christos       output_bfd->outsymbols = newsyms;
   2073   1.1  christos     }
   2074   1.8  christos 
   2075   1.1  christos   output_bfd->outsymbols[output_bfd->symcount] = sym;
   2076   1.1  christos   if (sym != NULL)
   2077   1.1  christos     ++output_bfd->symcount;
   2078   1.1  christos 
   2079  1.10  christos   return true;
   2080   1.1  christos }
   2081   1.1  christos 
   2082   1.1  christos /* Handle the symbols for an input BFD.  */
   2083   1.1  christos 
   2084   1.1  christos static bool
   2085   1.1  christos _bfd_generic_link_output_symbols (bfd *output_bfd,
   2086   1.1  christos 				  bfd *input_bfd,
   2087   1.1  christos 				  struct bfd_link_info *info,
   2088   1.1  christos 				  size_t *psymalloc)
   2089   1.8  christos {
   2090   1.1  christos   asymbol **sym_ptr;
   2091   1.1  christos   asymbol **sym_end;
   2092   1.1  christos 
   2093   1.1  christos   if (!bfd_generic_link_read_symbols (input_bfd))
   2094   1.1  christos     return false;
   2095   1.1  christos 
   2096   1.1  christos   /* Create a filename symbol if we are supposed to.  */
   2097   1.1  christos   if (info->create_object_symbols_section != NULL)
   2098   1.1  christos     {
   2099   1.1  christos       asection *sec;
   2100   1.1  christos 
   2101   1.1  christos       for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
   2102   1.1  christos 	{
   2103   1.1  christos 	  if (sec->output_section == info->create_object_symbols_section)
   2104   1.8  christos 	    {
   2105   1.8  christos 	      asymbol *newsym;
   2106   1.1  christos 
   2107   1.1  christos 	      newsym = bfd_make_empty_symbol (input_bfd);
   2108   1.1  christos 	      if (!newsym)
   2109   1.1  christos 		return false;
   2110   1.1  christos 	      newsym->name = bfd_get_filename (input_bfd);
   2111   1.1  christos 	      newsym->value = 0;
   2112   1.8  christos 	      newsym->flags = BSF_LOCAL | BSF_FILE;
   2113   1.1  christos 	      newsym->section = sec;
   2114   1.1  christos 
   2115   1.1  christos 	      if (! generic_add_output_symbol (output_bfd, psymalloc,
   2116   1.1  christos 					       newsym))
   2117   1.1  christos 		return false;
   2118   1.1  christos 
   2119   1.1  christos 	      break;
   2120   1.1  christos 	    }
   2121   1.1  christos 	}
   2122   1.1  christos     }
   2123   1.1  christos 
   2124   1.1  christos   /* Adjust the values of the globally visible symbols, and write out
   2125   1.1  christos      local symbols.  */
   2126   1.1  christos   sym_ptr = _bfd_generic_link_get_symbols (input_bfd);
   2127   1.1  christos   sym_end = sym_ptr + _bfd_generic_link_get_symcount (input_bfd);
   2128   1.1  christos   for (; sym_ptr < sym_end; sym_ptr++)
   2129   1.1  christos     {
   2130   1.1  christos       asymbol *sym;
   2131   1.1  christos       struct generic_link_hash_entry *h;
   2132   1.1  christos 
   2133   1.1  christos       h = NULL;
   2134   1.1  christos       sym = *sym_ptr;
   2135   1.7  christos       if ((sym->flags & (BSF_INDIRECT
   2136   1.7  christos 			 | BSF_WARNING
   2137   1.7  christos 			 | BSF_GLOBAL
   2138   1.1  christos 			 | BSF_CONSTRUCTOR
   2139   1.1  christos 			 | BSF_WEAK)) != 0
   2140   1.1  christos 	  || bfd_is_und_section (bfd_asymbol_section (sym))
   2141   1.1  christos 	  || bfd_is_com_section (bfd_asymbol_section (sym))
   2142   1.1  christos 	  || bfd_is_ind_section (bfd_asymbol_section (sym)))
   2143   1.1  christos 	{
   2144   1.6  christos 	  if (sym->udata.p != NULL)
   2145   1.6  christos 	    h = (struct generic_link_hash_entry *) sym->udata.p;
   2146   1.6  christos 	  else if ((sym->flags & BSF_CONSTRUCTOR) != 0)
   2147   1.6  christos 	    {
   2148   1.6  christos 	      /* This case normally means that the main linker code
   2149   1.6  christos 		 deliberately ignored this constructor symbol.  We
   2150   1.6  christos 		 should just pass it through.  This will screw up if
   2151   1.1  christos 		 the constructor symbol is from a different,
   2152   1.1  christos 		 non-generic, object file format, but the case will
   2153   1.7  christos 		 only arise when linking with -r, which will probably
   2154   1.1  christos 		 fail anyhow, since there will be no way to represent
   2155   1.1  christos 		 the relocs in the output format being used.  */
   2156   1.1  christos 	      h = NULL;
   2157   1.8  christos 	    }
   2158   1.1  christos 	  else if (bfd_is_und_section (bfd_asymbol_section (sym)))
   2159   1.1  christos 	    h = ((struct generic_link_hash_entry *)
   2160   1.1  christos 		 bfd_wrapped_link_hash_lookup (output_bfd, info,
   2161   1.8  christos 					       bfd_asymbol_name (sym),
   2162   1.1  christos 					       false, false, true));
   2163   1.1  christos 	  else
   2164   1.1  christos 	    h = _bfd_generic_link_hash_lookup (_bfd_generic_hash_table (info),
   2165   1.1  christos 					       bfd_asymbol_name (sym),
   2166   1.1  christos 					       false, false, true);
   2167   1.1  christos 
   2168   1.1  christos 	  if (h != NULL)
   2169   1.1  christos 	    {
   2170   1.1  christos 	      /* Force all references to this symbol to point to
   2171   1.1  christos 		 the same area in memory.  It is possible that
   2172   1.1  christos 		 this routine will be called with a hash table
   2173   1.1  christos 		 other than a generic hash table, so we double
   2174   1.1  christos 		 check that.  */
   2175   1.1  christos 	      if (info->output_bfd->xvec == input_bfd->xvec)
   2176   1.1  christos 		{
   2177   1.1  christos 		  if (h->sym != NULL)
   2178   1.1  christos 		    *sym_ptr = sym = h->sym;
   2179   1.1  christos 		}
   2180   1.1  christos 
   2181   1.1  christos 	      switch (h->root.type)
   2182   1.1  christos 		{
   2183   1.1  christos 		default:
   2184   1.1  christos 		case bfd_link_hash_new:
   2185   1.1  christos 		  abort ();
   2186   1.1  christos 		case bfd_link_hash_undefined:
   2187   1.1  christos 		  break;
   2188   1.1  christos 		case bfd_link_hash_undefweak:
   2189   1.1  christos 		  sym->flags |= BSF_WEAK;
   2190   1.1  christos 		  break;
   2191   1.3  christos 		case bfd_link_hash_indirect:
   2192   1.1  christos 		  h = (struct generic_link_hash_entry *) h->root.u.i.link;
   2193   1.1  christos 		  /* fall through */
   2194   1.1  christos 		case bfd_link_hash_defined:
   2195   1.1  christos 		  sym->flags |= BSF_GLOBAL;
   2196   1.1  christos 		  sym->flags &=~ (BSF_WEAK | BSF_CONSTRUCTOR);
   2197   1.1  christos 		  sym->value = h->root.u.def.value;
   2198   1.1  christos 		  sym->section = h->root.u.def.section;
   2199   1.1  christos 		  break;
   2200   1.1  christos 		case bfd_link_hash_defweak:
   2201   1.1  christos 		  sym->flags |= BSF_WEAK;
   2202   1.1  christos 		  sym->flags &=~ BSF_CONSTRUCTOR;
   2203   1.1  christos 		  sym->value = h->root.u.def.value;
   2204   1.1  christos 		  sym->section = h->root.u.def.section;
   2205   1.1  christos 		  break;
   2206   1.1  christos 		case bfd_link_hash_common:
   2207   1.1  christos 		  sym->value = h->root.u.c.size;
   2208   1.1  christos 		  sym->flags |= BSF_GLOBAL;
   2209   1.1  christos 		  if (! bfd_is_com_section (sym->section))
   2210   1.1  christos 		    {
   2211   1.1  christos 		      BFD_ASSERT (bfd_is_und_section (sym->section));
   2212   1.1  christos 		      sym->section = bfd_com_section_ptr;
   2213   1.1  christos 		    }
   2214   1.1  christos 		  /* We do not set the section of the symbol to
   2215   1.1  christos 		     h->root.u.c.p->section.  That value was saved so
   2216   1.1  christos 		     that we would know where to allocate the symbol
   2217   1.1  christos 		     if it was defined.  In this case the type is
   2218   1.1  christos 		     still bfd_link_hash_common, so we did not define
   2219   1.1  christos 		     it, so we do not want to use that section.  */
   2220  1.10  christos 		  break;
   2221   1.7  christos 		}
   2222   1.7  christos 	    }
   2223   1.7  christos 	}
   2224   1.7  christos 
   2225   1.8  christos       bool output = false;
   2226  1.10  christos       if ((sym->flags & BSF_KEEP) == 0
   2227  1.10  christos 	  && (info->strip == strip_all
   2228  1.10  christos 	      || (info->strip == strip_some
   2229  1.10  christos 		  && bfd_hash_lookup (info->keep_hash, bfd_asymbol_name (sym),
   2230  1.10  christos 				      false, false) == NULL)))
   2231  1.10  christos 	;
   2232  1.10  christos       /* If this symbol is in a section which is not being included
   2233  1.10  christos 	 in the output file, then we don't want to output the
   2234   1.6  christos 	 symbol.  */
   2235   1.1  christos       else if (!bfd_is_abs_section (sym->section)
   2236   1.1  christos 	       && bfd_section_removed_from_list (output_bfd,
   2237   1.1  christos 						 sym->section->output_section))
   2238   1.1  christos 	;
   2239   1.1  christos       else if ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0)
   2240   1.1  christos 	{
   2241   1.1  christos 	  /* If this symbol is marked as occurring now, rather
   2242   1.8  christos 	     than at the end, output it now.  This is used for
   2243   1.1  christos 	     COFF C_EXT FCN symbols.  FIXME: There must be a
   2244   1.7  christos 	     better way.  */
   2245   1.8  christos 	  if (bfd_asymbol_bfd (sym) == input_bfd
   2246   1.1  christos 	      && (sym->flags & BSF_NOT_AT_END) != 0)
   2247  1.10  christos 	    output = true;
   2248   1.1  christos 	}
   2249   1.1  christos       else if ((sym->flags & BSF_KEEP) != 0)
   2250   1.1  christos 	output = true;
   2251   1.8  christos       else if (bfd_is_ind_section (sym->section))
   2252   1.1  christos 	;
   2253   1.1  christos       else if ((sym->flags & BSF_DEBUGGING) != 0)
   2254   1.1  christos 	{
   2255  1.10  christos 	  if (info->strip == strip_none)
   2256   1.1  christos 	    output = true;
   2257   1.1  christos 	}
   2258  1.10  christos       else if (bfd_is_und_section (sym->section)
   2259   1.1  christos 	       || bfd_is_com_section (sym->section))
   2260   1.1  christos 	;
   2261   1.1  christos       else if ((sym->flags & BSF_LOCAL) != 0)
   2262   1.1  christos 	{
   2263   1.1  christos 	  if ((sym->flags & BSF_WARNING) == 0)
   2264   1.1  christos 	    {
   2265   1.1  christos 	      switch (info->discard)
   2266   1.8  christos 		{
   2267   1.3  christos 		default:
   2268   1.1  christos 		case discard_all:
   2269   1.1  christos 		  break;
   2270   1.1  christos 		case discard_sec_merge:
   2271   1.1  christos 		  output = true;
   2272  1.10  christos 		  if (bfd_link_relocatable (info)
   2273   1.8  christos 		      || ! (sym->section->flags & SEC_MERGE))
   2274   1.1  christos 		    break;
   2275   1.1  christos 		  /* FALLTHROUGH */
   2276   1.8  christos 		case discard_l:
   2277   1.1  christos 		  if (!bfd_is_local_label (input_bfd, sym))
   2278   1.1  christos 		    output = true;
   2279   1.1  christos 		  break;
   2280   1.1  christos 		case discard_none:
   2281   1.1  christos 		  output = true;
   2282   1.1  christos 		  break;
   2283   1.1  christos 		}
   2284   1.8  christos 	    }
   2285   1.1  christos 	}
   2286  1.10  christos       else if ((sym->flags & BSF_CONSTRUCTOR))
   2287   1.1  christos 	{
   2288   1.1  christos 	  if (info->strip != strip_all)
   2289  1.10  christos 	    output = true;
   2290  1.10  christos 	}
   2291  1.10  christos       else if (sym->flags == 0)
   2292   1.1  christos 	/* LTO doesn't set symbol information.  We get here with the
   2293  1.10  christos 	   generic linker for a symbol that was "common" but no longer
   2294   1.1  christos 	   needs to be global.  We also get here on fuzzed ELF objects
   2295   1.1  christos 	   with bogus symbol type and binding.  */
   2296   1.1  christos 	;
   2297   1.1  christos       else
   2298   1.8  christos 	BFD_FAIL ();
   2299   1.1  christos 
   2300   1.8  christos       if (output)
   2301   1.1  christos 	{
   2302   1.1  christos 	  if (! generic_add_output_symbol (output_bfd, psymalloc, sym))
   2303   1.1  christos 	    return false;
   2304   1.8  christos 	  if (h != NULL)
   2305   1.1  christos 	    h->written = true;
   2306   1.1  christos 	}
   2307   1.1  christos     }
   2308   1.1  christos 
   2309   1.1  christos   return true;
   2310   1.1  christos }
   2311   1.1  christos 
   2312   1.1  christos /* Set the section and value of a generic BFD symbol based on a linker
   2313   1.1  christos    hash table entry.  */
   2314   1.1  christos 
   2315   1.1  christos static void
   2316   1.1  christos set_symbol_from_hash (asymbol *sym, struct bfd_link_hash_entry *h)
   2317   1.1  christos {
   2318   1.1  christos   switch (h->type)
   2319   1.1  christos     {
   2320   1.6  christos     default:
   2321   1.1  christos       abort ();
   2322   1.1  christos       break;
   2323   1.1  christos     case bfd_link_hash_new:
   2324   1.1  christos       /* This can happen when a constructor symbol is seen but we are
   2325   1.1  christos 	 not building constructors.  */
   2326   1.1  christos       if (sym->section != NULL)
   2327   1.1  christos 	{
   2328   1.1  christos 	  BFD_ASSERT ((sym->flags & BSF_CONSTRUCTOR) != 0);
   2329   1.1  christos 	}
   2330   1.1  christos       else
   2331   1.1  christos 	{
   2332   1.1  christos 	  sym->flags |= BSF_CONSTRUCTOR;
   2333   1.1  christos 	  sym->section = bfd_abs_section_ptr;
   2334   1.1  christos 	  sym->value = 0;
   2335   1.1  christos 	}
   2336   1.1  christos       break;
   2337   1.1  christos     case bfd_link_hash_undefined:
   2338   1.1  christos       sym->section = bfd_und_section_ptr;
   2339   1.1  christos       sym->value = 0;
   2340   1.1  christos       break;
   2341   1.1  christos     case bfd_link_hash_undefweak:
   2342   1.1  christos       sym->section = bfd_und_section_ptr;
   2343   1.1  christos       sym->value = 0;
   2344   1.1  christos       sym->flags |= BSF_WEAK;
   2345   1.1  christos       break;
   2346   1.1  christos     case bfd_link_hash_defined:
   2347   1.1  christos       sym->section = h->u.def.section;
   2348   1.1  christos       sym->value = h->u.def.value;
   2349   1.1  christos       break;
   2350   1.1  christos     case bfd_link_hash_defweak:
   2351   1.1  christos       sym->flags |= BSF_WEAK;
   2352   1.1  christos       sym->section = h->u.def.section;
   2353   1.1  christos       sym->value = h->u.def.value;
   2354   1.1  christos       break;
   2355   1.1  christos     case bfd_link_hash_common:
   2356   1.1  christos       sym->value = h->u.c.size;
   2357   1.1  christos       if (sym->section == NULL)
   2358   1.1  christos 	sym->section = bfd_com_section_ptr;
   2359   1.1  christos       else if (! bfd_is_com_section (sym->section))
   2360   1.1  christos 	{
   2361   1.1  christos 	  BFD_ASSERT (bfd_is_und_section (sym->section));
   2362   1.1  christos 	  sym->section = bfd_com_section_ptr;
   2363   1.1  christos 	}
   2364   1.1  christos       /* Do not set the section; see _bfd_generic_link_output_symbols.  */
   2365   1.1  christos       break;
   2366   1.1  christos     case bfd_link_hash_indirect:
   2367   1.1  christos     case bfd_link_hash_warning:
   2368   1.1  christos       /* FIXME: What should we do here?  */
   2369   1.1  christos       break;
   2370   1.1  christos     }
   2371  1.10  christos }
   2372   1.1  christos 
   2373   1.1  christos /* Write out a global symbol, if it hasn't already been written out.
   2374   1.1  christos    This is called for each symbol in the hash table.  */
   2375  1.10  christos 
   2376   1.1  christos static bool
   2377   1.1  christos _bfd_generic_link_write_global_symbol (struct generic_link_hash_entry *h,
   2378   1.1  christos 				       void *data)
   2379   1.8  christos {
   2380   1.1  christos   struct generic_write_global_symbol_info *wginfo = data;
   2381   1.8  christos   asymbol *sym;
   2382   1.1  christos 
   2383   1.1  christos   if (h->written)
   2384   1.1  christos     return true;
   2385   1.1  christos 
   2386   1.8  christos   h->written = true;
   2387   1.8  christos 
   2388   1.1  christos   if (wginfo->info->strip == strip_all
   2389   1.1  christos       || (wginfo->info->strip == strip_some
   2390   1.1  christos 	  && bfd_hash_lookup (wginfo->info->keep_hash, h->root.root.string,
   2391   1.1  christos 			      false, false) == NULL))
   2392   1.1  christos     return true;
   2393   1.1  christos 
   2394   1.1  christos   if (h->sym != NULL)
   2395  1.10  christos     sym = h->sym;
   2396  1.10  christos   else
   2397  1.10  christos     {
   2398  1.10  christos       sym = bfd_make_empty_symbol (wginfo->output_bfd);
   2399   1.1  christos       if (!sym)
   2400   1.1  christos 	{
   2401   1.1  christos 	  wginfo->failed = true;
   2402   1.1  christos 	  return false;
   2403   1.1  christos 	}
   2404   1.1  christos       sym->name = h->root.root.string;
   2405   1.1  christos       sym->flags = 0;
   2406   1.1  christos     }
   2407   1.1  christos 
   2408   1.1  christos   set_symbol_from_hash (sym, &h->root);
   2409   1.1  christos 
   2410  1.10  christos   sym->flags |= BSF_GLOBAL;
   2411  1.10  christos 
   2412   1.1  christos   if (! generic_add_output_symbol (wginfo->output_bfd, wginfo->psymalloc,
   2413   1.1  christos 				   sym))
   2414   1.8  christos     {
   2415   1.1  christos       wginfo->failed = true;
   2416   1.1  christos       return false;
   2417   1.1  christos     }
   2418   1.1  christos 
   2419   1.8  christos   return true;
   2420   1.1  christos }
   2421   1.1  christos 
   2422   1.1  christos /* Create a relocation.  */
   2423   1.1  christos 
   2424   1.1  christos bool
   2425   1.1  christos _bfd_generic_reloc_link_order (bfd *abfd,
   2426   1.1  christos 			       struct bfd_link_info *info,
   2427   1.3  christos 			       asection *sec,
   2428   1.1  christos 			       struct bfd_link_order *link_order)
   2429   1.1  christos {
   2430   1.1  christos   arelent *r;
   2431   1.1  christos 
   2432   1.1  christos   if (! bfd_link_relocatable (info))
   2433   1.1  christos     abort ();
   2434   1.8  christos   if (sec->orelocation == NULL)
   2435   1.1  christos     abort ();
   2436   1.1  christos 
   2437   1.1  christos   r = (arelent *) bfd_alloc (abfd, sizeof (arelent));
   2438   1.1  christos   if (r == NULL)
   2439   1.1  christos     return false;
   2440   1.1  christos 
   2441   1.8  christos   r->address = link_order->offset;
   2442   1.1  christos   r->howto = bfd_reloc_type_lookup (abfd, link_order->u.reloc.p->reloc);
   2443   1.1  christos   if (r->howto == 0)
   2444   1.1  christos     {
   2445   1.1  christos       bfd_set_error (bfd_error_bad_value);
   2446  1.10  christos       return false;
   2447   1.1  christos     }
   2448   1.1  christos 
   2449   1.1  christos   /* Get the symbol to use for the relocation.  */
   2450   1.1  christos   if (link_order->type == bfd_section_reloc_link_order)
   2451   1.1  christos     r->sym_ptr_ptr = &link_order->u.reloc.p->u.section->symbol;
   2452   1.1  christos   else
   2453   1.1  christos     {
   2454   1.8  christos       struct generic_link_hash_entry *h;
   2455   1.1  christos 
   2456   1.1  christos       h = ((struct generic_link_hash_entry *)
   2457   1.1  christos 	   bfd_wrapped_link_hash_lookup (abfd, info,
   2458   1.5  christos 					 link_order->u.reloc.p->u.name,
   2459   1.5  christos 					 false, false, true));
   2460   1.1  christos       if (h == NULL
   2461   1.8  christos 	  || ! h->written)
   2462   1.1  christos 	{
   2463   1.1  christos 	  (*info->callbacks->unattached_reloc)
   2464   1.1  christos 	    (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
   2465   1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   2466   1.1  christos 	  return false;
   2467   1.1  christos 	}
   2468   1.1  christos       r->sym_ptr_ptr = &h->sym;
   2469   1.1  christos     }
   2470   1.1  christos 
   2471   1.1  christos   /* If this is an inplace reloc, write the addend to the object file.
   2472   1.1  christos      Otherwise, store it in the reloc addend.  */
   2473   1.1  christos   if (! r->howto->partial_inplace)
   2474   1.1  christos     r->addend = link_order->u.reloc.p->addend;
   2475   1.8  christos   else
   2476   1.1  christos     {
   2477   1.1  christos       bfd_size_type size;
   2478   1.1  christos       bfd_reloc_status_type rstat;
   2479   1.1  christos       bfd_byte *buf;
   2480   1.3  christos       bool ok;
   2481   1.8  christos       file_ptr loc;
   2482   1.1  christos 
   2483   1.1  christos       size = bfd_get_reloc_size (r->howto);
   2484   1.1  christos       buf = (bfd_byte *) bfd_zmalloc (size);
   2485   1.1  christos       if (buf == NULL && size != 0)
   2486   1.1  christos 	return false;
   2487   1.1  christos       rstat = _bfd_relocate_contents (r->howto, abfd,
   2488   1.1  christos 				      (bfd_vma) link_order->u.reloc.p->addend,
   2489   1.1  christos 				      buf);
   2490   1.1  christos       switch (rstat)
   2491   1.1  christos 	{
   2492   1.1  christos 	case bfd_reloc_ok:
   2493   1.5  christos 	  break;
   2494   1.5  christos 	default:
   2495   1.5  christos 	case bfd_reloc_outofrange:
   2496   1.7  christos 	  abort ();
   2497   1.5  christos 	case bfd_reloc_overflow:
   2498   1.5  christos 	  (*info->callbacks->reloc_overflow)
   2499   1.5  christos 	    (info, NULL,
   2500   1.1  christos 	     (link_order->type == bfd_section_reloc_link_order
   2501   1.1  christos 	      ? bfd_section_name (link_order->u.reloc.p->u.section)
   2502   1.7  christos 	      : link_order->u.reloc.p->u.name),
   2503   1.1  christos 	     r->howto->name, link_order->u.reloc.p->addend,
   2504   1.1  christos 	     NULL, NULL, 0);
   2505   1.1  christos 	  break;
   2506   1.8  christos 	}
   2507   1.1  christos       loc = link_order->offset * bfd_octets_per_byte (abfd, sec);
   2508   1.1  christos       ok = bfd_set_section_contents (abfd, sec, buf, loc, size);
   2509   1.1  christos       free (buf);
   2510   1.1  christos       if (! ok)
   2511   1.1  christos 	return false;
   2512   1.1  christos 
   2513   1.1  christos       r->addend = 0;
   2514   1.8  christos     }
   2515   1.1  christos 
   2516   1.1  christos   sec->orelocation[sec->reloc_count] = r;
   2517   1.1  christos   ++sec->reloc_count;
   2518   1.1  christos 
   2519   1.1  christos   return true;
   2520   1.1  christos }
   2521   1.1  christos 
   2522   1.8  christos /* Allocate a new link_order for a section.  */
   2524   1.1  christos 
   2525   1.1  christos struct bfd_link_order *
   2526   1.1  christos bfd_new_link_order (bfd *abfd, asection *section)
   2527   1.1  christos {
   2528   1.1  christos   size_t amt = sizeof (struct bfd_link_order);
   2529   1.1  christos   struct bfd_link_order *new_lo;
   2530   1.1  christos 
   2531   1.1  christos   new_lo = (struct bfd_link_order *) bfd_zalloc (abfd, amt);
   2532   1.1  christos   if (!new_lo)
   2533   1.1  christos     return NULL;
   2534   1.1  christos 
   2535   1.1  christos   new_lo->type = bfd_undefined_link_order;
   2536   1.1  christos 
   2537   1.1  christos   if (section->map_tail.link_order != NULL)
   2538   1.1  christos     section->map_tail.link_order->next = new_lo;
   2539   1.1  christos   else
   2540   1.1  christos     section->map_head.link_order = new_lo;
   2541   1.1  christos   section->map_tail.link_order = new_lo;
   2542   1.1  christos 
   2543   1.1  christos   return new_lo;
   2544   1.8  christos }
   2545   1.1  christos 
   2546   1.1  christos /* Default link order processing routine.  Note that we can not handle
   2547   1.1  christos    the reloc_link_order types here, since they depend upon the details
   2548   1.1  christos    of how the particular backends generates relocs.  */
   2549   1.1  christos 
   2550   1.1  christos bool
   2551   1.1  christos _bfd_default_link_order (bfd *abfd,
   2552   1.1  christos 			 struct bfd_link_info *info,
   2553   1.1  christos 			 asection *sec,
   2554   1.1  christos 			 struct bfd_link_order *link_order)
   2555   1.1  christos {
   2556   1.1  christos   switch (link_order->type)
   2557   1.1  christos     {
   2558   1.1  christos     case bfd_undefined_link_order:
   2559   1.8  christos     case bfd_section_reloc_link_order:
   2560   1.1  christos     case bfd_symbol_reloc_link_order:
   2561   1.1  christos     default:
   2562   1.1  christos       abort ();
   2563   1.1  christos     case bfd_indirect_link_order:
   2564   1.1  christos       return default_indirect_link_order (abfd, info, sec, link_order,
   2565   1.1  christos 					  false);
   2566   1.1  christos     case bfd_data_link_order:
   2567   1.8  christos       return default_data_link_order (abfd, info, sec, link_order);
   2568   1.1  christos     }
   2569   1.7  christos }
   2570   1.1  christos 
   2571   1.1  christos /* Default routine to handle a bfd_data_link_order.  */
   2572   1.1  christos 
   2573   1.1  christos static bool
   2574   1.1  christos default_data_link_order (bfd *abfd,
   2575   1.1  christos 			 struct bfd_link_info *info,
   2576   1.1  christos 			 asection *sec,
   2577   1.8  christos 			 struct bfd_link_order *link_order)
   2578   1.1  christos {
   2579   1.1  christos   bfd_size_type size;
   2580   1.1  christos   size_t fill_size;
   2581   1.1  christos   bfd_byte *fill;
   2582   1.1  christos   file_ptr loc;
   2583   1.8  christos   bool result;
   2584   1.1  christos 
   2585   1.1  christos   BFD_ASSERT ((sec->flags & SEC_HAS_CONTENTS) != 0);
   2586   1.1  christos 
   2587   1.1  christos   size = link_order->size;
   2588   1.1  christos   if (size == 0)
   2589   1.7  christos     return true;
   2590   1.1  christos 
   2591   1.1  christos   fill = link_order->u.data.contents;
   2592   1.8  christos   fill_size = link_order->u.data.size;
   2593   1.1  christos   if (fill_size == 0)
   2594   1.1  christos     {
   2595   1.1  christos       fill = abfd->arch_info->fill (size, info->big_endian,
   2596   1.1  christos 				    (sec->flags & SEC_CODE) != 0);
   2597   1.1  christos       if (fill == NULL)
   2598   1.1  christos 	return false;
   2599   1.8  christos     }
   2600   1.1  christos   else if (fill_size < size)
   2601   1.1  christos     {
   2602   1.1  christos       bfd_byte *p;
   2603   1.1  christos       fill = (bfd_byte *) bfd_malloc (size);
   2604   1.1  christos       if (fill == NULL)
   2605   1.1  christos 	return false;
   2606   1.1  christos       p = fill;
   2607   1.1  christos       if (fill_size == 1)
   2608   1.1  christos 	memset (p, (int) link_order->u.data.contents[0], (size_t) size);
   2609   1.1  christos       else
   2610   1.1  christos 	{
   2611   1.1  christos 	  do
   2612   1.1  christos 	    {
   2613   1.1  christos 	      memcpy (p, link_order->u.data.contents, fill_size);
   2614   1.1  christos 	      p += fill_size;
   2615   1.1  christos 	      size -= fill_size;
   2616   1.1  christos 	    }
   2617   1.1  christos 	  while (size >= fill_size);
   2618   1.7  christos 	  if (size != 0)
   2619   1.1  christos 	    memcpy (p, link_order->u.data.contents, (size_t) size);
   2620   1.1  christos 	  size = link_order->size;
   2621   1.1  christos 	}
   2622   1.1  christos     }
   2623   1.1  christos 
   2624   1.1  christos   loc = link_order->offset * bfd_octets_per_byte (abfd, sec);
   2625   1.1  christos   result = bfd_set_section_contents (abfd, sec, fill, loc, size);
   2626   1.1  christos 
   2627   1.1  christos   if (fill != link_order->u.data.contents)
   2628   1.8  christos     free (fill);
   2629   1.1  christos   return result;
   2630   1.1  christos }
   2631   1.1  christos 
   2632   1.1  christos /* Default routine to handle a bfd_indirect_link_order.  */
   2633   1.8  christos 
   2634   1.1  christos static bool
   2635   1.1  christos default_indirect_link_order (bfd *output_bfd,
   2636   1.1  christos 			     struct bfd_link_info *info,
   2637   1.9  christos 			     asection *output_section,
   2638   1.1  christos 			     struct bfd_link_order *link_order,
   2639   1.1  christos 			     bool generic_linker)
   2640   1.1  christos {
   2641   1.1  christos   asection *input_section;
   2642   1.1  christos   bfd *input_bfd;
   2643   1.1  christos   bfd_byte *alloced = NULL;
   2644   1.1  christos   bfd_byte *new_contents;
   2645   1.1  christos   file_ptr loc;
   2646   1.8  christos 
   2647   1.1  christos   BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
   2648   1.1  christos 
   2649   1.1  christos   input_section = link_order->u.indirect.section;
   2650   1.1  christos   input_bfd = input_section->owner;
   2651   1.1  christos   if (input_section->size == 0)
   2652   1.3  christos     return true;
   2653   1.1  christos 
   2654   1.1  christos   BFD_ASSERT (input_section->output_section == output_section);
   2655   1.1  christos   BFD_ASSERT (input_section->output_offset == link_order->offset);
   2656   1.1  christos   BFD_ASSERT (input_section->size == link_order->size);
   2657   1.1  christos 
   2658   1.1  christos   if (bfd_link_relocatable (info)
   2659   1.1  christos       && input_section->reloc_count > 0
   2660   1.1  christos       && output_section->orelocation == NULL)
   2661   1.6  christos     {
   2662   1.6  christos       /* Space has not been allocated for the output relocations.
   2663   1.6  christos 	 This can happen when we are called by a specific backend
   2664   1.1  christos 	 because somebody is attempting to link together different
   2665   1.1  christos 	 types of object files.  Handling this case correctly is
   2666   1.8  christos 	 difficult, and sometimes impossible.  */
   2667   1.1  christos       _bfd_error_handler
   2668   1.1  christos 	/* xgettext:c-format */
   2669   1.1  christos 	(_("attempt to do relocatable link with %s input and %s output"),
   2670   1.1  christos 	 bfd_get_target (input_bfd), bfd_get_target (output_bfd));
   2671   1.1  christos       bfd_set_error (bfd_error_wrong_format);
   2672   1.1  christos       return false;
   2673   1.1  christos     }
   2674   1.1  christos 
   2675   1.1  christos   if (! generic_linker)
   2676   1.1  christos     {
   2677   1.1  christos       asymbol **sympp;
   2678   1.1  christos       asymbol **symppend;
   2679   1.8  christos 
   2680   1.1  christos       /* Get the canonical symbols.  The generic linker will always
   2681   1.1  christos 	 have retrieved them by this point, but we are being called by
   2682   1.1  christos 	 a specific linker, presumably because we are linking
   2683   1.1  christos 	 different types of object files together.  */
   2684   1.1  christos       if (!bfd_generic_link_read_symbols (input_bfd))
   2685   1.1  christos 	return false;
   2686   1.1  christos 
   2687   1.1  christos       /* Since we have been called by a specific linker, rather than
   2688   1.1  christos 	 the generic linker, the values of the symbols will not be
   2689   1.1  christos 	 right.  They will be the values as seen in the input file,
   2690   1.1  christos 	 not the values of the final link.  We need to fix them up
   2691   1.1  christos 	 before we can relocate the section.  */
   2692   1.1  christos       sympp = _bfd_generic_link_get_symbols (input_bfd);
   2693   1.1  christos       symppend = sympp + _bfd_generic_link_get_symcount (input_bfd);
   2694   1.1  christos       for (; sympp < symppend; sympp++)
   2695   1.1  christos 	{
   2696   1.1  christos 	  asymbol *sym;
   2697   1.1  christos 	  struct bfd_link_hash_entry *h;
   2698   1.1  christos 
   2699   1.1  christos 	  sym = *sympp;
   2700   1.7  christos 
   2701   1.7  christos 	  if ((sym->flags & (BSF_INDIRECT
   2702   1.7  christos 			     | BSF_WARNING
   2703   1.1  christos 			     | BSF_GLOBAL
   2704   1.1  christos 			     | BSF_CONSTRUCTOR
   2705   1.1  christos 			     | BSF_WEAK)) != 0
   2706   1.1  christos 	      || bfd_is_und_section (bfd_asymbol_section (sym))
   2707   1.1  christos 	      || bfd_is_com_section (bfd_asymbol_section (sym))
   2708   1.7  christos 	      || bfd_is_ind_section (bfd_asymbol_section (sym)))
   2709   1.1  christos 	    {
   2710   1.1  christos 	      /* sym->udata may have been set by
   2711   1.8  christos 		 generic_link_add_symbol_list.  */
   2712   1.1  christos 	      if (sym->udata.p != NULL)
   2713   1.1  christos 		h = (struct bfd_link_hash_entry *) sym->udata.p;
   2714   1.1  christos 	      else if (bfd_is_und_section (bfd_asymbol_section (sym)))
   2715   1.8  christos 		h = bfd_wrapped_link_hash_lookup (output_bfd, info,
   2716   1.1  christos 						  bfd_asymbol_name (sym),
   2717   1.1  christos 						  false, false, true);
   2718   1.1  christos 	      else
   2719   1.1  christos 		h = bfd_link_hash_lookup (info->hash,
   2720   1.1  christos 					  bfd_asymbol_name (sym),
   2721   1.1  christos 					  false, false, true);
   2722   1.1  christos 	      if (h != NULL)
   2723   1.1  christos 		set_symbol_from_hash (sym, h);
   2724   1.1  christos 	    }
   2725   1.1  christos 	}
   2726   1.1  christos     }
   2727   1.1  christos 
   2728   1.1  christos   if ((output_section->flags & (SEC_GROUP | SEC_LINKER_CREATED)) == SEC_GROUP
   2729   1.1  christos       && input_section->size != 0)
   2730   1.1  christos     {
   2731   1.1  christos       /* Group section contents are set by bfd_elf_set_group_contents.  */
   2732   1.1  christos       if (!output_bfd->output_has_begun)
   2733   1.1  christos 	{
   2734   1.1  christos 	  /* FIXME: This hack ensures bfd_elf_set_group_contents is called.  */
   2735   1.1  christos 	  if (!bfd_set_section_contents (output_bfd, output_section, "", 0, 1))
   2736   1.1  christos 	    goto error_return;
   2737   1.1  christos 	}
   2738   1.1  christos       new_contents = output_section->contents;
   2739   1.1  christos       BFD_ASSERT (new_contents != NULL);
   2740   1.9  christos       BFD_ASSERT (input_section->output_offset == 0);
   2741   1.3  christos     }
   2742   1.1  christos   else
   2743   1.9  christos     {
   2744   1.1  christos       /* Get and relocate the section contents.  */
   2745   1.1  christos       new_contents = (bfd_get_relocated_section_contents
   2746   1.1  christos 		      (output_bfd, info, link_order, NULL,
   2747   1.1  christos 		       bfd_link_relocatable (info),
   2748   1.1  christos 		       _bfd_generic_link_get_symbols (input_bfd)));
   2749   1.7  christos       alloced = new_contents;
   2750   1.7  christos       if (!new_contents)
   2751   1.1  christos 	goto error_return;
   2752   1.1  christos     }
   2753   1.1  christos 
   2754   1.1  christos   /* Output the section contents.  */
   2755   1.9  christos   loc = (input_section->output_offset
   2756   1.8  christos 	 * bfd_octets_per_byte (output_bfd, output_section));
   2757   1.1  christos   if (! bfd_set_section_contents (output_bfd, output_section,
   2758   1.1  christos 				  new_contents, loc, input_section->size))
   2759   1.9  christos     goto error_return;
   2760   1.8  christos 
   2761   1.1  christos   free (alloced);
   2762   1.1  christos   return true;
   2763   1.1  christos 
   2764   1.1  christos  error_return:
   2765   1.1  christos   free (alloced);
   2766   1.1  christos   return false;
   2767   1.1  christos }
   2768   1.1  christos 
   2769   1.1  christos /* A little routine to count the number of relocs in a link_order
   2770   1.1  christos    list.  */
   2771   1.1  christos 
   2772   1.1  christos unsigned int
   2773   1.1  christos _bfd_count_link_order_relocs (struct bfd_link_order *link_order)
   2774   1.1  christos {
   2775   1.1  christos   register unsigned int c;
   2776   1.1  christos   register struct bfd_link_order *l;
   2777   1.1  christos 
   2778   1.1  christos   c = 0;
   2779   1.1  christos   for (l = link_order; l != NULL; l = l->next)
   2780   1.1  christos     {
   2781   1.1  christos       if (l->type == bfd_section_reloc_link_order
   2782   1.1  christos 	  || l->type == bfd_symbol_reloc_link_order)
   2783   1.1  christos 	++c;
   2784   1.1  christos     }
   2785   1.1  christos 
   2786   1.1  christos   return c;
   2787   1.1  christos }
   2788   1.8  christos 
   2789   1.1  christos /*
   2790   1.1  christos FUNCTION
   2791   1.1  christos 	bfd_link_split_section
   2792   1.1  christos 
   2793   1.1  christos SYNOPSIS
   2794   1.1  christos 	bool bfd_link_split_section (bfd *abfd, asection *sec);
   2795   1.6  christos 
   2796   1.1  christos DESCRIPTION
   2797   1.1  christos 	Return nonzero if @var{sec} should be split during a
   2798   1.1  christos 	reloceatable or final link.
   2799   1.1  christos 
   2800   1.8  christos .#define bfd_link_split_section(abfd, sec) \
   2801   1.1  christos .	BFD_SEND (abfd, _bfd_link_split_section, (abfd, sec))
   2802   1.1  christos .
   2803   1.1  christos 
   2804   1.8  christos */
   2805   1.1  christos 
   2806   1.1  christos bool
   2807   1.1  christos _bfd_generic_link_split_section (bfd *abfd ATTRIBUTE_UNUSED,
   2808   1.1  christos 				 asection *sec ATTRIBUTE_UNUSED)
   2809   1.1  christos {
   2810   1.1  christos   return false;
   2811   1.1  christos }
   2812   1.8  christos 
   2813   1.8  christos /*
   2814   1.8  christos FUNCTION
   2815   1.1  christos 	bfd_section_already_linked
   2816   1.1  christos 
   2817   1.1  christos SYNOPSIS
   2818   1.1  christos 	bool bfd_section_already_linked (bfd *abfd,
   2819   1.1  christos 					 asection *sec,
   2820   1.1  christos 					 struct bfd_link_info *info);
   2821   1.6  christos 
   2822   1.1  christos DESCRIPTION
   2823   1.1  christos 	Check if @var{data} has been already linked during a reloceatable
   2824   1.1  christos 	or final link.  Return TRUE if it has.
   2825   1.1  christos 
   2826   1.1  christos .#define bfd_section_already_linked(abfd, sec, info) \
   2827   1.1  christos .	BFD_SEND (abfd, _section_already_linked, (abfd, sec, info))
   2828   1.1  christos .
   2829   1.3  christos 
   2830   1.1  christos */
   2831   1.1  christos 
   2832   1.1  christos /* Sections marked with the SEC_LINK_ONCE flag should only be linked
   2833   1.1  christos    once into the output.  This routine checks each section, and
   2834   1.1  christos    arrange to discard it if a section of the same name has already
   2835   1.1  christos    been linked.  This code assumes that all relevant sections have the
   2836   1.1  christos    SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
   2837   1.1  christos    section name.  bfd_section_already_linked is called via
   2838   1.1  christos    bfd_map_over_sections.  */
   2839   1.1  christos 
   2840   1.1  christos /* The hash table.  */
   2841   1.1  christos 
   2842   1.1  christos static struct bfd_hash_table _bfd_section_already_linked_table;
   2843   1.1  christos 
   2844   1.8  christos /* Support routines for the hash table used by section_already_linked,
   2845   1.8  christos    initialize the table, traverse, lookup, fill in an entry and remove
   2846   1.1  christos    the table.  */
   2847   1.1  christos 
   2848   1.8  christos void
   2849   1.1  christos bfd_section_already_linked_table_traverse
   2850   1.1  christos   (bool (*func) (struct bfd_section_already_linked_hash_entry *, void *),
   2851   1.1  christos    void *info)
   2852   1.1  christos {
   2853   1.1  christos   bfd_hash_traverse (&_bfd_section_already_linked_table,
   2854   1.1  christos 		     (bool (*) (struct bfd_hash_entry *, void *)) func,
   2855   1.1  christos 		     info);
   2856   1.1  christos }
   2857   1.8  christos 
   2858   1.1  christos struct bfd_section_already_linked_hash_entry *
   2859   1.1  christos bfd_section_already_linked_table_lookup (const char *name)
   2860   1.8  christos {
   2861   1.1  christos   return ((struct bfd_section_already_linked_hash_entry *)
   2862   1.1  christos 	  bfd_hash_lookup (&_bfd_section_already_linked_table, name,
   2863   1.1  christos 			   true, false));
   2864   1.1  christos }
   2865   1.1  christos 
   2866   1.1  christos bool
   2867   1.1  christos bfd_section_already_linked_table_insert
   2868   1.1  christos   (struct bfd_section_already_linked_hash_entry *already_linked_list,
   2869   1.1  christos    asection *sec)
   2870   1.1  christos {
   2871   1.1  christos   struct bfd_section_already_linked *l;
   2872   1.8  christos 
   2873   1.1  christos   /* Allocate the memory from the same obstack as the hash table is
   2874   1.1  christos      kept in.  */
   2875   1.1  christos   l = (struct bfd_section_already_linked *)
   2876   1.8  christos       bfd_hash_allocate (&_bfd_section_already_linked_table, sizeof *l);
   2877   1.1  christos   if (l == NULL)
   2878   1.1  christos     return false;
   2879   1.1  christos   l->sec = sec;
   2880   1.1  christos   l->next = already_linked_list->entry;
   2881   1.1  christos   already_linked_list->entry = l;
   2882   1.1  christos   return true;
   2883   1.1  christos }
   2884   1.1  christos 
   2885   1.1  christos static struct bfd_hash_entry *
   2886   1.1  christos already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
   2887   1.1  christos 			struct bfd_hash_table *table,
   2888   1.1  christos 			const char *string ATTRIBUTE_UNUSED)
   2889   1.1  christos {
   2890   1.1  christos   struct bfd_section_already_linked_hash_entry *ret =
   2891   1.1  christos     (struct bfd_section_already_linked_hash_entry *)
   2892   1.1  christos       bfd_hash_allocate (table, sizeof *ret);
   2893   1.1  christos 
   2894   1.1  christos   if (ret == NULL)
   2895   1.1  christos     return NULL;
   2896   1.8  christos 
   2897   1.1  christos   ret->entry = NULL;
   2898   1.1  christos 
   2899   1.1  christos   return &ret->root;
   2900   1.1  christos }
   2901   1.1  christos 
   2902   1.1  christos bool
   2903   1.1  christos bfd_section_already_linked_table_init (void)
   2904   1.1  christos {
   2905   1.1  christos   return bfd_hash_table_init_n (&_bfd_section_already_linked_table,
   2906   1.1  christos 				already_linked_newfunc,
   2907   1.1  christos 				sizeof (struct bfd_section_already_linked_hash_entry),
   2908   1.1  christos 				42);
   2909   1.1  christos }
   2910   1.1  christos 
   2911   1.1  christos void
   2912   1.1  christos bfd_section_already_linked_table_free (void)
   2913   1.1  christos {
   2914   1.8  christos   bfd_hash_table_free (&_bfd_section_already_linked_table);
   2915   1.1  christos }
   2916   1.1  christos 
   2917   1.1  christos /* Report warnings as appropriate for duplicate section SEC.
   2918   1.1  christos    Return FALSE if we decide to keep SEC after all.  */
   2919   1.1  christos 
   2920   1.1  christos bool
   2921   1.1  christos _bfd_handle_already_linked (asection *sec,
   2922   1.1  christos 			    struct bfd_section_already_linked *l,
   2923   1.1  christos 			    struct bfd_link_info *info)
   2924   1.1  christos {
   2925   1.1  christos   switch (sec->flags & SEC_LINK_DUPLICATES)
   2926   1.1  christos     {
   2927   1.1  christos     default:
   2928   1.1  christos       abort ();
   2929   1.1  christos 
   2930   1.1  christos     case SEC_LINK_DUPLICATES_DISCARD:
   2931   1.3  christos       /* If we found an LTO IR match for this comdat group on
   2932   1.1  christos 	 the first pass, replace it with the LTO output on the
   2933   1.1  christos 	 second pass.  We can't simply choose real object
   2934   1.1  christos 	 files over IR because the first pass may contain a
   2935   1.8  christos 	 mix of LTO and normal objects and we must keep the
   2936   1.1  christos 	 first match, be it IR or real.  */
   2937   1.1  christos       if (sec->owner->lto_output
   2938   1.1  christos 	  && (l->sec->owner->flags & BFD_PLUGIN) != 0)
   2939   1.1  christos 	{
   2940   1.1  christos 	  l->sec = sec;
   2941   1.6  christos 	  return false;
   2942   1.6  christos 	}
   2943   1.1  christos       break;
   2944   1.1  christos 
   2945   1.1  christos     case SEC_LINK_DUPLICATES_ONE_ONLY:
   2946   1.1  christos       info->callbacks->einfo
   2947   1.1  christos 	/* xgettext:c-format */
   2948   1.1  christos 	(_("%pB: ignoring duplicate section `%pA'\n"),
   2949   1.1  christos 	 sec->owner, sec);
   2950   1.1  christos       break;
   2951   1.6  christos 
   2952   1.6  christos     case SEC_LINK_DUPLICATES_SAME_SIZE:
   2953   1.1  christos       if ((l->sec->owner->flags & BFD_PLUGIN) != 0)
   2954   1.1  christos 	;
   2955   1.1  christos       else if (sec->size != l->sec->size)
   2956   1.1  christos 	info->callbacks->einfo
   2957   1.1  christos 	  /* xgettext:c-format */
   2958   1.1  christos 	  (_("%pB: duplicate section `%pA' has different size\n"),
   2959   1.1  christos 	   sec->owner, sec);
   2960   1.1  christos       break;
   2961   1.6  christos 
   2962   1.6  christos     case SEC_LINK_DUPLICATES_SAME_CONTENTS:
   2963   1.1  christos       if ((l->sec->owner->flags & BFD_PLUGIN) != 0)
   2964   1.1  christos 	;
   2965   1.1  christos       else if (sec->size != l->sec->size)
   2966   1.9  christos 	info->callbacks->einfo
   2967   1.1  christos 	  /* xgettext:c-format */
   2968   1.9  christos 	  (_("%pB: duplicate section `%pA' has different size\n"),
   2969   1.9  christos 	   sec->owner, sec);
   2970   1.9  christos       else if (sec->size != 0)
   2971   1.9  christos 	{
   2972   1.9  christos 	  bfd_byte *sec_contents, *l_sec_contents;
   2973   1.9  christos 
   2974   1.1  christos 	  if ((sec->flags & SEC_HAS_CONTENTS) == 0
   2975   1.6  christos 	      && (l->sec->flags & SEC_HAS_CONTENTS) == 0)
   2976   1.6  christos 	    ;
   2977   1.1  christos 	  else if ((sec->flags & SEC_HAS_CONTENTS) == 0
   2978   1.9  christos 		   || !bfd_malloc_and_get_section (sec->owner, sec,
   2979   1.9  christos 						   &sec_contents))
   2980   1.9  christos 	    info->callbacks->einfo
   2981   1.9  christos 	      /* xgettext:c-format */
   2982   1.9  christos 	      (_("%pB: could not read contents of section `%pA'\n"),
   2983   1.9  christos 	       sec->owner, sec);
   2984   1.9  christos 	  else if ((l->sec->flags & SEC_HAS_CONTENTS) == 0
   2985   1.9  christos 		   || !bfd_malloc_and_get_section (l->sec->owner, l->sec,
   2986   1.9  christos 						   &l_sec_contents))
   2987   1.9  christos 	    {
   2988   1.9  christos 	      info->callbacks->einfo
   2989   1.9  christos 		/* xgettext:c-format */
   2990   1.9  christos 		(_("%pB: could not read contents of section `%pA'\n"),
   2991   1.9  christos 		 l->sec->owner, l->sec);
   2992   1.9  christos 	      free (sec_contents);
   2993   1.9  christos 	    }
   2994   1.9  christos 	  else
   2995   1.9  christos 	    {
   2996   1.9  christos 	      if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
   2997   1.9  christos 		info->callbacks->einfo
   2998   1.1  christos 		  /* xgettext:c-format */
   2999   1.1  christos 		  (_("%pB: duplicate section `%pA' has different contents\n"),
   3000   1.1  christos 		   sec->owner, sec);
   3001   1.1  christos 	      free (l_sec_contents);
   3002   1.1  christos 	      free (sec_contents);
   3003   1.1  christos 	    }
   3004   1.1  christos 	}
   3005   1.1  christos       break;
   3006   1.1  christos     }
   3007   1.1  christos 
   3008   1.1  christos   /* Set the output_section field so that lang_add_section
   3009   1.8  christos      does not create a lang_input_section structure for this
   3010   1.1  christos      section.  Since there might be a symbol in the section
   3011   1.1  christos      being discarded, we must retain a pointer to the section
   3012   1.1  christos      which we are really going to use.  */
   3013   1.1  christos   sec->output_section = bfd_abs_section_ptr;
   3014   1.8  christos   sec->kept_section = l->sec;
   3015   1.1  christos   return true;
   3016   1.1  christos }
   3017   1.1  christos 
   3018   1.1  christos /* This is used on non-ELF inputs.  */
   3019   1.1  christos 
   3020   1.1  christos bool
   3021   1.1  christos _bfd_generic_section_already_linked (bfd *abfd ATTRIBUTE_UNUSED,
   3022   1.1  christos 				     asection *sec,
   3023   1.1  christos 				     struct bfd_link_info *info)
   3024   1.8  christos {
   3025   1.1  christos   const char *name;
   3026   1.1  christos   struct bfd_section_already_linked *l;
   3027   1.1  christos   struct bfd_section_already_linked_hash_entry *already_linked_list;
   3028   1.8  christos 
   3029   1.1  christos   if ((sec->flags & SEC_LINK_ONCE) == 0)
   3030   1.1  christos     return false;
   3031   1.1  christos 
   3032   1.1  christos   /* The generic linker doesn't handle section groups.  */
   3033   1.1  christos   if ((sec->flags & SEC_GROUP) != 0)
   3034   1.1  christos     return false;
   3035   1.1  christos 
   3036   1.1  christos   /* FIXME: When doing a relocatable link, we may have trouble
   3037   1.1  christos      copying relocations in other sections that refer to local symbols
   3038   1.1  christos      in the section being discarded.  Those relocations will have to
   3039   1.1  christos      be converted somehow; as of this writing I'm not sure that any of
   3040   1.1  christos      the backends handle that correctly.
   3041   1.1  christos 
   3042   1.1  christos      It is tempting to instead not discard link once sections when
   3043   1.7  christos      doing a relocatable link (technically, they should be discarded
   3044   1.1  christos      whenever we are building constructors).  However, that fails,
   3045   1.1  christos      because the linker winds up combining all the link once sections
   3046  1.10  christos      into a single large link once section, which defeats the purpose
   3047  1.10  christos      of having link once sections in the first place.  */
   3048   1.1  christos 
   3049   1.1  christos   name = bfd_section_name (sec);
   3050   1.1  christos 
   3051   1.1  christos   already_linked_list = bfd_section_already_linked_table_lookup (name);
   3052   1.1  christos   if (!already_linked_list)
   3053   1.1  christos     goto bad;
   3054   1.1  christos 
   3055   1.1  christos   l = already_linked_list->entry;
   3056   1.1  christos   if (l != NULL)
   3057   1.1  christos     {
   3058   1.1  christos       /* The section has already been linked.  See if we should
   3059  1.10  christos 	 issue a warning.  */
   3060  1.10  christos       return _bfd_handle_already_linked (sec, l, info);
   3061  1.10  christos     }
   3062  1.10  christos 
   3063   1.8  christos   /* This is the first section with this name.  Record it.  */
   3064   1.1  christos   if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
   3065   1.1  christos     {
   3066   1.1  christos     bad:
   3067   1.1  christos       info->callbacks->fatal (_("%P: already_linked_table: %E\n"));
   3068   1.1  christos     }
   3069   1.1  christos   return false;
   3070   1.1  christos }
   3071   1.1  christos 
   3072   1.1  christos /* Choose a neighbouring section to S in OBFD that will be output, or
   3073   1.1  christos    the absolute section if ADDR is out of bounds of the neighbours.  */
   3074   1.1  christos 
   3075   1.1  christos asection *
   3076   1.1  christos _bfd_nearby_section (bfd *obfd, asection *s, bfd_vma addr)
   3077   1.1  christos {
   3078   1.1  christos   asection *next, *prev, *best;
   3079   1.1  christos 
   3080   1.1  christos   /* Find preceding kept section.  */
   3081   1.1  christos   for (prev = s->prev; prev != NULL; prev = prev->prev)
   3082   1.1  christos     if ((prev->flags & SEC_EXCLUDE) == 0
   3083   1.1  christos 	&& !bfd_section_removed_from_list (obfd, prev))
   3084   1.1  christos       break;
   3085   1.1  christos 
   3086   1.1  christos   /* Find following kept section.  Start at prev->next because
   3087   1.1  christos      other sections may have been added after S was removed.  */
   3088   1.1  christos   if (s->prev != NULL)
   3089   1.1  christos     next = s->prev->next;
   3090   1.1  christos   else
   3091   1.1  christos     next = s->owner->sections;
   3092   1.1  christos   for (; next != NULL; next = next->next)
   3093   1.1  christos     if ((next->flags & SEC_EXCLUDE) == 0
   3094   1.1  christos 	&& !bfd_section_removed_from_list (obfd, next))
   3095   1.1  christos       break;
   3096   1.1  christos 
   3097   1.1  christos   /* Choose better of two sections, based on flags.  The idea
   3098   1.1  christos      is to choose a section that will be in the same segment
   3099   1.1  christos      as S would have been if it was kept.  */
   3100   1.1  christos   best = next;
   3101   1.1  christos   if (prev == NULL)
   3102   1.1  christos     {
   3103   1.1  christos       if (next == NULL)
   3104   1.1  christos 	best = bfd_abs_section_ptr;
   3105   1.1  christos     }
   3106   1.1  christos   else if (next == NULL)
   3107   1.1  christos     best = prev;
   3108   1.1  christos   else if (((prev->flags ^ next->flags)
   3109   1.1  christos 	    & (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_LOAD)) != 0)
   3110   1.1  christos     {
   3111   1.1  christos       if (((next->flags ^ s->flags)
   3112   1.1  christos 	   & (SEC_ALLOC | SEC_THREAD_LOCAL)) != 0
   3113   1.1  christos 	  /* We prefer to choose a loaded section.  Section S
   3114   1.1  christos 	     doesn't have SEC_LOAD set (it being excluded, that
   3115   1.1  christos 	     part of the flag processing didn't happen) so we
   3116   1.1  christos 	     can't compare that flag to those of NEXT and PREV.  */
   3117   1.1  christos 	  || ((prev->flags & SEC_LOAD) != 0
   3118   1.1  christos 	      && (next->flags & SEC_LOAD) == 0))
   3119   1.1  christos 	best = prev;
   3120   1.1  christos     }
   3121   1.1  christos   else if (((prev->flags ^ next->flags) & SEC_READONLY) != 0)
   3122   1.1  christos     {
   3123   1.1  christos       if (((next->flags ^ s->flags) & SEC_READONLY) != 0)
   3124   1.1  christos 	best = prev;
   3125   1.1  christos     }
   3126   1.1  christos   else if (((prev->flags ^ next->flags) & SEC_CODE) != 0)
   3127   1.1  christos     {
   3128   1.1  christos       if (((next->flags ^ s->flags) & SEC_CODE) != 0)
   3129   1.1  christos 	best = prev;
   3130   1.1  christos     }
   3131   1.1  christos   else
   3132   1.1  christos     {
   3133   1.1  christos       /* Flags we care about are the same.  Prefer the following
   3134   1.1  christos 	 section if that will result in a positive valued sym.  */
   3135   1.1  christos       if (addr < next->vma)
   3136   1.1  christos 	best = prev;
   3137   1.1  christos     }
   3138   1.8  christos 
   3139   1.1  christos   return best;
   3140   1.1  christos }
   3141   1.1  christos 
   3142   1.1  christos /* Convert symbols in excluded output sections to use a kept section.  */
   3143   1.1  christos 
   3144   1.1  christos static bool
   3145   1.1  christos fix_syms (struct bfd_link_hash_entry *h, void *data)
   3146   1.1  christos {
   3147   1.1  christos   bfd *obfd = (bfd *) data;
   3148   1.1  christos 
   3149   1.1  christos   if (h->type == bfd_link_hash_defined
   3150   1.1  christos       || h->type == bfd_link_hash_defweak)
   3151   1.1  christos     {
   3152   1.1  christos       asection *s = h->u.def.section;
   3153   1.1  christos       if (s != NULL
   3154   1.1  christos 	  && s->output_section != NULL
   3155   1.1  christos 	  && (s->output_section->flags & SEC_EXCLUDE) != 0
   3156   1.1  christos 	  && bfd_section_removed_from_list (obfd, s->output_section))
   3157   1.1  christos 	{
   3158   1.1  christos 	  asection *op;
   3159   1.1  christos 
   3160   1.1  christos 	  h->u.def.value += s->output_offset + s->output_section->vma;
   3161   1.8  christos 	  op = _bfd_nearby_section (obfd, s->output_section, h->u.def.value);
   3162   1.1  christos 	  h->u.def.value -= op->vma;
   3163   1.1  christos 	  h->u.def.section = op;
   3164   1.1  christos 	}
   3165   1.1  christos     }
   3166   1.1  christos 
   3167   1.1  christos   return true;
   3168   1.1  christos }
   3169   1.1  christos 
   3170   1.1  christos void
   3171   1.1  christos _bfd_fix_excluded_sec_syms (bfd *obfd, struct bfd_link_info *info)
   3172   1.1  christos {
   3173   1.1  christos   bfd_link_hash_traverse (info->hash, fix_syms, obfd);
   3174   1.1  christos }
   3175   1.8  christos 
   3176   1.1  christos /*
   3177   1.1  christos FUNCTION
   3178   1.1  christos 	bfd_generic_define_common_symbol
   3179   1.1  christos 
   3180   1.1  christos SYNOPSIS
   3181   1.1  christos 	bool bfd_generic_define_common_symbol
   3182   1.1  christos 	  (bfd *output_bfd, struct bfd_link_info *info,
   3183   1.1  christos 	   struct bfd_link_hash_entry *h);
   3184   1.6  christos 
   3185   1.1  christos DESCRIPTION
   3186   1.1  christos 	Convert common symbol @var{h} into a defined symbol.
   3187   1.1  christos 	Return TRUE on success and FALSE on failure.
   3188   1.8  christos 
   3189   1.1  christos .#define bfd_define_common_symbol(output_bfd, info, h) \
   3190   1.1  christos .	BFD_SEND (output_bfd, _bfd_define_common_symbol, (output_bfd, info, h))
   3191   1.1  christos .
   3192   1.1  christos */
   3193   1.1  christos 
   3194   1.1  christos bool
   3195   1.1  christos bfd_generic_define_common_symbol (bfd *output_bfd,
   3196   1.1  christos 				  struct bfd_link_info *info ATTRIBUTE_UNUSED,
   3197   1.1  christos 				  struct bfd_link_hash_entry *h)
   3198   1.1  christos {
   3199   1.1  christos   unsigned int power_of_two;
   3200   1.1  christos   bfd_vma alignment, size;
   3201   1.1  christos   asection *section;
   3202   1.1  christos 
   3203   1.1  christos   BFD_ASSERT (h != NULL && h->type == bfd_link_hash_common);
   3204   1.8  christos 
   3205   1.8  christos   size = h->u.c.size;
   3206   1.8  christos   power_of_two = h->u.c.p->alignment_power;
   3207   1.8  christos   section = h->u.c.p->section;
   3208   1.8  christos 
   3209   1.8  christos   /* Increase the size of the section to align the common symbol.
   3210   1.8  christos      The alignment must be a power of two.  But if the section does
   3211   1.1  christos      not have any alignment requirement then do not increase the
   3212   1.1  christos      alignment unnecessarily.  */
   3213   1.1  christos   if (power_of_two)
   3214   1.1  christos     alignment = bfd_octets_per_byte (output_bfd, section) << power_of_two;
   3215   1.1  christos   else
   3216   1.1  christos     alignment = 1;
   3217   1.1  christos   BFD_ASSERT (alignment != 0 && (alignment & -alignment) == alignment);
   3218   1.1  christos   section->size += alignment - 1;
   3219   1.1  christos   section->size &= -alignment;
   3220   1.1  christos 
   3221   1.1  christos   /* Adjust the section's overall alignment if necessary.  */
   3222   1.1  christos   if (power_of_two > section->alignment_power)
   3223   1.1  christos     section->alignment_power = power_of_two;
   3224   1.1  christos 
   3225   1.1  christos   /* Change the symbol from common to defined.  */
   3226   1.1  christos   h->type = bfd_link_hash_defined;
   3227   1.1  christos   h->u.def.section = section;
   3228   1.1  christos   h->u.def.value = section->size;
   3229   1.1  christos 
   3230   1.7  christos   /* Increase the size of the section.  */
   3231   1.8  christos   section->size += size;
   3232   1.1  christos 
   3233   1.1  christos   /* Make sure the section is allocated in memory, and make sure that
   3234   1.1  christos      it is no longer a common section.  */
   3235   1.1  christos   section->flags |= SEC_ALLOC;
   3236   1.6  christos   section->flags &= ~(SEC_IS_COMMON | SEC_HAS_CONTENTS);
   3237   1.6  christos   return true;
   3238   1.6  christos }
   3239   1.6  christos 
   3240   1.6  christos /*
   3241   1.6  christos FUNCTION
   3242   1.6  christos 	_bfd_generic_link_hide_symbol
   3243   1.6  christos 
   3244   1.6  christos SYNOPSIS
   3245   1.6  christos 	void _bfd_generic_link_hide_symbol
   3246   1.6  christos 	  (bfd *output_bfd, struct bfd_link_info *info,
   3247   1.6  christos 	   struct bfd_link_hash_entry *h);
   3248   1.6  christos 
   3249   1.6  christos DESCRIPTION
   3250   1.6  christos 	Hide symbol @var{h}.
   3251   1.6  christos 	This is an internal function.  It should not be called from
   3252   1.6  christos 	outside the BFD library.
   3253   1.6  christos 
   3254   1.6  christos .#define bfd_link_hide_symbol(output_bfd, info, h) \
   3255   1.6  christos .	BFD_SEND (output_bfd, _bfd_link_hide_symbol, (output_bfd, info, h))
   3256   1.6  christos .
   3257   1.6  christos */
   3258   1.6  christos 
   3259   1.6  christos void
   3260   1.6  christos _bfd_generic_link_hide_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
   3261   1.6  christos 			       struct bfd_link_info *info ATTRIBUTE_UNUSED,
   3262   1.6  christos 			       struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED)
   3263   1.6  christos {
   3264   1.6  christos }
   3265   1.6  christos 
   3266   1.6  christos /*
   3267   1.6  christos FUNCTION
   3268   1.6  christos 	bfd_generic_define_start_stop
   3269   1.6  christos 
   3270   1.6  christos SYNOPSIS
   3271   1.6  christos 	struct bfd_link_hash_entry *bfd_generic_define_start_stop
   3272   1.6  christos 	  (struct bfd_link_info *info,
   3273   1.6  christos 	   const char *symbol, asection *sec);
   3274   1.6  christos 
   3275   1.6  christos DESCRIPTION
   3276   1.6  christos 	Define a __start, __stop, .startof. or .sizeof. symbol.
   3277   1.6  christos 	Return the symbol or NULL if no such undefined symbol exists.
   3278   1.6  christos 
   3279   1.6  christos .#define bfd_define_start_stop(output_bfd, info, symbol, sec) \
   3280   1.6  christos .	BFD_SEND (output_bfd, _bfd_define_start_stop, (info, symbol, sec))
   3281   1.6  christos .
   3282   1.6  christos */
   3283   1.6  christos 
   3284   1.8  christos struct bfd_link_hash_entry *
   3285   1.6  christos bfd_generic_define_start_stop (struct bfd_link_info *info,
   3286   1.8  christos 			       const char *symbol, asection *sec)
   3287   1.6  christos {
   3288   1.6  christos   struct bfd_link_hash_entry *h;
   3289   1.6  christos 
   3290   1.6  christos   h = bfd_link_hash_lookup (info->hash, symbol, false, false, true);
   3291   1.6  christos   if (h != NULL
   3292   1.6  christos       && !h->ldscript_def
   3293   1.6  christos       && (h->type == bfd_link_hash_undefined
   3294   1.6  christos 	  || h->type == bfd_link_hash_undefweak))
   3295   1.6  christos     {
   3296   1.6  christos       h->type = bfd_link_hash_defined;
   3297   1.6  christos       h->u.def.section = sec;
   3298   1.6  christos       h->u.def.value = 0;
   3299   1.6  christos       return h;
   3300   1.3  christos     }
   3301   1.1  christos   return NULL;
   3302   1.1  christos }
   3303   1.1  christos 
   3304   1.1  christos /*
   3305   1.8  christos FUNCTION
   3306   1.1  christos 	bfd_find_version_for_sym
   3307   1.1  christos 
   3308   1.1  christos SYNOPSIS
   3309   1.1  christos 	struct bfd_elf_version_tree * bfd_find_version_for_sym
   3310   1.1  christos 	  (struct bfd_elf_version_tree *verdefs,
   3311   1.1  christos 	   const char *sym_name, bool *hide);
   3312   1.1  christos 
   3313   1.1  christos DESCRIPTION
   3314   1.1  christos 	Search an elf version script tree for symbol versioning
   3315   1.1  christos 	info and export / don't-export status for a given symbol.
   3316   1.1  christos 	Return non-NULL on success and NULL on failure; also sets
   3317   1.1  christos 	the output @samp{hide} boolean parameter.
   3318   1.8  christos 
   3319   1.1  christos */
   3320   1.1  christos 
   3321   1.1  christos struct bfd_elf_version_tree *
   3322   1.1  christos bfd_find_version_for_sym (struct bfd_elf_version_tree *verdefs,
   3323   1.1  christos 			  const char *sym_name,
   3324   1.1  christos 			  bool *hide)
   3325   1.1  christos {
   3326   1.1  christos   struct bfd_elf_version_tree *t;
   3327   1.1  christos   struct bfd_elf_version_tree *local_ver, *global_ver, *exist_ver;
   3328   1.1  christos   struct bfd_elf_version_tree *star_local_ver, *star_global_ver;
   3329   1.1  christos 
   3330   1.1  christos   local_ver = NULL;
   3331   1.1  christos   global_ver = NULL;
   3332   1.1  christos   star_local_ver = NULL;
   3333   1.1  christos   star_global_ver = NULL;
   3334   1.1  christos   exist_ver = NULL;
   3335   1.1  christos   for (t = verdefs; t != NULL; t = t->next)
   3336   1.1  christos     {
   3337   1.1  christos       if (t->globals.list != NULL)
   3338   1.1  christos 	{
   3339   1.1  christos 	  struct bfd_elf_version_expr *d = NULL;
   3340   1.1  christos 
   3341   1.1  christos 	  while ((d = (*t->match) (&t->globals, d, sym_name)) != NULL)
   3342   1.1  christos 	    {
   3343   1.1  christos 	      if (d->literal || strcmp (d->pattern, "*") != 0)
   3344   1.1  christos 		global_ver = t;
   3345   1.1  christos 	      else
   3346   1.1  christos 		star_global_ver = t;
   3347   1.1  christos 	      if (d->symver)
   3348   1.1  christos 		exist_ver = t;
   3349   1.1  christos 	      d->script = 1;
   3350   1.1  christos 	      /* If the match is a wildcard pattern, keep looking for
   3351   1.1  christos 		 a more explicit, perhaps even local, match.  */
   3352   1.1  christos 	      if (d->literal)
   3353   1.1  christos 		break;
   3354   1.1  christos 	    }
   3355   1.1  christos 
   3356   1.1  christos 	  if (d != NULL)
   3357   1.1  christos 	    break;
   3358   1.1  christos 	}
   3359   1.1  christos 
   3360   1.1  christos       if (t->locals.list != NULL)
   3361   1.1  christos 	{
   3362   1.1  christos 	  struct bfd_elf_version_expr *d = NULL;
   3363   1.1  christos 
   3364   1.1  christos 	  while ((d = (*t->match) (&t->locals, d, sym_name)) != NULL)
   3365   1.1  christos 	    {
   3366   1.1  christos 	      if (d->literal || strcmp (d->pattern, "*") != 0)
   3367   1.1  christos 		local_ver = t;
   3368   1.1  christos 	      else
   3369   1.1  christos 		star_local_ver = t;
   3370   1.1  christos 	      /* If the match is a wildcard pattern, keep looking for
   3371   1.1  christos 		 a more explicit, perhaps even global, match.  */
   3372   1.1  christos 	      if (d->literal)
   3373   1.1  christos 		{
   3374   1.1  christos 		  /* An exact match overrides a global wildcard.  */
   3375   1.1  christos 		  global_ver = NULL;
   3376   1.1  christos 		  star_global_ver = NULL;
   3377   1.1  christos 		  break;
   3378   1.1  christos 		}
   3379   1.1  christos 	    }
   3380   1.1  christos 
   3381   1.1  christos 	  if (d != NULL)
   3382   1.1  christos 	    break;
   3383   1.1  christos 	}
   3384   1.1  christos     }
   3385   1.1  christos 
   3386   1.1  christos   if (global_ver == NULL && local_ver == NULL)
   3387   1.1  christos     global_ver = star_global_ver;
   3388   1.1  christos 
   3389   1.1  christos   if (global_ver != NULL)
   3390   1.1  christos     {
   3391   1.1  christos       /* If we already have a versioned symbol that matches the
   3392   1.1  christos 	 node for this symbol, then we don't want to create a
   3393   1.1  christos 	 duplicate from the unversioned symbol.  Instead hide the
   3394   1.1  christos 	 unversioned symbol.  */
   3395   1.1  christos       *hide = exist_ver == global_ver;
   3396   1.1  christos       return global_ver;
   3397   1.1  christos     }
   3398   1.8  christos 
   3399   1.1  christos   if (local_ver == NULL)
   3400   1.1  christos     local_ver = star_local_ver;
   3401   1.1  christos 
   3402   1.1  christos   if (local_ver != NULL)
   3403   1.1  christos     {
   3404   1.1  christos       *hide = true;
   3405   1.1  christos       return local_ver;
   3406   1.1  christos     }
   3407   1.1  christos 
   3408   1.1  christos   return NULL;
   3409   1.1  christos }
   3410   1.8  christos 
   3411   1.1  christos /*
   3412   1.1  christos FUNCTION
   3413   1.1  christos 	bfd_hide_sym_by_version
   3414   1.1  christos 
   3415   1.1  christos SYNOPSIS
   3416   1.1  christos 	bool bfd_hide_sym_by_version
   3417   1.1  christos 	  (struct bfd_elf_version_tree *verdefs, const char *sym_name);
   3418   1.1  christos 
   3419   1.8  christos DESCRIPTION
   3420   1.1  christos 	Search an elf version script tree for symbol versioning
   3421   1.1  christos 	info for a given symbol.  Return TRUE if the symbol is hidden.
   3422   1.1  christos 
   3423   1.8  christos */
   3424   1.1  christos 
   3425   1.1  christos bool
   3426   1.1  christos bfd_hide_sym_by_version (struct bfd_elf_version_tree *verdefs,
   3427   1.5  christos 			 const char *sym_name)
   3428   1.5  christos {
   3429   1.5  christos   bool hidden = false;
   3430   1.5  christos   bfd_find_version_for_sym (verdefs, sym_name, &hidden);
   3431   1.5  christos   return hidden;
   3432   1.5  christos }
   3433   1.8  christos 
   3434   1.5  christos /*
   3435   1.5  christos FUNCTION
   3436   1.5  christos 	bfd_link_check_relocs
   3437   1.5  christos 
   3438   1.5  christos SYNOPSIS
   3439   1.5  christos 	bool bfd_link_check_relocs
   3440   1.5  christos 	  (bfd *abfd, struct bfd_link_info *info);
   3441   1.5  christos 
   3442   1.5  christos DESCRIPTION
   3443   1.8  christos 	Checks the relocs in ABFD for validity.
   3444   1.5  christos 	Does not execute the relocs.
   3445   1.5  christos 	Return TRUE if everything is OK, FALSE otherwise.
   3446   1.5  christos 	This is the external entry point to this code.
   3447   1.5  christos */
   3448   1.5  christos 
   3449   1.5  christos bool
   3450   1.5  christos bfd_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
   3451   1.5  christos {
   3452   1.5  christos   return BFD_SEND (abfd, _bfd_link_check_relocs, (abfd, info));
   3453   1.5  christos }
   3454   1.8  christos 
   3455   1.5  christos /*
   3456   1.5  christos FUNCTION
   3457   1.5  christos 	_bfd_generic_link_check_relocs
   3458   1.6  christos 
   3459   1.5  christos SYNOPSIS
   3460   1.5  christos 	bool _bfd_generic_link_check_relocs
   3461   1.5  christos 	  (bfd *abfd, struct bfd_link_info *info);
   3462   1.5  christos 
   3463   1.5  christos DESCRIPTION
   3464   1.8  christos 	Stub function for targets that do not implement reloc checking.
   3465   1.5  christos 	Return TRUE.
   3466   1.5  christos 	This is an internal function.  It should not be called from
   3467   1.5  christos 	outside the BFD library.
   3468   1.8  christos */
   3469   1.5  christos 
   3470   1.6  christos bool
   3471   1.6  christos _bfd_generic_link_check_relocs (bfd *abfd ATTRIBUTE_UNUSED,
   3472   1.6  christos 				struct bfd_link_info *info ATTRIBUTE_UNUSED)
   3473   1.6  christos {
   3474   1.6  christos   return true;
   3475   1.6  christos }
   3476   1.8  christos 
   3477   1.6  christos /*
   3478   1.6  christos FUNCTION
   3479   1.6  christos 	bfd_merge_private_bfd_data
   3480   1.6  christos 
   3481   1.6  christos SYNOPSIS
   3482   1.6  christos 	bool bfd_merge_private_bfd_data
   3483   1.6  christos 	  (bfd *ibfd, struct bfd_link_info *info);
   3484   1.6  christos 
   3485   1.6  christos DESCRIPTION
   3486   1.6  christos 	Merge private BFD information from the BFD @var{ibfd} to the
   3487   1.6  christos 	the output file BFD when linking.  Return <<TRUE>> on success,
   3488   1.6  christos 	<<FALSE>> on error.  Possible error returns are:
   3489   1.6  christos 
   3490   1.9  christos 	o <<bfd_error_no_memory>> -
   3491   1.6  christos 	Not enough memory exists to create private data for @var{obfd}.
   3492   1.6  christos 
   3493   1.6  christos .#define bfd_merge_private_bfd_data(ibfd, info) \
   3494   1.6  christos .	BFD_SEND ((info)->output_bfd, _bfd_merge_private_bfd_data, \
   3495   1.6  christos .		  (ibfd, info))
   3496   1.6  christos .
   3497   1.6  christos */
   3498   1.8  christos 
   3499   1.6  christos /*
   3500   1.6  christos INTERNAL_FUNCTION
   3501   1.6  christos 	_bfd_generic_verify_endian_match
   3502   1.6  christos 
   3503   1.6  christos SYNOPSIS
   3504   1.6  christos 	bool _bfd_generic_verify_endian_match
   3505   1.6  christos 	  (bfd *ibfd, struct bfd_link_info *info);
   3506   1.6  christos 
   3507   1.8  christos DESCRIPTION
   3508   1.6  christos 	Can be used from / for bfd_merge_private_bfd_data to check that
   3509   1.6  christos 	endianness matches between input and output file.  Returns
   3510   1.6  christos 	TRUE for a match, otherwise returns FALSE and emits an error.
   3511   1.6  christos */
   3512   1.6  christos 
   3513   1.6  christos bool
   3514   1.6  christos _bfd_generic_verify_endian_match (bfd *ibfd, struct bfd_link_info *info)
   3515   1.6  christos {
   3516   1.6  christos   bfd *obfd = info->output_bfd;
   3517   1.6  christos 
   3518   1.6  christos   if (ibfd->xvec->byteorder != obfd->xvec->byteorder
   3519   1.6  christos       && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
   3520   1.6  christos       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
   3521   1.6  christos     {
   3522   1.6  christos       if (bfd_big_endian (ibfd))
   3523   1.8  christos 	_bfd_error_handler (_("%pB: compiled for a big endian system "
   3524   1.6  christos 			      "and target is little endian"), ibfd);
   3525   1.6  christos       else
   3526   1.8  christos 	_bfd_error_handler (_("%pB: compiled for a little endian system "
   3527   1.6  christos 			      "and target is big endian"), ibfd);
   3528   1.6  christos       bfd_set_error (bfd_error_wrong_format);
   3529   1.6  christos       return false;
   3530   1.6  christos     }
   3531   1.6  christos 
   3532   1.6  christos   return true;
   3533   1.6  christos }
   3534   1.6  christos 
   3535   1.6  christos int
   3536   1.8  christos _bfd_nolink_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
   3537   1.6  christos 			    struct bfd_link_info *info ATTRIBUTE_UNUSED)
   3538   1.6  christos {
   3539   1.6  christos   return 0;
   3540   1.8  christos }
   3541   1.6  christos 
   3542   1.6  christos bool
   3543   1.6  christos _bfd_nolink_bfd_relax_section (bfd *abfd,
   3544   1.6  christos 			       asection *section ATTRIBUTE_UNUSED,
   3545   1.6  christos 			       struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
   3546   1.6  christos 			       bool *again ATTRIBUTE_UNUSED)
   3547   1.6  christos {
   3548   1.6  christos   return _bfd_bool_bfd_false_error (abfd);
   3549   1.6  christos }
   3550   1.6  christos 
   3551   1.8  christos bfd_byte *
   3552   1.6  christos _bfd_nolink_bfd_get_relocated_section_contents
   3553   1.6  christos     (bfd *abfd,
   3554   1.6  christos      struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
   3555   1.6  christos      struct bfd_link_order *link_order ATTRIBUTE_UNUSED,
   3556   1.6  christos      bfd_byte *data ATTRIBUTE_UNUSED,
   3557   1.8  christos      bool relocatable ATTRIBUTE_UNUSED,
   3558   1.6  christos      asymbol **symbols ATTRIBUTE_UNUSED)
   3559   1.6  christos {
   3560   1.6  christos   return (bfd_byte *) _bfd_ptr_bfd_null_error (abfd);
   3561   1.6  christos }
   3562   1.6  christos 
   3563   1.6  christos bool
   3564   1.6  christos _bfd_nolink_bfd_lookup_section_flags
   3565   1.6  christos     (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   3566   1.8  christos      struct flag_info *flaginfo ATTRIBUTE_UNUSED,
   3567   1.6  christos      asection *section)
   3568   1.6  christos {
   3569   1.6  christos   return _bfd_bool_bfd_false_error (section->owner);
   3570   1.6  christos }
   3571   1.6  christos 
   3572   1.6  christos bool
   3573   1.7  christos _bfd_nolink_bfd_is_group_section (bfd *abfd,
   3574   1.7  christos 				  const asection *sec ATTRIBUTE_UNUSED)
   3575   1.7  christos {
   3576   1.7  christos   return _bfd_bool_bfd_false_error (abfd);
   3577   1.7  christos }
   3578   1.7  christos 
   3579   1.7  christos const char *
   3580   1.8  christos _bfd_nolink_bfd_group_name (bfd *abfd,
   3581   1.6  christos 			    const asection *sec ATTRIBUTE_UNUSED)
   3582   1.6  christos {
   3583   1.6  christos   return _bfd_ptr_bfd_null_error (abfd);
   3584   1.6  christos }
   3585   1.6  christos 
   3586   1.6  christos bool
   3587   1.6  christos _bfd_nolink_bfd_discard_group (bfd *abfd, asection *sec ATTRIBUTE_UNUSED)
   3588   1.6  christos {
   3589   1.6  christos   return _bfd_bool_bfd_false_error (abfd);
   3590   1.6  christos }
   3591   1.6  christos 
   3592   1.6  christos struct bfd_link_hash_table *
   3593   1.6  christos _bfd_nolink_bfd_link_hash_table_create (bfd *abfd)
   3594   1.6  christos {
   3595   1.6  christos   return (struct bfd_link_hash_table *) _bfd_ptr_bfd_null_error (abfd);
   3596   1.6  christos }
   3597   1.6  christos 
   3598   1.6  christos void
   3599   1.6  christos _bfd_nolink_bfd_link_just_syms (asection *sec ATTRIBUTE_UNUSED,
   3600   1.6  christos 				struct bfd_link_info *info ATTRIBUTE_UNUSED)
   3601   1.6  christos {
   3602   1.6  christos }
   3603   1.6  christos 
   3604   1.6  christos void
   3605   1.6  christos _bfd_nolink_bfd_copy_link_hash_symbol_type
   3606   1.8  christos     (bfd *abfd ATTRIBUTE_UNUSED,
   3607   1.6  christos      struct bfd_link_hash_entry *from ATTRIBUTE_UNUSED,
   3608   1.6  christos      struct bfd_link_hash_entry *to ATTRIBUTE_UNUSED)
   3609   1.6  christos {
   3610   1.6  christos }
   3611   1.6  christos 
   3612   1.8  christos bool
   3613   1.6  christos _bfd_nolink_bfd_link_split_section (bfd *abfd, asection *sec ATTRIBUTE_UNUSED)
   3614   1.6  christos {
   3615   1.6  christos   return _bfd_bool_bfd_false_error (abfd);
   3616   1.6  christos }
   3617   1.6  christos 
   3618   1.6  christos bool
   3619   1.6  christos _bfd_nolink_section_already_linked (bfd *abfd,
   3620   1.8  christos 				    asection *sec ATTRIBUTE_UNUSED,
   3621   1.6  christos 				    struct bfd_link_info *info ATTRIBUTE_UNUSED)
   3622   1.6  christos {
   3623   1.6  christos   return _bfd_bool_bfd_false_error (abfd);
   3624   1.6  christos }
   3625   1.6  christos 
   3626   1.6  christos bool
   3627   1.6  christos _bfd_nolink_bfd_define_common_symbol
   3628   1.6  christos     (bfd *abfd,
   3629   1.6  christos      struct bfd_link_info *info ATTRIBUTE_UNUSED,
   3630   1.6  christos      struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED)
   3631   1.6  christos {
   3632   1.6  christos   return _bfd_bool_bfd_false_error (abfd);
   3633   1.6  christos }
   3634   1.6  christos 
   3635   1.6  christos struct bfd_link_hash_entry *
   3636                 _bfd_nolink_bfd_define_start_stop (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   3637                 				   const char *name ATTRIBUTE_UNUSED,
   3638                 				   asection *sec)
   3639                 {
   3640                   return (struct bfd_link_hash_entry *) _bfd_ptr_bfd_null_error (sec->owner);
   3641                 }
   3642