home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gdb-4.16-base.tgz / gdb-4.16-base.tar / fsf / gdb / bfd / reloc.c < prev    next >
C/C++ Source or Header  |  1996-04-23  |  68KB  |  2,392 lines

  1. /* BFD support for handling relocation entries.
  2.    Copyright (C) 1990, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  20.  
  21. /*
  22. SECTION
  23.     Relocations
  24.  
  25.     BFD maintains relocations in much the same way it maintains
  26.     symbols: they are left alone until required, then read in
  27.     en-mass and translated into an internal form.  A common
  28.     routine <<bfd_perform_relocation>> acts upon the
  29.     canonical form to do the fixup.
  30.  
  31.     Relocations are maintained on a per section basis,
  32.     while symbols are maintained on a per BFD basis.
  33.  
  34.     All that a back end has to do to fit the BFD interface is to create
  35.     a <<struct reloc_cache_entry>> for each relocation
  36.     in a particular section, and fill in the right bits of the structures.
  37.  
  38. @menu
  39. @* typedef arelent::
  40. @* howto manager::
  41. @end menu
  42.  
  43. */
  44.  
  45. /* DO compile in the reloc_code name table from libbfd.h.  */
  46. #define _BFD_MAKE_TABLE_bfd_reloc_code_real
  47.  
  48. #include "bfd.h"
  49. #include "sysdep.h"
  50. #include "bfdlink.h"
  51. #include "libbfd.h"
  52. /*
  53. DOCDD
  54. INODE
  55.     typedef arelent, howto manager, Relocations, Relocations
  56.  
  57. SUBSECTION
  58.     typedef arelent
  59.  
  60.     This is the structure of a relocation entry:
  61.  
  62. CODE_FRAGMENT
  63. .
  64. .typedef enum bfd_reloc_status
  65. .{
  66. .       {* No errors detected *}
  67. .  bfd_reloc_ok,
  68. .
  69. .       {* The relocation was performed, but there was an overflow. *}
  70. .  bfd_reloc_overflow,
  71. .
  72. .       {* The address to relocate was not within the section supplied. *}
  73. .  bfd_reloc_outofrange,
  74. .
  75. .       {* Used by special functions *}
  76. .  bfd_reloc_continue,
  77. .
  78. .       {* Unsupported relocation size requested. *}
  79. .  bfd_reloc_notsupported,
  80. .
  81. .       {* Unused *}
  82. .  bfd_reloc_other,
  83. .
  84. .       {* The symbol to relocate against was undefined. *}
  85. .  bfd_reloc_undefined,
  86. .
  87. .       {* The relocation was performed, but may not be ok - presently
  88. .          generated only when linking i960 coff files with i960 b.out
  89. .          symbols.  If this type is returned, the error_message argument
  90. .          to bfd_perform_relocation will be set.  *}
  91. .  bfd_reloc_dangerous
  92. . }
  93. . bfd_reloc_status_type;
  94. .
  95. .
  96. .typedef struct reloc_cache_entry
  97. .{
  98. .       {* A pointer into the canonical table of pointers  *}
  99. .  struct symbol_cache_entry **sym_ptr_ptr;
  100. .
  101. .       {* offset in section *}
  102. .  bfd_size_type address;
  103. .
  104. .       {* addend for relocation value *}
  105. .  bfd_vma addend;
  106. .
  107. .       {* Pointer to how to perform the required relocation *}
  108. .  reloc_howto_type *howto;
  109. .
  110. .} arelent;
  111.  
  112. */
  113.  
  114. /*
  115. DESCRIPTION
  116.  
  117.         Here is a description of each of the fields within an <<arelent>>:
  118.  
  119.         o <<sym_ptr_ptr>>
  120.  
  121.         The symbol table pointer points to a pointer to the symbol
  122.         associated with the relocation request.  It is
  123.         the pointer into the table returned by the back end's
  124.         <<get_symtab>> action. @xref{Symbols}. The symbol is referenced
  125.         through a pointer to a pointer so that tools like the linker
  126.         can fix up all the symbols of the same name by modifying only
  127.         one pointer. The relocation routine looks in the symbol and
  128.         uses the base of the section the symbol is attached to and the
  129.         value of the symbol as the initial relocation offset. If the
  130.         symbol pointer is zero, then the section provided is looked up.
  131.  
  132.         o <<address>>
  133.  
  134.         The <<address>> field gives the offset in bytes from the base of
  135.         the section data which owns the relocation record to the first
  136.         byte of relocatable information. The actual data relocated
  137.         will be relative to this point; for example, a relocation
  138.         type which modifies the bottom two bytes of a four byte word
  139.         would not touch the first byte pointed to in a big endian
  140.         world.
  141.     
  142.     o <<addend>>
  143.  
  144.     The <<addend>> is a value provided by the back end to be added (!)
  145.     to the relocation offset. Its interpretation is dependent upon
  146.     the howto. For example, on the 68k the code:
  147.  
  148.  
  149. |        char foo[];
  150. |        main()
  151. |                {
  152. |                return foo[0x12345678];
  153. |                }
  154.  
  155.         Could be compiled into:
  156.  
  157. |        linkw fp,#-4
  158. |        moveb @@#12345678,d0
  159. |        extbl d0
  160. |        unlk fp
  161. |        rts
  162.  
  163.  
  164.         This could create a reloc pointing to <<foo>>, but leave the
  165.         offset in the data, something like:
  166.  
  167.  
  168. |RELOCATION RECORDS FOR [.text]:
  169. |offset   type      value
  170. |00000006 32        _foo
  171. |
  172. |00000000 4e56 fffc          ; linkw fp,#-4
  173. |00000004 1039 1234 5678     ; moveb @@#12345678,d0
  174. |0000000a 49c0               ; extbl d0
  175. |0000000c 4e5e               ; unlk fp
  176. |0000000e 4e75               ; rts
  177.  
  178.  
  179.         Using coff and an 88k, some instructions don't have enough
  180.         space in them to represent the full address range, and
  181.         pointers have to be loaded in two parts. So you'd get something like:
  182.  
  183.  
  184. |        or.u     r13,r0,hi16(_foo+0x12345678)
  185. |        ld.b     r2,r13,lo16(_foo+0x12345678)
  186. |        jmp      r1
  187.  
  188.  
  189.         This should create two relocs, both pointing to <<_foo>>, and with
  190.         0x12340000 in their addend field. The data would consist of:
  191.  
  192.  
  193. |RELOCATION RECORDS FOR [.text]:
  194. |offset   type      value
  195. |00000002 HVRT16    _foo+0x12340000
  196. |00000006 LVRT16    _foo+0x12340000
  197. |
  198. |00000000 5da05678           ; or.u r13,r0,0x5678
  199. |00000004 1c4d5678           ; ld.b r2,r13,0x5678
  200. |00000008 f400c001           ; jmp r1
  201.  
  202.  
  203.         The relocation routine digs out the value from the data, adds
  204.         it to the addend to get the original offset, and then adds the
  205.         value of <<_foo>>. Note that all 32 bits have to be kept around
  206.         somewhere, to cope with carry from bit 15 to bit 16.
  207.  
  208.         One further example is the sparc and the a.out format. The
  209.         sparc has a similar problem to the 88k, in that some
  210.         instructions don't have room for an entire offset, but on the
  211.         sparc the parts are created in odd sized lumps. The designers of
  212.         the a.out format chose to not use the data within the section
  213.         for storing part of the offset; all the offset is kept within
  214.         the reloc. Anything in the data should be ignored.
  215.  
  216. |        save %sp,-112,%sp
  217. |        sethi %hi(_foo+0x12345678),%g2
  218. |        ldsb [%g2+%lo(_foo+0x12345678)],%i0
  219. |        ret
  220. |        restore
  221.  
  222.         Both relocs contain a pointer to <<foo>>, and the offsets
  223.         contain junk.
  224.  
  225.  
  226. |RELOCATION RECORDS FOR [.text]:
  227. |offset   type      value
  228. |00000004 HI22      _foo+0x12345678
  229. |00000008 LO10      _foo+0x12345678
  230. |
  231. |00000000 9de3bf90     ; save %sp,-112,%sp
  232. |00000004 05000000     ; sethi %hi(_foo+0),%g2
  233. |00000008 f048a000     ; ldsb [%g2+%lo(_foo+0)],%i0
  234. |0000000c 81c7e008     ; ret
  235. |00000010 81e80000     ; restore
  236.  
  237.  
  238.         o <<howto>>
  239.  
  240.         The <<howto>> field can be imagined as a
  241.         relocation instruction. It is a pointer to a structure which
  242.         contains information on what to do with all of the other
  243.         information in the reloc record and data section. A back end
  244.         would normally have a relocation instruction set and turn
  245.         relocations into pointers to the correct structure on input -
  246.         but it would be possible to create each howto field on demand.
  247.  
  248. */
  249.  
  250. /*
  251. SUBSUBSECTION
  252.     <<enum complain_overflow>>
  253.  
  254.     Indicates what sort of overflow checking should be done when
  255.     performing a relocation.
  256.  
  257. CODE_FRAGMENT
  258. .
  259. .enum complain_overflow
  260. .{
  261. .    {* Do not complain on overflow. *}
  262. .  complain_overflow_dont,
  263. .
  264. .    {* Complain if the bitfield overflows, whether it is considered
  265. .       as signed or unsigned. *}
  266. .  complain_overflow_bitfield,
  267. .
  268. .    {* Complain if the value overflows when considered as signed
  269. .       number. *}
  270. .  complain_overflow_signed,
  271. .
  272. .    {* Complain if the value overflows when considered as an
  273. .       unsigned number. *}
  274. .  complain_overflow_unsigned
  275. .};
  276.  
  277. */
  278.  
  279. /*
  280. SUBSUBSECTION
  281.         <<reloc_howto_type>>
  282.  
  283.         The <<reloc_howto_type>> is a structure which contains all the
  284.         information that libbfd needs to know to tie up a back end's data.
  285.  
  286. CODE_FRAGMENT
  287. .struct symbol_cache_entry;        {* Forward declaration *}
  288. .
  289. .struct reloc_howto_struct
  290. .{
  291. .       {*  The type field has mainly a documetary use - the back end can
  292. .           do what it wants with it, though normally the back end's
  293. .           external idea of what a reloc number is stored
  294. .           in this field. For example, a PC relative word relocation
  295. .           in a coff environment has the type 023 - because that's
  296. .           what the outside world calls a R_PCRWORD reloc. *}
  297. .  unsigned int type;
  298. .
  299. .       {*  The value the final relocation is shifted right by. This drops
  300. .           unwanted data from the relocation.  *}
  301. .  unsigned int rightshift;
  302. .
  303. .    {*  The size of the item to be relocated.  This is *not* a
  304. .        power-of-two measure.  To get the number of bytes operated
  305. .        on by a type of relocation, use bfd_get_reloc_size.  *}
  306. .  int size;
  307. .
  308. .       {*  The number of bits in the item to be relocated.  This is used
  309. .        when doing overflow checking.  *}
  310. .  unsigned int bitsize;
  311. .
  312. .       {*  Notes that the relocation is relative to the location in the
  313. .           data section of the addend. The relocation function will
  314. .           subtract from the relocation value the address of the location
  315. .           being relocated. *}
  316. .  boolean pc_relative;
  317. .
  318. .    {*  The bit position of the reloc value in the destination.
  319. .        The relocated value is left shifted by this amount. *}
  320. .  unsigned int bitpos;
  321. .
  322. .    {* What type of overflow error should be checked for when
  323. .       relocating. *}
  324. .  enum complain_overflow complain_on_overflow;
  325. .
  326. .       {* If this field is non null, then the supplied function is
  327. .          called rather than the normal function. This allows really
  328. .          strange relocation methods to be accomodated (e.g., i960 callj
  329. .          instructions). *}
  330. .  bfd_reloc_status_type (*special_function)
  331. .                    PARAMS ((bfd *abfd,
  332. .                         arelent *reloc_entry,
  333. .                                            struct symbol_cache_entry *symbol,
  334. .                                            PTR data,
  335. .                                            asection *input_section,
  336. .                                            bfd *output_bfd,
  337. .                                            char **error_message));
  338. .
  339. .       {* The textual name of the relocation type. *}
  340. .  char *name;
  341. .
  342. .       {* When performing a partial link, some formats must modify the
  343. .          relocations rather than the data - this flag signals this.*}
  344. .  boolean partial_inplace;
  345. .
  346. .       {* The src_mask selects which parts of the read in data
  347. .          are to be used in the relocation sum.  E.g., if this was an 8 bit
  348. .          bit of data which we read and relocated, this would be
  349. .          0x000000ff. When we have relocs which have an addend, such as
  350. .          sun4 extended relocs, the value in the offset part of a
  351. .          relocating field is garbage so we never use it. In this case
  352. .          the mask would be 0x00000000. *}
  353. .  bfd_vma src_mask;
  354. .
  355. .       {* The dst_mask selects which parts of the instruction are replaced
  356. .          into the instruction. In most cases src_mask == dst_mask,
  357. .          except in the above special case, where dst_mask would be
  358. .          0x000000ff, and src_mask would be 0x00000000.   *}
  359. .  bfd_vma dst_mask;
  360. .
  361. .       {* When some formats create PC relative instructions, they leave
  362. .          the value of the pc of the place being relocated in the offset
  363. .          slot of the instruction, so that a PC relative relocation can
  364. .          be made just by adding in an ordinary offset (e.g., sun3 a.out).
  365. .          Some formats leave the displacement part of an instruction
  366. .          empty (e.g., m88k bcs); this flag signals the fact.*}
  367. .  boolean pcrel_offset;
  368. .
  369. .};
  370.  
  371. */
  372.  
  373. /*
  374. FUNCTION
  375.     The HOWTO Macro
  376.  
  377. DESCRIPTION
  378.     The HOWTO define is horrible and will go away.
  379.  
  380.  
  381. .#define HOWTO(C, R,S,B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
  382. .  {(unsigned)C,R,S,B, P, BI, O,SF,NAME,INPLACE,MASKSRC,MASKDST,PC}
  383.  
  384. DESCRIPTION
  385.     And will be replaced with the totally magic way. But for the
  386.     moment, we are compatible, so do it this way.
  387.  
  388.  
  389. .#define NEWHOWTO( FUNCTION, NAME,SIZE,REL,IN) HOWTO(0,0,SIZE,0,REL,0,complain_overflow_dont,FUNCTION, NAME,false,0,0,IN)
  390. .
  391. DESCRIPTION
  392.     Helper routine to turn a symbol into a relocation value.
  393.  
  394. .#define HOWTO_PREPARE(relocation, symbol)      \
  395. .  {                                            \
  396. .  if (symbol != (asymbol *)NULL) {             \
  397. .    if (bfd_is_com_section (symbol->section)) { \
  398. .      relocation = 0;                          \
  399. .    }                                          \
  400. .    else {                                     \
  401. .      relocation = symbol->value;              \
  402. .    }                                          \
  403. .  }                                            \
  404. .}
  405.  
  406. */
  407.  
  408. /*
  409. FUNCTION
  410.     bfd_get_reloc_size
  411.  
  412. SYNOPSIS
  413.     int bfd_get_reloc_size (reloc_howto_type *);
  414.  
  415. DESCRIPTION
  416.     For a reloc_howto_type that operates on a fixed number of bytes,
  417.     this returns the number of bytes operated on.
  418.  */
  419.  
  420. int
  421. bfd_get_reloc_size (howto)
  422.      reloc_howto_type *howto;
  423. {
  424.   switch (howto->size)
  425.     {
  426.     case 0: return 1;
  427.     case 1: return 2;
  428.     case 2: return 4;
  429.     case 3: return 0;
  430.     case 4: return 8;
  431.     case -2: return 4;
  432.     default: abort ();
  433.     }
  434. }
  435.  
  436. /*
  437. TYPEDEF
  438.     arelent_chain
  439.  
  440. DESCRIPTION
  441.  
  442.     How relocs are tied together in an <<asection>>:
  443.  
  444. .typedef struct relent_chain {
  445. .  arelent relent;
  446. .  struct   relent_chain *next;
  447. .} arelent_chain;
  448.  
  449. */
  450.  
  451.  
  452.  
  453. /*
  454. FUNCTION
  455.     bfd_perform_relocation
  456.  
  457. SYNOPSIS
  458.     bfd_reloc_status_type
  459.                 bfd_perform_relocation
  460.                         (bfd *abfd,
  461.                          arelent *reloc_entry,
  462.                          PTR data,
  463.                          asection *input_section,
  464.                          bfd *output_bfd,
  465.              char **error_message);
  466.  
  467. DESCRIPTION
  468.     If @var{output_bfd} is supplied to this function, the
  469.     generated image will be relocatable; the relocations are
  470.     copied to the output file after they have been changed to
  471.     reflect the new state of the world. There are two ways of
  472.     reflecting the results of partial linkage in an output file:
  473.     by modifying the output data in place, and by modifying the
  474.     relocation record.  Some native formats (e.g., basic a.out and
  475.     basic coff) have no way of specifying an addend in the
  476.     relocation type, so the addend has to go in the output data.
  477.     This is no big deal since in these formats the output data
  478.     slot will always be big enough for the addend. Complex reloc
  479.     types with addends were invented to solve just this problem.
  480.     The @var{error_message} argument is set to an error message if
  481.     this return @code{bfd_reloc_dangerous}.
  482.  
  483. */
  484.  
  485.  
  486. bfd_reloc_status_type
  487. bfd_perform_relocation (abfd, reloc_entry, data, input_section, output_bfd,
  488.             error_message)
  489.      bfd *abfd;
  490.      arelent *reloc_entry;
  491.      PTR data;
  492.      asection *input_section;
  493.      bfd *output_bfd;
  494.      char **error_message;
  495. {
  496.   bfd_vma relocation;
  497.   bfd_reloc_status_type flag = bfd_reloc_ok;
  498.   bfd_size_type addr = reloc_entry->address;
  499.   bfd_vma output_base = 0;
  500.   reloc_howto_type *howto = reloc_entry->howto;
  501.   asection *reloc_target_output_section;
  502.   asymbol *symbol;
  503.  
  504.   symbol = *(reloc_entry->sym_ptr_ptr);
  505.   if (bfd_is_abs_section (symbol->section)
  506.       && output_bfd != (bfd *) NULL)
  507.     {
  508.       reloc_entry->address += input_section->output_offset;
  509.       return bfd_reloc_ok;
  510.     }
  511.  
  512.   /* If we are not producing relocateable output, return an error if
  513.      the symbol is not defined.  An undefined weak symbol is
  514.      considered to have a value of zero (SVR4 ABI, p. 4-27).  */
  515.   if (bfd_is_und_section (symbol->section)
  516.       && (symbol->flags & BSF_WEAK) == 0
  517.       && output_bfd == (bfd *) NULL)
  518.     flag = bfd_reloc_undefined;
  519.  
  520.   /* If there is a function supplied to handle this relocation type,
  521.      call it.  It'll return `bfd_reloc_continue' if further processing
  522.      can be done.  */
  523.   if (howto->special_function)
  524.     {
  525.       bfd_reloc_status_type cont;
  526.       cont = howto->special_function (abfd, reloc_entry, symbol, data,
  527.                       input_section, output_bfd,
  528.                       error_message);
  529.       if (cont != bfd_reloc_continue)
  530.     return cont;
  531.     }
  532.  
  533.   /* Is the address of the relocation really within the section?  */
  534.   if (reloc_entry->address > input_section->_cooked_size)
  535.     return bfd_reloc_outofrange;
  536.  
  537.   /* Work out which section the relocation is targetted at and the
  538.      initial relocation command value.  */
  539.  
  540.   /* Get symbol value.  (Common symbols are special.)  */
  541.   if (bfd_is_com_section (symbol->section))
  542.     relocation = 0;
  543.   else
  544.     relocation = symbol->value;
  545.  
  546.  
  547.   reloc_target_output_section = symbol->section->output_section;
  548.  
  549.   /* Convert input-section-relative symbol value to absolute.  */
  550.   if (output_bfd && howto->partial_inplace == false)
  551.     output_base = 0;
  552.   else
  553.     output_base = reloc_target_output_section->vma;
  554.  
  555.   relocation += output_base + symbol->section->output_offset;
  556.  
  557.   /* Add in supplied addend.  */
  558.   relocation += reloc_entry->addend;
  559.  
  560.   /* Here the variable relocation holds the final address of the
  561.      symbol we are relocating against, plus any addend.  */
  562.  
  563.   if (howto->pc_relative == true)
  564.     {
  565.       /* This is a PC relative relocation.  We want to set RELOCATION
  566.      to the distance between the address of the symbol and the
  567.      location.  RELOCATION is already the address of the symbol.
  568.  
  569.      We start by subtracting the address of the section containing
  570.      the location.
  571.  
  572.      If pcrel_offset is set, we must further subtract the position
  573.      of the location within the section.  Some targets arrange for
  574.      the addend to be the negative of the position of the location
  575.      within the section; for example, i386-aout does this.  For
  576.      i386-aout, pcrel_offset is false.  Some other targets do not
  577.      include the position of the location; for example, m88kbcs,
  578.      or ELF.  For those targets, pcrel_offset is true.
  579.  
  580.      If we are producing relocateable output, then we must ensure
  581.      that this reloc will be correctly computed when the final
  582.      relocation is done.  If pcrel_offset is false we want to wind
  583.      up with the negative of the location within the section,
  584.      which means we must adjust the existing addend by the change
  585.      in the location within the section.  If pcrel_offset is true
  586.      we do not want to adjust the existing addend at all.
  587.  
  588.      FIXME: This seems logical to me, but for the case of
  589.      producing relocateable output it is not what the code
  590.      actually does.  I don't want to change it, because it seems
  591.      far too likely that something will break.  */
  592.  
  593.       relocation -=
  594.     input_section->output_section->vma + input_section->output_offset;
  595.  
  596.       if (howto->pcrel_offset == true)
  597.     relocation -= reloc_entry->address;
  598.     }
  599.  
  600.   if (output_bfd != (bfd *) NULL)
  601.     {
  602.       if (howto->partial_inplace == false)
  603.     {
  604.       /* This is a partial relocation, and we want to apply the relocation
  605.          to the reloc entry rather than the raw data. Modify the reloc
  606.          inplace to reflect what we now know.  */
  607.       reloc_entry->addend = relocation;
  608.       reloc_entry->address += input_section->output_offset;
  609.       return flag;
  610.     }
  611.       else
  612.     {
  613.       /* This is a partial relocation, but inplace, so modify the
  614.          reloc record a bit.
  615.  
  616.          If we've relocated with a symbol with a section, change
  617.          into a ref to the section belonging to the symbol.  */
  618.  
  619.       reloc_entry->address += input_section->output_offset;
  620.  
  621.       /* WTF?? */
  622.       if (abfd->xvec->flavour == bfd_target_coff_flavour
  623.           && strcmp (abfd->xvec->name, "aixcoff-rs6000") != 0
  624.           && strcmp (abfd->xvec->name, "coff-Intel-little") != 0
  625.           && strcmp (abfd->xvec->name, "coff-Intel-big") != 0)
  626.         {
  627. #if 1
  628.           /* For m68k-coff, the addend was being subtracted twice during
  629.          relocation with -r.  Removing the line below this comment
  630.          fixes that problem; see PR 2953.
  631.  
  632. However, Ian wrote the following, regarding removing the line below,
  633. which explains why it is still enabled:  --djm
  634.  
  635. If you put a patch like that into BFD you need to check all the COFF
  636. linkers.  I am fairly certain that patch will break coff-i386 (e.g.,
  637. SCO); see coff_i386_reloc in coff-i386.c where I worked around the
  638. problem in a different way.  There may very well be a reason that the
  639. code works as it does.
  640.  
  641. Hmmm.  The first obvious point is that bfd_perform_relocation should
  642. not have any tests that depend upon the flavour.  It's seem like
  643. entirely the wrong place for such a thing.  The second obvious point
  644. is that the current code ignores the reloc addend when producing
  645. relocateable output for COFF.  That's peculiar.  In fact, I really
  646. have no idea what the point of the line you want to remove is.
  647.  
  648. A typical COFF reloc subtracts the old value of the symbol and adds in
  649. the new value to the location in the object file (if it's a pc
  650. relative reloc it adds the difference between the symbol value and the
  651. location).  When relocating we need to preserve that property.
  652.  
  653. BFD handles this by setting the addend to the negative of the old
  654. value of the symbol.  Unfortunately it handles common symbols in a
  655. non-standard way (it doesn't subtract the old value) but that's a
  656. different story (we can't change it without losing backward
  657. compatibility with old object files) (coff-i386 does subtract the old
  658. value, to be compatible with existing coff-i386 targets, like SCO).
  659.  
  660. So everything works fine when not producing relocateable output.  When
  661. we are producing relocateable output, logically we should do exactly
  662. what we do when not producing relocateable output.  Therefore, your
  663. patch is correct.  In fact, it should probably always just set
  664. reloc_entry->addend to 0 for all cases, since it is, in fact, going to
  665. add the value into the object file.  This won't hurt the COFF code,
  666. which doesn't use the addend; I'm not sure what it will do to other
  667. formats (the thing to check for would be whether any formats both use
  668. the addend and set partial_inplace).
  669.  
  670. When I wanted to make coff-i386 produce relocateable output, I ran
  671. into the problem that you are running into: I wanted to remove that
  672. line.  Rather than risk it, I made the coff-i386 relocs use a special
  673. function; it's coff_i386_reloc in coff-i386.c.  The function
  674. specifically adds the addend field into the object file, knowing that
  675. bfd_perform_relocation is not going to.  If you remove that line, then
  676. coff-i386.c will wind up adding the addend field in twice.  It's
  677. trivial to fix; it just needs to be done.
  678.  
  679. The problem with removing the line is just that it may break some
  680. working code.  With BFD it's hard to be sure of anything.  The right
  681. way to deal with this is simply to build and test at least all the
  682. supported COFF targets.  It should be straightforward if time and disk
  683. space consuming.  For each target:
  684.     1) build the linker
  685.     2) generate some executable, and link it using -r (I would
  686.        probably use paranoia.o and link against newlib/libc.a, which
  687.        for all the supported targets would be available in
  688.        /usr/cygnus/progressive/H-host/target/lib/libc.a).
  689.     3) make the change to reloc.c
  690.     4) rebuild the linker
  691.     5) repeat step 2
  692.     6) if the resulting object files are the same, you have at least
  693.        made it no worse
  694.     7) if they are different you have to figure out which version is
  695.        right
  696. */
  697.           relocation -= reloc_entry->addend;
  698. #endif
  699.           reloc_entry->addend = 0;
  700.         }
  701.       else
  702.         {
  703.           reloc_entry->addend = relocation;
  704.         }
  705.     }
  706.     }
  707.   else
  708.     {
  709.       reloc_entry->addend = 0;
  710.     }
  711.  
  712.   /* FIXME: This overflow checking is incomplete, because the value
  713.      might have overflowed before we get here.  For a correct check we
  714.      need to compute the value in a size larger than bitsize, but we
  715.      can't reasonably do that for a reloc the same size as a host
  716.      machine word.
  717.      FIXME: We should also do overflow checking on the result after
  718.      adding in the value contained in the object file.  */
  719.   if (howto->complain_on_overflow != complain_overflow_dont
  720.       && flag == bfd_reloc_ok)
  721.     {
  722.       bfd_vma check;
  723.  
  724.       /* Get the value that will be used for the relocation, but
  725.      starting at bit position zero.  */
  726.       check = relocation >> howto->rightshift;
  727.       switch (howto->complain_on_overflow)
  728.     {
  729.     case complain_overflow_signed:
  730.       {
  731.         /* Assumes two's complement.  */
  732.         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
  733.         bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
  734.  
  735.         /* The above right shift is incorrect for a signed value.
  736.            Fix it up by forcing on the upper bits.  */
  737.         if (howto->rightshift > 0
  738.         && (bfd_signed_vma) relocation < 0)
  739.           check |= ((bfd_vma) - 1
  740.             & ~((bfd_vma) - 1
  741.                 >> howto->rightshift));
  742.         if ((bfd_signed_vma) check > reloc_signed_max
  743.         || (bfd_signed_vma) check < reloc_signed_min)
  744.           flag = bfd_reloc_overflow;
  745.       }
  746.       break;
  747.     case complain_overflow_unsigned:
  748.       {
  749.         /* Assumes two's complement.  This expression avoids
  750.            overflow if howto->bitsize is the number of bits in
  751.            bfd_vma.  */
  752.         bfd_vma reloc_unsigned_max =
  753.         (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  754.  
  755.         if ((bfd_vma) check > reloc_unsigned_max)
  756.           flag = bfd_reloc_overflow;
  757.       }
  758.       break;
  759.     case complain_overflow_bitfield:
  760.       {
  761.         /* Assumes two's complement.  This expression avoids
  762.            overflow if howto->bitsize is the number of bits in
  763.            bfd_vma.  */
  764.         bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  765.  
  766.         if (((bfd_vma) check & ~reloc_bits) != 0
  767.         && ((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
  768.           {
  769.         /* The above right shift is incorrect for a signed
  770.            value.  See if turning on the upper bits fixes the
  771.            overflow.  */
  772.         if (howto->rightshift > 0
  773.             && (bfd_signed_vma) relocation < 0)
  774.           {
  775.             check |= ((bfd_vma) - 1
  776.                   & ~((bfd_vma) - 1
  777.                   >> howto->rightshift));
  778.             if (((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
  779.               flag = bfd_reloc_overflow;
  780.           }
  781.         else
  782.           flag = bfd_reloc_overflow;
  783.           }
  784.       }
  785.       break;
  786.     default:
  787.       abort ();
  788.     }
  789.     }
  790.  
  791.   /*
  792.     Either we are relocating all the way, or we don't want to apply
  793.     the relocation to the reloc entry (probably because there isn't
  794.     any room in the output format to describe addends to relocs)
  795.     */
  796.  
  797.   /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
  798.      (OSF version 1.3, compiler version 3.11).  It miscompiles the
  799.      following program:
  800.  
  801.      struct str
  802.      {
  803.        unsigned int i0;
  804.      } s = { 0 };
  805.  
  806.      int
  807.      main ()
  808.      {
  809.        unsigned long x;
  810.  
  811.        x = 0x100000000;
  812.        x <<= (unsigned long) s.i0;
  813.        if (x == 0)
  814.      printf ("failed\n");
  815.        else
  816.      printf ("succeeded (%lx)\n", x);
  817.      }
  818.      */
  819.  
  820.   relocation >>= (bfd_vma) howto->rightshift;
  821.  
  822.   /* Shift everything up to where it's going to be used */
  823.  
  824.   relocation <<= (bfd_vma) howto->bitpos;
  825.  
  826.   /* Wait for the day when all have the mask in them */
  827.  
  828.   /* What we do:
  829.      i instruction to be left alone
  830.      o offset within instruction
  831.      r relocation offset to apply
  832.      S src mask
  833.      D dst mask
  834.      N ~dst mask
  835.      A part 1
  836.      B part 2
  837.      R result
  838.  
  839.      Do this:
  840.      i i i i i o o o o o        from bfd_get<size>
  841.      and           S S S S S    to get the size offset we want
  842.      +   r r r r r r r r r r  to get the final value to place
  843.      and           D D D D D  to chop to right size
  844.      -----------------------
  845.      A A A A A
  846.      And this:
  847.      ...   i i i i i o o o o o  from bfd_get<size>
  848.      and   N N N N N            get instruction
  849.      -----------------------
  850.      ...   B B B B B
  851.  
  852.      And then:
  853.      B B B B B
  854.      or              A A A A A
  855.      -----------------------
  856.      R R R R R R R R R R        put into bfd_put<size>
  857.      */
  858.  
  859. #define DOIT(x) \
  860.   x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) +  relocation) & howto->dst_mask))
  861.  
  862.   switch (howto->size)
  863.     {
  864.     case 0:
  865.       {
  866.     char x = bfd_get_8 (abfd, (char *) data + addr);
  867.     DOIT (x);
  868.     bfd_put_8 (abfd, x, (unsigned char *) data + addr);
  869.       }
  870.       break;
  871.  
  872.     case 1:
  873.       if (relocation)
  874.     {
  875.       short x = bfd_get_16 (abfd, (bfd_byte *) data + addr);
  876.       DOIT (x);
  877.       bfd_put_16 (abfd, x, (unsigned char *) data + addr);
  878.     }
  879.       break;
  880.     case 2:
  881.       if (relocation)
  882.     {
  883.       long x = bfd_get_32 (abfd, (bfd_byte *) data + addr);
  884.       DOIT (x);
  885.       bfd_put_32 (abfd, x, (bfd_byte *) data + addr);
  886.     }
  887.       break;
  888.     case -2:
  889.       {
  890.     long x = bfd_get_32 (abfd, (bfd_byte *) data + addr);
  891.     relocation = -relocation;
  892.     DOIT (x);
  893.     bfd_put_32 (abfd, x, (bfd_byte *) data + addr);
  894.       }
  895.       break;
  896.  
  897.     case -1:
  898.       {
  899.     long x = bfd_get_16 (abfd, (bfd_byte *) data + addr);
  900.     relocation = -relocation;
  901.     DOIT (x);
  902.     bfd_put_16 (abfd, x, (bfd_byte *) data + addr);
  903.       }
  904.       break;
  905.  
  906.     case 3:
  907.       /* Do nothing */
  908.       break;
  909.  
  910.     case 4:
  911. #ifdef BFD64
  912.       if (relocation)
  913.     {
  914.       bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data + addr);
  915.       DOIT (x);
  916.       bfd_put_64 (abfd, x, (bfd_byte *) data + addr);
  917.     }
  918. #else
  919.       abort ();
  920. #endif
  921.       break;
  922.     default:
  923.       return bfd_reloc_other;
  924.     }
  925.  
  926.   return flag;
  927. }
  928.  
  929. /*
  930. FUNCTION
  931.     bfd_install_relocation
  932.  
  933. SYNOPSIS
  934.     bfd_reloc_status_type
  935.                 bfd_install_relocation
  936.                         (bfd *abfd,
  937.                          arelent *reloc_entry,
  938.                          PTR data, bfd_vma data_start,
  939.                          asection *input_section,
  940.              char **error_message);
  941.  
  942. DESCRIPTION
  943.     This looks remarkably like <<bfd_perform_relocation>>, except it
  944.     does not expect that the section contents have been filled in.
  945.     I.e., it's suitable for use when creating, rather than applying
  946.     a relocation.
  947.  
  948.     For now, this function should be considered reserved for the
  949.     assembler.
  950.  
  951. */
  952.  
  953.  
  954. bfd_reloc_status_type
  955. bfd_install_relocation (abfd, reloc_entry, data_start, data_start_offset,
  956.             input_section, error_message)
  957.      bfd *abfd;
  958.      arelent *reloc_entry;
  959.      PTR data_start;
  960.      bfd_vma data_start_offset;
  961.      asection *input_section;
  962.      char **error_message;
  963. {
  964.   bfd_vma relocation;
  965.   bfd_reloc_status_type flag = bfd_reloc_ok;
  966.   bfd_size_type addr = reloc_entry->address;
  967.   bfd_vma output_base = 0;
  968.   reloc_howto_type *howto = reloc_entry->howto;
  969.   asection *reloc_target_output_section;
  970.   asymbol *symbol;
  971.   bfd_byte *data;
  972.  
  973.   symbol = *(reloc_entry->sym_ptr_ptr);
  974.   if (bfd_is_abs_section (symbol->section))
  975.     {
  976.       reloc_entry->address += input_section->output_offset;
  977.       return bfd_reloc_ok;
  978.     }
  979.  
  980.   /* If there is a function supplied to handle this relocation type,
  981.      call it.  It'll return `bfd_reloc_continue' if further processing
  982.      can be done.  */
  983.   if (howto->special_function)
  984.     {
  985.       bfd_reloc_status_type cont;
  986.       /* XXX - The special_function calls haven't been fixed up to deal
  987.      with creating new relocations and section contents.  */
  988.       cont = howto->special_function (abfd, reloc_entry, symbol,
  989.                       /* XXX - Non-portable! */
  990.                       ((bfd_byte *) data_start
  991.                        - data_start_offset),
  992.                       input_section, abfd, error_message);
  993.       if (cont != bfd_reloc_continue)
  994.     return cont;
  995.     }
  996.  
  997.   /* Is the address of the relocation really within the section?  */
  998.   if (reloc_entry->address > input_section->_cooked_size)
  999.     return bfd_reloc_outofrange;
  1000.  
  1001.   /* Work out which section the relocation is targetted at and the
  1002.      initial relocation command value.  */
  1003.  
  1004.   /* Get symbol value.  (Common symbols are special.)  */
  1005.   if (bfd_is_com_section (symbol->section))
  1006.     relocation = 0;
  1007.   else
  1008.     relocation = symbol->value;
  1009.  
  1010.  
  1011.   reloc_target_output_section = symbol->section->output_section;
  1012.  
  1013.   /* Convert input-section-relative symbol value to absolute.  */
  1014.   if (howto->partial_inplace == false)
  1015.     output_base = 0;
  1016.   else
  1017.     output_base = reloc_target_output_section->vma;
  1018.  
  1019.   relocation += output_base + symbol->section->output_offset;
  1020.  
  1021.   /* Add in supplied addend.  */
  1022.   relocation += reloc_entry->addend;
  1023.  
  1024.   /* Here the variable relocation holds the final address of the
  1025.      symbol we are relocating against, plus any addend.  */
  1026.  
  1027.   if (howto->pc_relative == true)
  1028.     {
  1029.       /* This is a PC relative relocation.  We want to set RELOCATION
  1030.      to the distance between the address of the symbol and the
  1031.      location.  RELOCATION is already the address of the symbol.
  1032.  
  1033.      We start by subtracting the address of the section containing
  1034.      the location.
  1035.  
  1036.      If pcrel_offset is set, we must further subtract the position
  1037.      of the location within the section.  Some targets arrange for
  1038.      the addend to be the negative of the position of the location
  1039.      within the section; for example, i386-aout does this.  For
  1040.      i386-aout, pcrel_offset is false.  Some other targets do not
  1041.      include the position of the location; for example, m88kbcs,
  1042.      or ELF.  For those targets, pcrel_offset is true.
  1043.  
  1044.      If we are producing relocateable output, then we must ensure
  1045.      that this reloc will be correctly computed when the final
  1046.      relocation is done.  If pcrel_offset is false we want to wind
  1047.      up with the negative of the location within the section,
  1048.      which means we must adjust the existing addend by the change
  1049.      in the location within the section.  If pcrel_offset is true
  1050.      we do not want to adjust the existing addend at all.
  1051.  
  1052.      FIXME: This seems logical to me, but for the case of
  1053.      producing relocateable output it is not what the code
  1054.      actually does.  I don't want to change it, because it seems
  1055.      far too likely that something will break.  */
  1056.  
  1057.       relocation -=
  1058.     input_section->output_section->vma + input_section->output_offset;
  1059.  
  1060.       if (howto->pcrel_offset == true && howto->partial_inplace == true)
  1061.     relocation -= reloc_entry->address;
  1062.     }
  1063.  
  1064.   if (howto->partial_inplace == false)
  1065.     {
  1066.       /* This is a partial relocation, and we want to apply the relocation
  1067.      to the reloc entry rather than the raw data. Modify the reloc
  1068.      inplace to reflect what we now know.  */
  1069.       reloc_entry->addend = relocation;
  1070.       reloc_entry->address += input_section->output_offset;
  1071.       return flag;
  1072.     }
  1073.   else
  1074.     {
  1075.       /* This is a partial relocation, but inplace, so modify the
  1076.      reloc record a bit.
  1077.      
  1078.      If we've relocated with a symbol with a section, change
  1079.      into a ref to the section belonging to the symbol.  */
  1080.       
  1081.       reloc_entry->address += input_section->output_offset;
  1082.       
  1083.       /* WTF?? */
  1084.       if (abfd->xvec->flavour == bfd_target_coff_flavour
  1085.       && strcmp (abfd->xvec->name, "aixcoff-rs6000") != 0
  1086.       && strcmp (abfd->xvec->name, "coff-Intel-little") != 0
  1087.       && strcmp (abfd->xvec->name, "coff-Intel-big") != 0)
  1088.     {
  1089. #if 1
  1090. /* For m68k-coff, the addend was being subtracted twice during
  1091.    relocation with -r.  Removing the line below this comment
  1092.    fixes that problem; see PR 2953.
  1093.          
  1094. However, Ian wrote the following, regarding removing the line below,
  1095. which explains why it is still enabled:  --djm
  1096.          
  1097. If you put a patch like that into BFD you need to check all the COFF
  1098. linkers.  I am fairly certain that patch will break coff-i386 (e.g.,
  1099. SCO); see coff_i386_reloc in coff-i386.c where I worked around the
  1100. problem in a different way.  There may very well be a reason that the
  1101. code works as it does.
  1102.  
  1103. Hmmm.  The first obvious point is that bfd_install_relocation should
  1104. not have any tests that depend upon the flavour.  It's seem like
  1105. entirely the wrong place for such a thing.  The second obvious point
  1106. is that the current code ignores the reloc addend when producing
  1107. relocateable output for COFF.  That's peculiar.  In fact, I really
  1108. have no idea what the point of the line you want to remove is.
  1109.  
  1110. A typical COFF reloc subtracts the old value of the symbol and adds in
  1111. the new value to the location in the object file (if it's a pc
  1112. relative reloc it adds the difference between the symbol value and the
  1113. location).  When relocating we need to preserve that property.
  1114.  
  1115. BFD handles this by setting the addend to the negative of the old
  1116. value of the symbol.  Unfortunately it handles common symbols in a
  1117. non-standard way (it doesn't subtract the old value) but that's a
  1118. different story (we can't change it without losing backward
  1119. compatibility with old object files) (coff-i386 does subtract the old
  1120. value, to be compatible with existing coff-i386 targets, like SCO).
  1121.  
  1122. So everything works fine when not producing relocateable output.  When
  1123. we are producing relocateable output, logically we should do exactly
  1124. what we do when not producing relocateable output.  Therefore, your
  1125. patch is correct.  In fact, it should probably always just set
  1126. reloc_entry->addend to 0 for all cases, since it is, in fact, going to
  1127. add the value into the object file.  This won't hurt the COFF code,
  1128. which doesn't use the addend; I'm not sure what it will do to other
  1129. formats (the thing to check for would be whether any formats both use
  1130. the addend and set partial_inplace).
  1131.  
  1132. When I wanted to make coff-i386 produce relocateable output, I ran
  1133. into the problem that you are running into: I wanted to remove that
  1134. line.  Rather than risk it, I made the coff-i386 relocs use a special
  1135. function; it's coff_i386_reloc in coff-i386.c.  The function
  1136. specifically adds the addend field into the object file, knowing that
  1137. bfd_install_relocation is not going to.  If you remove that line, then
  1138. coff-i386.c will wind up adding the addend field in twice.  It's
  1139. trivial to fix; it just needs to be done.
  1140.  
  1141. The problem with removing the line is just that it may break some
  1142. working code.  With BFD it's hard to be sure of anything.  The right
  1143. way to deal with this is simply to build and test at least all the
  1144. supported COFF targets.  It should be straightforward if time and disk
  1145. space consuming.  For each target:
  1146.     1) build the linker
  1147.     2) generate some executable, and link it using -r (I would
  1148.        probably use paranoia.o and link against newlib/libc.a, which
  1149.        for all the supported targets would be available in
  1150.        /usr/cygnus/progressive/H-host/target/lib/libc.a).
  1151.     3) make the change to reloc.c
  1152.     4) rebuild the linker
  1153.     5) repeat step 2
  1154.     6) if the resulting object files are the same, you have at least
  1155.        made it no worse
  1156.     7) if they are different you have to figure out which version is
  1157.        right
  1158. */
  1159.       relocation -= reloc_entry->addend;
  1160. #endif
  1161.       reloc_entry->addend = 0;
  1162.     }
  1163.       else
  1164.     {
  1165.       reloc_entry->addend = relocation;
  1166.     }
  1167.     }
  1168.  
  1169.   /* FIXME: This overflow checking is incomplete, because the value
  1170.      might have overflowed before we get here.  For a correct check we
  1171.      need to compute the value in a size larger than bitsize, but we
  1172.      can't reasonably do that for a reloc the same size as a host
  1173.      machine word.
  1174.  
  1175.      FIXME: We should also do overflow checking on the result after
  1176.      adding in the value contained in the object file.  */
  1177.   if (howto->complain_on_overflow != complain_overflow_dont)
  1178.     {
  1179.       bfd_vma check;
  1180.  
  1181.       /* Get the value that will be used for the relocation, but
  1182.      starting at bit position zero.  */
  1183.       check = relocation >> howto->rightshift;
  1184.       switch (howto->complain_on_overflow)
  1185.     {
  1186.     case complain_overflow_signed:
  1187.       {
  1188.         /* Assumes two's complement.  */
  1189.         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
  1190.         bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
  1191.  
  1192.         /* The above right shift is incorrect for a signed value.
  1193.            Fix it up by forcing on the upper bits.  */
  1194.         if (howto->rightshift > 0
  1195.         && (bfd_signed_vma) relocation < 0)
  1196.           check |= ((bfd_vma) - 1
  1197.             & ~((bfd_vma) - 1
  1198.                 >> howto->rightshift));
  1199.         if ((bfd_signed_vma) check > reloc_signed_max
  1200.         || (bfd_signed_vma) check < reloc_signed_min)
  1201.           flag = bfd_reloc_overflow;
  1202.       }
  1203.       break;
  1204.     case complain_overflow_unsigned:
  1205.       {
  1206.         /* Assumes two's complement.  This expression avoids
  1207.            overflow if howto->bitsize is the number of bits in
  1208.            bfd_vma.  */
  1209.         bfd_vma reloc_unsigned_max =
  1210.         (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  1211.  
  1212.         if ((bfd_vma) check > reloc_unsigned_max)
  1213.           flag = bfd_reloc_overflow;
  1214.       }
  1215.       break;
  1216.     case complain_overflow_bitfield:
  1217.       {
  1218.         /* Assumes two's complement.  This expression avoids
  1219.            overflow if howto->bitsize is the number of bits in
  1220.            bfd_vma.  */
  1221.         bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  1222.  
  1223.         if (((bfd_vma) check & ~reloc_bits) != 0
  1224.         && ((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
  1225.           {
  1226.         /* The above right shift is incorrect for a signed
  1227.            value.  See if turning on the upper bits fixes the
  1228.            overflow.  */
  1229.         if (howto->rightshift > 0
  1230.             && (bfd_signed_vma) relocation < 0)
  1231.           {
  1232.             check |= ((bfd_vma) - 1
  1233.                   & ~((bfd_vma) - 1
  1234.                   >> howto->rightshift));
  1235.             if (((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
  1236.               flag = bfd_reloc_overflow;
  1237.           }
  1238.         else
  1239.           flag = bfd_reloc_overflow;
  1240.           }
  1241.       }
  1242.       break;
  1243.     default:
  1244.       abort ();
  1245.     }
  1246.     }
  1247.  
  1248.   /*
  1249.     Either we are relocating all the way, or we don't want to apply
  1250.     the relocation to the reloc entry (probably because there isn't
  1251.     any room in the output format to describe addends to relocs)
  1252.     */
  1253.  
  1254.   /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
  1255.      (OSF version 1.3, compiler version 3.11).  It miscompiles the
  1256.      following program:
  1257.  
  1258.      struct str
  1259.      {
  1260.        unsigned int i0;
  1261.      } s = { 0 };
  1262.  
  1263.      int
  1264.      main ()
  1265.      {
  1266.        unsigned long x;
  1267.  
  1268.        x = 0x100000000;
  1269.        x <<= (unsigned long) s.i0;
  1270.        if (x == 0)
  1271.      printf ("failed\n");
  1272.        else
  1273.      printf ("succeeded (%lx)\n", x);
  1274.      }
  1275.      */
  1276.  
  1277.   relocation >>= (bfd_vma) howto->rightshift;
  1278.  
  1279.   /* Shift everything up to where it's going to be used */
  1280.  
  1281.   relocation <<= (bfd_vma) howto->bitpos;
  1282.  
  1283.   /* Wait for the day when all have the mask in them */
  1284.  
  1285.   /* What we do:
  1286.      i instruction to be left alone
  1287.      o offset within instruction
  1288.      r relocation offset to apply
  1289.      S src mask
  1290.      D dst mask
  1291.      N ~dst mask
  1292.      A part 1
  1293.      B part 2
  1294.      R result
  1295.  
  1296.      Do this:
  1297.      i i i i i o o o o o        from bfd_get<size>
  1298.      and           S S S S S    to get the size offset we want
  1299.      +   r r r r r r r r r r  to get the final value to place
  1300.      and           D D D D D  to chop to right size
  1301.      -----------------------
  1302.      A A A A A
  1303.      And this:
  1304.      ...   i i i i i o o o o o  from bfd_get<size>
  1305.      and   N N N N N            get instruction
  1306.      -----------------------
  1307.      ...   B B B B B
  1308.  
  1309.      And then:
  1310.      B B B B B
  1311.      or              A A A A A
  1312.      -----------------------
  1313.      R R R R R R R R R R        put into bfd_put<size>
  1314.      */
  1315.  
  1316. #define DOIT(x) \
  1317.   x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) +  relocation) & howto->dst_mask))
  1318.  
  1319.   data = (bfd_byte *) data_start + (addr - data_start_offset);
  1320.  
  1321.   switch (howto->size)
  1322.     {
  1323.     case 0:
  1324.       {
  1325.     char x = bfd_get_8 (abfd, (char *) data);
  1326.     DOIT (x);
  1327.     bfd_put_8 (abfd, x, (unsigned char *) data);
  1328.       }
  1329.       break;
  1330.  
  1331.     case 1:
  1332.       if (relocation)
  1333.     {
  1334.       short x = bfd_get_16 (abfd, (bfd_byte *) data);
  1335.       DOIT (x);
  1336.       bfd_put_16 (abfd, x, (unsigned char *) data);
  1337.     }
  1338.       break;
  1339.     case 2:
  1340.       if (relocation)
  1341.     {
  1342.       long x = bfd_get_32 (abfd, (bfd_byte *) data);
  1343.       DOIT (x);
  1344.       bfd_put_32 (abfd, x, (bfd_byte *) data);
  1345.     }
  1346.       break;
  1347.     case -2:
  1348.       {
  1349.     long x = bfd_get_32 (abfd, (bfd_byte *) data);
  1350.     relocation = -relocation;
  1351.     DOIT (x);
  1352.     bfd_put_32 (abfd, x, (bfd_byte *) data);
  1353.       }
  1354.       break;
  1355.  
  1356.     case 3:
  1357.       /* Do nothing */
  1358.       break;
  1359.  
  1360.     case 4:
  1361.       if (relocation)
  1362.     {
  1363.       bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data);
  1364.       DOIT (x);
  1365.       bfd_put_64 (abfd, x, (bfd_byte *) data);
  1366.     }
  1367.       break;
  1368.     default:
  1369.       return bfd_reloc_other;
  1370.     }
  1371.  
  1372.   return flag;
  1373. }
  1374.  
  1375. /* This relocation routine is used by some of the backend linkers.
  1376.    They do not construct asymbol or arelent structures, so there is no
  1377.    reason for them to use bfd_perform_relocation.  Also,
  1378.    bfd_perform_relocation is so hacked up it is easier to write a new
  1379.    function than to try to deal with it.
  1380.  
  1381.    This routine does a final relocation.  It should not be used when
  1382.    generating relocateable output.
  1383.  
  1384.    FIXME: This routine ignores any special_function in the HOWTO,
  1385.    since the existing special_function values have been written for
  1386.    bfd_perform_relocation.
  1387.  
  1388.    HOWTO is the reloc howto information.
  1389.    INPUT_BFD is the BFD which the reloc applies to.
  1390.    INPUT_SECTION is the section which the reloc applies to.
  1391.    CONTENTS is the contents of the section.
  1392.    ADDRESS is the address of the reloc within INPUT_SECTION.
  1393.    VALUE is the value of the symbol the reloc refers to.
  1394.    ADDEND is the addend of the reloc.  */
  1395.  
  1396. bfd_reloc_status_type
  1397. _bfd_final_link_relocate (howto, input_bfd, input_section, contents, address,
  1398.               value, addend)
  1399.      reloc_howto_type *howto;
  1400.      bfd *input_bfd;
  1401.      asection *input_section;
  1402.      bfd_byte *contents;
  1403.      bfd_vma address;
  1404.      bfd_vma value;
  1405.      bfd_vma addend;
  1406. {
  1407.   bfd_vma relocation;
  1408.  
  1409.   /* Sanity check the address.  */
  1410.   if (address > input_section->_cooked_size)
  1411.     return bfd_reloc_outofrange;
  1412.  
  1413.   /* This function assumes that we are dealing with a basic relocation
  1414.      against a symbol.  We want to compute the value of the symbol to
  1415.      relocate to.  This is just VALUE, the value of the symbol, plus
  1416.      ADDEND, any addend associated with the reloc.  */
  1417.   relocation = value + addend;
  1418.  
  1419.   /* If the relocation is PC relative, we want to set RELOCATION to
  1420.      the distance between the symbol (currently in RELOCATION) and the
  1421.      location we are relocating.  Some targets (e.g., i386-aout)
  1422.      arrange for the contents of the section to be the negative of the
  1423.      offset of the location within the section; for such targets
  1424.      pcrel_offset is false.  Other targets (e.g., m88kbcs or ELF)
  1425.      simply leave the contents of the section as zero; for such
  1426.      targets pcrel_offset is true.  If pcrel_offset is false we do not
  1427.      need to subtract out the offset of the location within the
  1428.      section (which is just ADDRESS).  */
  1429.   if (howto->pc_relative)
  1430.     {
  1431.       relocation -= (input_section->output_section->vma
  1432.              + input_section->output_offset);
  1433.       if (howto->pcrel_offset)
  1434.     relocation -= address;
  1435.     }
  1436.  
  1437.   return _bfd_relocate_contents (howto, input_bfd, relocation,
  1438.                  contents + address);
  1439. }
  1440.  
  1441. /* Relocate a given location using a given value and howto.  */
  1442.  
  1443. bfd_reloc_status_type
  1444. _bfd_relocate_contents (howto, input_bfd, relocation, location)
  1445.      reloc_howto_type *howto;
  1446.      bfd *input_bfd;
  1447.      bfd_vma relocation;
  1448.      bfd_byte *location;
  1449. {
  1450.   int size;
  1451.   bfd_vma x;
  1452.   boolean overflow;
  1453.  
  1454.   /* If the size is negative, negate RELOCATION.  This isn't very
  1455.      general.  */
  1456.   if (howto->size < 0)
  1457.     relocation = -relocation;
  1458.  
  1459.   /* Get the value we are going to relocate.  */
  1460.   size = bfd_get_reloc_size (howto);
  1461.   switch (size)
  1462.     {
  1463.     default:
  1464.     case 0:
  1465.       abort ();
  1466.     case 1:
  1467.       x = bfd_get_8 (input_bfd, location);
  1468.       break;
  1469.     case 2:
  1470.       x = bfd_get_16 (input_bfd, location);
  1471.       break;
  1472.     case 4:
  1473.       x = bfd_get_32 (input_bfd, location);
  1474.       break;
  1475.     case 8:
  1476. #ifdef BFD64
  1477.       x = bfd_get_64 (input_bfd, location);
  1478. #else
  1479.       abort ();
  1480. #endif
  1481.       break;
  1482.     }
  1483.  
  1484.   /* Check for overflow.  FIXME: We may drop bits during the addition
  1485.      which we don't check for.  We must either check at every single
  1486.      operation, which would be tedious, or we must do the computations
  1487.      in a type larger than bfd_vma, which would be inefficient.  */
  1488.   overflow = false;
  1489.   if (howto->complain_on_overflow != complain_overflow_dont)
  1490.     {
  1491.       bfd_vma check;
  1492.       bfd_signed_vma signed_check;
  1493.       bfd_vma add;
  1494.       bfd_signed_vma signed_add;
  1495.  
  1496.       if (howto->rightshift == 0)
  1497.     {
  1498.       check = relocation;
  1499.       signed_check = (bfd_signed_vma) relocation;
  1500.     }
  1501.       else
  1502.     {
  1503.       /* Drop unwanted bits from the value we are relocating to.  */
  1504.       check = relocation >> howto->rightshift;
  1505.  
  1506.       /* If this is a signed value, the rightshift just dropped
  1507.          leading 1 bits (assuming twos complement).  */
  1508.       if ((bfd_signed_vma) relocation >= 0)
  1509.         signed_check = check;
  1510.       else
  1511.         signed_check = (check
  1512.                 | ((bfd_vma) - 1
  1513.                    & ~((bfd_vma) - 1 >> howto->rightshift)));
  1514.     }
  1515.  
  1516.       /* Get the value from the object file.  */
  1517.       add = x & howto->src_mask;
  1518.  
  1519.       /* Get the value from the object file with an appropriate sign.
  1520.      The expression involving howto->src_mask isolates the upper
  1521.      bit of src_mask.  If that bit is set in the value we are
  1522.      adding, it is negative, and we subtract out that number times
  1523.      two.  If src_mask includes the highest possible bit, then we
  1524.      can not get the upper bit, but that does not matter since
  1525.      signed_add needs no adjustment to become negative in that
  1526.      case.  */
  1527.       signed_add = add;
  1528.       if ((add & (((~howto->src_mask) >> 1) & howto->src_mask)) != 0)
  1529.     signed_add -= (((~howto->src_mask) >> 1) & howto->src_mask) << 1;
  1530.  
  1531.       /* Add the value from the object file, shifted so that it is a
  1532.      straight number.  */
  1533.       if (howto->bitpos == 0)
  1534.     {
  1535.       check += add;
  1536.       signed_check += signed_add;
  1537.     }
  1538.       else
  1539.     {
  1540.       check += add >> howto->bitpos;
  1541.  
  1542.       /* For the signed case we use ADD, rather than SIGNED_ADD,
  1543.          to avoid warnings from SVR4 cc.  This is OK since we
  1544.          explictly handle the sign bits.  */
  1545.       if (signed_add >= 0)
  1546.         signed_check += add >> howto->bitpos;
  1547.       else
  1548.         signed_check += ((add >> howto->bitpos)
  1549.                  | ((bfd_vma) - 1
  1550.                 & ~((bfd_vma) - 1 >> howto->bitpos)));
  1551.     }
  1552.  
  1553.       switch (howto->complain_on_overflow)
  1554.     {
  1555.     case complain_overflow_signed:
  1556.       {
  1557.         /* Assumes two's complement.  */
  1558.         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
  1559.         bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
  1560.  
  1561.         if (signed_check > reloc_signed_max
  1562.         || signed_check < reloc_signed_min)
  1563.           overflow = true;
  1564.       }
  1565.       break;
  1566.     case complain_overflow_unsigned:
  1567.       {
  1568.         /* Assumes two's complement.  This expression avoids
  1569.            overflow if howto->bitsize is the number of bits in
  1570.            bfd_vma.  */
  1571.         bfd_vma reloc_unsigned_max =
  1572.         (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  1573.  
  1574.         if (check > reloc_unsigned_max)
  1575.           overflow = true;
  1576.       }
  1577.       break;
  1578.     case complain_overflow_bitfield:
  1579.       {
  1580.         /* Assumes two's complement.  This expression avoids
  1581.            overflow if howto->bitsize is the number of bits in
  1582.            bfd_vma.  */
  1583.         bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  1584.  
  1585.         if ((check & ~reloc_bits) != 0
  1586.         && (((bfd_vma) signed_check & ~reloc_bits)
  1587.             != (-1 & ~reloc_bits)))
  1588.           overflow = true;
  1589.       }
  1590.       break;
  1591.     default:
  1592.       abort ();
  1593.     }
  1594.     }
  1595.  
  1596.   /* Put RELOCATION in the right bits.  */
  1597.   relocation >>= (bfd_vma) howto->rightshift;
  1598.   relocation <<= (bfd_vma) howto->bitpos;
  1599.  
  1600.   /* Add RELOCATION to the right bits of X.  */
  1601.   x = ((x & ~howto->dst_mask)
  1602.        | (((x & howto->src_mask) + relocation) & howto->dst_mask));
  1603.  
  1604.   /* Put the relocated value back in the object file.  */
  1605.   switch (size)
  1606.     {
  1607.     default:
  1608.     case 0:
  1609.       abort ();
  1610.     case 1:
  1611.       bfd_put_8 (input_bfd, x, location);
  1612.       break;
  1613.     case 2:
  1614.       bfd_put_16 (input_bfd, x, location);
  1615.       break;
  1616.     case 4:
  1617.       bfd_put_32 (input_bfd, x, location);
  1618.       break;
  1619.     case 8:
  1620. #ifdef BFD64
  1621.       bfd_put_64 (input_bfd, x, location);
  1622. #else
  1623.       abort ();
  1624. #endif
  1625.       break;
  1626.     }
  1627.  
  1628.   return overflow ? bfd_reloc_overflow : bfd_reloc_ok;
  1629. }
  1630.  
  1631. /*
  1632. DOCDD
  1633. INODE
  1634.     howto manager,  , typedef arelent, Relocations
  1635.  
  1636. SECTION
  1637.     The howto manager
  1638.  
  1639.     When an application wants to create a relocation, but doesn't
  1640.     know what the target machine might call it, it can find out by
  1641.     using this bit of code.
  1642.  
  1643. */
  1644.  
  1645. /*
  1646. TYPEDEF
  1647.     bfd_reloc_code_type
  1648.  
  1649. DESCRIPTION
  1650.     The insides of a reloc code.  The idea is that, eventually, there
  1651.     will be one enumerator for every type of relocation we ever do.
  1652.     Pass one of these values to <<bfd_reloc_type_lookup>>, and it'll
  1653.     return a howto pointer.
  1654.  
  1655.     This does mean that the application must determine the correct
  1656.     enumerator value; you can't get a howto pointer from a random set
  1657.     of attributes.
  1658.  
  1659. SENUM
  1660.    bfd_reloc_code_real
  1661.  
  1662. ENUM
  1663.   BFD_RELOC_64
  1664. ENUMX
  1665.   BFD_RELOC_32
  1666. ENUMX
  1667.   BFD_RELOC_26
  1668. ENUMX
  1669.   BFD_RELOC_16
  1670. ENUMX
  1671.   BFD_RELOC_14
  1672. ENUMX
  1673.   BFD_RELOC_8
  1674. ENUMDOC
  1675.   Basic absolute relocations of N bits.
  1676.  
  1677. ENUM
  1678.   BFD_RELOC_64_PCREL
  1679. ENUMX
  1680.   BFD_RELOC_32_PCREL
  1681. ENUMX
  1682.   BFD_RELOC_24_PCREL
  1683. ENUMX
  1684.   BFD_RELOC_16_PCREL
  1685. ENUMX
  1686.   BFD_RELOC_12_PCREL
  1687. ENUMX
  1688.   BFD_RELOC_8_PCREL
  1689. ENUMDOC
  1690.   PC-relative relocations.  Sometimes these are relative to the address
  1691. of the relocation itself; sometimes they are relative to the start of
  1692. the section containing the relocation.  It depends on the specific target.
  1693.  
  1694. The 24-bit relocation is used in some Intel 960 configurations.
  1695.  
  1696. ENUM
  1697.   BFD_RELOC_32_GOT_PCREL
  1698. ENUMX
  1699.   BFD_RELOC_16_GOT_PCREL
  1700. ENUMX
  1701.   BFD_RELOC_8_GOT_PCREL
  1702. ENUMX
  1703.   BFD_RELOC_32_GOTOFF
  1704. ENUMX
  1705.   BFD_RELOC_16_GOTOFF
  1706. ENUMX
  1707.   BFD_RELOC_LO16_GOTOFF
  1708. ENUMX
  1709.   BFD_RELOC_HI16_GOTOFF
  1710. ENUMX
  1711.   BFD_RELOC_HI16_S_GOTOFF
  1712. ENUMX
  1713.   BFD_RELOC_8_GOTOFF
  1714. ENUMX
  1715.   BFD_RELOC_32_PLT_PCREL
  1716. ENUMX
  1717.   BFD_RELOC_24_PLT_PCREL
  1718. ENUMX
  1719.   BFD_RELOC_16_PLT_PCREL
  1720. ENUMX
  1721.   BFD_RELOC_8_PLT_PCREL
  1722. ENUMX
  1723.   BFD_RELOC_32_PLTOFF
  1724. ENUMX
  1725.   BFD_RELOC_16_PLTOFF
  1726. ENUMX
  1727.   BFD_RELOC_LO16_PLTOFF
  1728. ENUMX
  1729.   BFD_RELOC_HI16_PLTOFF
  1730. ENUMX
  1731.   BFD_RELOC_HI16_S_PLTOFF
  1732. ENUMX
  1733.   BFD_RELOC_8_PLTOFF
  1734. ENUMDOC
  1735.   For ELF.
  1736.  
  1737. ENUM
  1738.   BFD_RELOC_68K_GLOB_DAT
  1739. ENUMX
  1740.   BFD_RELOC_68K_JMP_SLOT
  1741. ENUMX
  1742.   BFD_RELOC_68K_RELATIVE
  1743. ENUMDOC
  1744.   Relocations used by 68K ELF.
  1745.  
  1746. ENUM
  1747.   BFD_RELOC_32_BASEREL
  1748. ENUMX
  1749.   BFD_RELOC_16_BASEREL
  1750. ENUMX
  1751.   BFD_RELOC_LO16_BASEREL
  1752. ENUMX
  1753.   BFD_RELOC_HI16_BASEREL
  1754. ENUMX
  1755.   BFD_RELOC_HI16_S_BASEREL
  1756. ENUMX
  1757.   BFD_RELOC_8_BASEREL
  1758. ENUMX
  1759.   BFD_RELOC_RVA
  1760. ENUMDOC
  1761.   Linkage-table relative.
  1762.  
  1763. ENUM
  1764.   BFD_RELOC_8_FFnn
  1765. ENUMDOC
  1766.   Absolute 8-bit relocation, but used to form an address like 0xFFnn.
  1767.  
  1768. ENUM
  1769.   BFD_RELOC_32_PCREL_S2
  1770. ENUMX
  1771.   BFD_RELOC_16_PCREL_S2
  1772. ENUMX
  1773.   BFD_RELOC_23_PCREL_S2
  1774. ENUMDOC
  1775.   These PC-relative relocations are stored as word displacements --
  1776. i.e., byte displacements shifted right two bits.  The 30-bit word
  1777. displacement (<<32_PCREL_S2>> -- 32 bits, shifted 2) is used on the
  1778. SPARC.  (SPARC tools generally refer to this as <<WDISP30>>.)  The
  1779. signed 16-bit displacement is used on the MIPS, and the 23-bit
  1780. displacement is used on the Alpha.
  1781.  
  1782. ENUM
  1783.   BFD_RELOC_HI22
  1784. ENUMX
  1785.   BFD_RELOC_LO10
  1786. ENUMDOC
  1787.   High 22 bits and low 10 bits of 32-bit value, placed into lower bits of
  1788. the target word.  These are used on the SPARC.
  1789.  
  1790. ENUM
  1791.   BFD_RELOC_GPREL16
  1792. ENUMX
  1793.   BFD_RELOC_GPREL32
  1794. ENUMDOC
  1795.   For systems that allocate a Global Pointer register, these are
  1796. displacements off that register.  These relocation types are
  1797. handled specially, because the value the register will have is
  1798. decided relatively late.
  1799.  
  1800.  
  1801. ENUM
  1802.   BFD_RELOC_I960_CALLJ
  1803. ENUMDOC
  1804.   Reloc types used for i960/b.out.
  1805.  
  1806. ENUM
  1807.   BFD_RELOC_NONE
  1808. ENUMX
  1809.   BFD_RELOC_SPARC_WDISP22
  1810. ENUMX
  1811.   BFD_RELOC_SPARC22
  1812. ENUMX
  1813.   BFD_RELOC_SPARC13
  1814. ENUMX
  1815.   BFD_RELOC_SPARC_GOT10
  1816. ENUMX
  1817.   BFD_RELOC_SPARC_GOT13
  1818. ENUMX
  1819.   BFD_RELOC_SPARC_GOT22
  1820. ENUMX
  1821.   BFD_RELOC_SPARC_PC10
  1822. ENUMX
  1823.   BFD_RELOC_SPARC_PC22
  1824. ENUMX
  1825.   BFD_RELOC_SPARC_WPLT30
  1826. ENUMX
  1827.   BFD_RELOC_SPARC_COPY
  1828. ENUMX
  1829.   BFD_RELOC_SPARC_GLOB_DAT
  1830. ENUMX
  1831.   BFD_RELOC_SPARC_JMP_SLOT
  1832. ENUMX
  1833.   BFD_RELOC_SPARC_RELATIVE
  1834. ENUMX
  1835.   BFD_RELOC_SPARC_UA32
  1836. ENUMDOC
  1837.   SPARC ELF relocations.  There is probably some overlap with other
  1838.   relocation types already defined.
  1839.  
  1840. ENUM
  1841.   BFD_RELOC_SPARC_BASE13
  1842. ENUMX
  1843.   BFD_RELOC_SPARC_BASE22
  1844. ENUMDOC
  1845.   I think these are specific to SPARC a.out (e.g., Sun 4).
  1846.  
  1847. ENUMEQ
  1848.   BFD_RELOC_SPARC_64
  1849.   BFD_RELOC_64
  1850. ENUMX
  1851.   BFD_RELOC_SPARC_10
  1852. ENUMX
  1853.   BFD_RELOC_SPARC_11
  1854. ENUMX
  1855.   BFD_RELOC_SPARC_OLO10
  1856. ENUMX
  1857.   BFD_RELOC_SPARC_HH22
  1858. ENUMX
  1859.   BFD_RELOC_SPARC_HM10
  1860. ENUMX
  1861.   BFD_RELOC_SPARC_LM22
  1862. ENUMX
  1863.   BFD_RELOC_SPARC_PC_HH22
  1864. ENUMX
  1865.   BFD_RELOC_SPARC_PC_HM10
  1866. ENUMX
  1867.   BFD_RELOC_SPARC_PC_LM22
  1868. ENUMX
  1869.   BFD_RELOC_SPARC_WDISP16
  1870. ENUMX
  1871.   BFD_RELOC_SPARC_WDISP19
  1872. ENUMX
  1873.   BFD_RELOC_SPARC_GLOB_JMP
  1874. ENUMX
  1875.   BFD_RELOC_SPARC_7
  1876. ENUMX
  1877.   BFD_RELOC_SPARC_6
  1878. ENUMX
  1879.   BFD_RELOC_SPARC_5
  1880. ENUMDOC
  1881.   Some relocations we're using for SPARC V9 -- subject to change.
  1882.  
  1883. ENUM
  1884.   BFD_RELOC_ALPHA_GPDISP_HI16
  1885. ENUMDOC
  1886.   Alpha ECOFF relocations.  Some of these treat the symbol or "addend"
  1887.      in some special way.
  1888.   For GPDISP_HI16 ("gpdisp") relocations, the symbol is ignored when
  1889.      writing; when reading, it will be the absolute section symbol.  The
  1890.      addend is the displacement in bytes of the "lda" instruction from
  1891.      the "ldah" instruction (which is at the address of this reloc).
  1892. ENUM
  1893.   BFD_RELOC_ALPHA_GPDISP_LO16
  1894. ENUMDOC
  1895.   For GPDISP_LO16 ("ignore") relocations, the symbol is handled as
  1896.      with GPDISP_HI16 relocs.  The addend is ignored when writing the
  1897.      relocations out, and is filled in with the file's GP value on
  1898.      reading, for convenience.
  1899.  
  1900. ENUM
  1901.   BFD_RELOC_ALPHA_LITERAL
  1902. ENUMX
  1903.   BFD_RELOC_ALPHA_LITUSE
  1904. ENUMDOC
  1905.   The Alpha LITERAL/LITUSE relocs are produced by a symbol reference;
  1906.      the assembler turns it into a LDQ instruction to load the address of
  1907.      the symbol, and then fills in a register in the real instruction.
  1908.  
  1909.      The LITERAL reloc, at the LDQ instruction, refers to the .lita
  1910.      section symbol.  The addend is ignored when writing, but is filled
  1911.      in with the file's GP value on reading, for convenience, as with the
  1912.      GPDISP_LO16 reloc.
  1913.  
  1914.      The LITUSE reloc, on the instruction using the loaded address, gives
  1915.      information to the linker that it might be able to use to optimize
  1916.      away some literal section references.  The symbol is ignored (read
  1917.      as the absolute section symbol), and the "addend" indicates the type
  1918.      of instruction using the register:
  1919.               1 - "memory" fmt insn
  1920.               2 - byte-manipulation (byte offset reg)
  1921.               3 - jsr (target of branch)
  1922.  
  1923.      The GNU linker currently doesn't do any of this optimizing.
  1924.  
  1925. ENUM
  1926.   BFD_RELOC_ALPHA_HINT
  1927. ENUMDOC
  1928.   The HINT relocation indicates a value that should be filled into the
  1929.      "hint" field of a jmp/jsr/ret instruction, for possible branch-
  1930.      prediction logic which may be provided on some processors.
  1931.  
  1932. ENUM
  1933.   BFD_RELOC_MIPS_JMP
  1934. ENUMDOC
  1935.   Bits 27..2 of the relocation address shifted right 2 bits;
  1936.      simple reloc otherwise.
  1937.  
  1938. ENUM
  1939.   BFD_RELOC_HI16
  1940. ENUMDOC
  1941.   High 16 bits of 32-bit value; simple reloc.
  1942. ENUM
  1943.   BFD_RELOC_HI16_S
  1944. ENUMDOC
  1945.   High 16 bits of 32-bit value but the low 16 bits will be sign
  1946.      extended and added to form the final result.  If the low 16
  1947.      bits form a negative number, we need to add one to the high value
  1948.      to compensate for the borrow when the low bits are added.
  1949. ENUM
  1950.   BFD_RELOC_LO16
  1951. ENUMDOC
  1952.   Low 16 bits.
  1953. ENUM
  1954.   BFD_RELOC_PCREL_HI16_S
  1955. ENUMDOC
  1956.   Like BFD_RELOC_HI16_S, but PC relative.
  1957. ENUM
  1958.   BFD_RELOC_PCREL_LO16
  1959. ENUMDOC
  1960.   Like BFD_RELOC_LO16, but PC relative.
  1961.  
  1962. ENUMEQ
  1963.   BFD_RELOC_MIPS_GPREL
  1964.   BFD_RELOC_GPREL16
  1965. ENUMDOC
  1966.   Relocation relative to the global pointer.
  1967.  
  1968. ENUM
  1969.   BFD_RELOC_MIPS_LITERAL
  1970. ENUMDOC
  1971.   Relocation against a MIPS literal section.
  1972.  
  1973. ENUM
  1974.   BFD_RELOC_MIPS_GOT16
  1975. ENUMX
  1976.   BFD_RELOC_MIPS_CALL16
  1977. ENUMEQX
  1978.   BFD_RELOC_MIPS_GPREL32
  1979.   BFD_RELOC_GPREL32
  1980. ENUMX
  1981.   BFD_RELOC_MIPS_GOT_HI16
  1982. ENUMX
  1983.   BFD_RELOC_MIPS_GOT_LO16
  1984. ENUMX
  1985.   BFD_RELOC_MIPS_CALL_HI16
  1986. ENUMX
  1987.   BFD_RELOC_MIPS_CALL_LO16
  1988. ENUMDOC
  1989.   MIPS ELF relocations.
  1990.  
  1991. ENUM
  1992.   BFD_RELOC_386_GOT32
  1993. ENUMX
  1994.   BFD_RELOC_386_PLT32
  1995. ENUMX
  1996.   BFD_RELOC_386_COPY
  1997. ENUMX
  1998.   BFD_RELOC_386_GLOB_DAT
  1999. ENUMX
  2000.   BFD_RELOC_386_JUMP_SLOT
  2001. ENUMX
  2002.   BFD_RELOC_386_RELATIVE
  2003. ENUMX
  2004.   BFD_RELOC_386_GOTOFF
  2005. ENUMX
  2006.   BFD_RELOC_386_GOTPC
  2007. ENUMDOC
  2008.   i386/elf relocations
  2009.  
  2010. ENUM
  2011.   BFD_RELOC_NS32K_IMM_8
  2012. ENUMX
  2013.   BFD_RELOC_NS32K_IMM_16
  2014. ENUMX
  2015.   BFD_RELOC_NS32K_IMM_32
  2016. ENUMX
  2017.   BFD_RELOC_NS32K_IMM_8_PCREL
  2018. ENUMX
  2019.   BFD_RELOC_NS32K_IMM_16_PCREL
  2020. ENUMX
  2021.   BFD_RELOC_NS32K_IMM_32_PCREL
  2022. ENUMX
  2023.   BFD_RELOC_NS32K_DISP_8
  2024. ENUMX
  2025.   BFD_RELOC_NS32K_DISP_16
  2026. ENUMX
  2027.   BFD_RELOC_NS32K_DISP_32
  2028. ENUMX
  2029.   BFD_RELOC_NS32K_DISP_8_PCREL
  2030. ENUMX
  2031.   BFD_RELOC_NS32K_DISP_16_PCREL
  2032. ENUMX
  2033.   BFD_RELOC_NS32K_DISP_32_PCREL
  2034. ENUMDOC
  2035.   ns32k relocations
  2036.  
  2037. ENUM
  2038.   BFD_RELOC_PPC_B26
  2039. ENUMX
  2040.   BFD_RELOC_PPC_BA26
  2041. ENUMX
  2042.   BFD_RELOC_PPC_TOC16
  2043. ENUMX
  2044.   BFD_RELOC_PPC_B16
  2045. ENUMX
  2046.   BFD_RELOC_PPC_B16_BRTAKEN
  2047. ENUMX
  2048.   BFD_RELOC_PPC_B16_BRNTAKEN
  2049. ENUMX
  2050.   BFD_RELOC_PPC_BA16
  2051. ENUMX
  2052.   BFD_RELOC_PPC_BA16_BRTAKEN
  2053. ENUMX
  2054.   BFD_RELOC_PPC_BA16_BRNTAKEN
  2055. ENUMX
  2056.   BFD_RELOC_PPC_COPY
  2057. ENUMX
  2058.   BFD_RELOC_PPC_GLOB_DAT
  2059. ENUMX
  2060.   BFD_RELOC_PPC_JMP_SLOT
  2061. ENUMX
  2062.   BFD_RELOC_PPC_RELATIVE
  2063. ENUMX
  2064.   BFD_RELOC_PPC_LOCAL24PC
  2065. ENUMX
  2066.   BFD_RELOC_PPC_EMB_NADDR32
  2067. ENUMX
  2068.   BFD_RELOC_PPC_EMB_NADDR16
  2069. ENUMX
  2070.   BFD_RELOC_PPC_EMB_NADDR16_LO
  2071. ENUMX
  2072.   BFD_RELOC_PPC_EMB_NADDR16_HI
  2073. ENUMX
  2074.   BFD_RELOC_PPC_EMB_NADDR16_HA
  2075. ENUMX
  2076.   BFD_RELOC_PPC_EMB_SDAI16
  2077. ENUMX
  2078.   BFD_RELOC_PPC_EMB_SDA2I16
  2079. ENUMX
  2080.   BFD_RELOC_PPC_EMB_SDA2REL
  2081. ENUMX
  2082.   BFD_RELOC_PPC_EMB_SDA21
  2083. ENUMX
  2084.   BFD_RELOC_PPC_EMB_MRKREF
  2085. ENUMX
  2086.   BFD_RELOC_PPC_EMB_RELSEC16
  2087. ENUMX
  2088.   BFD_RELOC_PPC_EMB_RELST_LO
  2089. ENUMX
  2090.   BFD_RELOC_PPC_EMB_RELST_HI
  2091. ENUMX
  2092.   BFD_RELOC_PPC_EMB_RELST_HA
  2093. ENUMX
  2094.   BFD_RELOC_PPC_EMB_BIT_FLD
  2095. ENUMX
  2096.   BFD_RELOC_PPC_EMB_RELSDA
  2097. ENUMDOC
  2098.   Power(rs6000) and PowerPC relocations.
  2099.  
  2100. ENUM
  2101.   BFD_RELOC_CTOR
  2102. ENUMDOC
  2103.   The type of reloc used to build a contructor table - at the moment
  2104.   probably a 32 bit wide absolute relocation, but the target can choose.
  2105.   It generally does map to one of the other relocation types.
  2106.  
  2107. ENUM
  2108.   BFD_RELOC_ARM_PCREL_BRANCH
  2109. ENUMDOC
  2110.   ARM 26 bit pc-relative branch.  The lowest two bits must be zero and are
  2111.   not stored in the instruction.
  2112. ENUM
  2113.   BFD_RELOC_ARM_IMMEDIATE
  2114. ENUMX
  2115.   BFD_RELOC_ARM_OFFSET_IMM
  2116. ENUMX
  2117.   BFD_RELOC_ARM_SHIFT_IMM
  2118. ENUMX
  2119.   BFD_RELOC_ARM_SWI
  2120. ENUMX
  2121.   BFD_RELOC_ARM_MULTI
  2122. ENUMX
  2123.   BFD_RELOC_ARM_CP_OFF_IMM
  2124. ENUMX
  2125.   BFD_RELOC_ARM_ADR_IMM
  2126. ENUMX
  2127.   BFD_RELOC_ARM_LDR_IMM
  2128. ENUMX
  2129.   BFD_RELOC_ARM_LITERAL
  2130. ENUMX
  2131.   BFD_RELOC_ARM_IN_POOL
  2132. ENUMDOC
  2133.   These relocs are only used within the ARM assembler.  They are not
  2134.   (at present) written to any object files.
  2135.  
  2136. COMMENT
  2137. ENDSENUM
  2138.   BFD_RELOC_UNUSED
  2139. CODE_FRAGMENT
  2140. .
  2141. .typedef enum bfd_reloc_code_real bfd_reloc_code_real_type;
  2142. */
  2143.  
  2144.  
  2145. /*
  2146. FUNCTION
  2147.     bfd_reloc_type_lookup
  2148.  
  2149. SYNOPSIS
  2150.     reloc_howto_type *
  2151.     bfd_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code);
  2152.  
  2153. DESCRIPTION
  2154.     Return a pointer to a howto structure which, when
  2155.     invoked, will perform the relocation @var{code} on data from the
  2156.     architecture noted.
  2157.  
  2158. */
  2159.  
  2160.  
  2161. reloc_howto_type *
  2162. bfd_reloc_type_lookup (abfd, code)
  2163.      bfd *abfd;
  2164.      bfd_reloc_code_real_type code;
  2165. {
  2166.   return BFD_SEND (abfd, reloc_type_lookup, (abfd, code));
  2167. }
  2168.  
  2169. static reloc_howto_type bfd_howto_32 =
  2170. HOWTO (0, 00, 2, 32, false, 0, complain_overflow_bitfield, 0, "VRT32", false, 0xffffffff, 0xffffffff, true);
  2171.  
  2172.  
  2173. /*
  2174. INTERNAL_FUNCTION
  2175.     bfd_default_reloc_type_lookup
  2176.  
  2177. SYNOPSIS
  2178.     reloc_howto_type *bfd_default_reloc_type_lookup
  2179.     (bfd *abfd, bfd_reloc_code_real_type  code);
  2180.  
  2181. DESCRIPTION
  2182.     Provides a default relocation lookup routine for any architecture.
  2183.  
  2184.  
  2185. */
  2186.  
  2187. reloc_howto_type *
  2188. bfd_default_reloc_type_lookup (abfd, code)
  2189.      bfd *abfd;
  2190.      bfd_reloc_code_real_type code;
  2191. {
  2192.   switch (code)
  2193.     {
  2194.     case BFD_RELOC_CTOR:
  2195.       /* The type of reloc used in a ctor, which will be as wide as the
  2196.      address - so either a 64, 32, or 16 bitter.  */
  2197.       switch (bfd_get_arch_info (abfd)->bits_per_address)
  2198.     {
  2199.     case 64:
  2200.       BFD_FAIL ();
  2201.     case 32:
  2202.       return &bfd_howto_32;
  2203.     case 16:
  2204.       BFD_FAIL ();
  2205.     default:
  2206.       BFD_FAIL ();
  2207.     }
  2208.     default:
  2209.       BFD_FAIL ();
  2210.     }
  2211.   return (reloc_howto_type *) NULL;
  2212. }
  2213.  
  2214. /*
  2215. FUNCTION
  2216.     bfd_get_reloc_code_name
  2217.  
  2218. SYNOPSIS
  2219.     const char *bfd_get_reloc_code_name (bfd_reloc_code_real_type code);
  2220.  
  2221. DESCRIPTION
  2222.     Provides a printable name for the supplied relocation code.
  2223.     Useful mainly for printing error messages.
  2224. */
  2225.  
  2226. const char *
  2227. bfd_get_reloc_code_name (code)
  2228.      bfd_reloc_code_real_type code;
  2229. {
  2230.   if (code > BFD_RELOC_UNUSED)
  2231.     return 0;
  2232.   return bfd_reloc_code_real_names[(int)code];
  2233. }
  2234.  
  2235. /*
  2236. INTERNAL_FUNCTION
  2237.     bfd_generic_relax_section
  2238.  
  2239. SYNOPSIS
  2240.     boolean bfd_generic_relax_section
  2241.      (bfd *abfd,
  2242.       asection *section,
  2243.       struct bfd_link_info *,
  2244.       boolean *);
  2245.  
  2246. DESCRIPTION
  2247.     Provides default handling for relaxing for back ends which
  2248.     don't do relaxing -- i.e., does nothing.
  2249. */
  2250.  
  2251. /*ARGSUSED*/
  2252. boolean
  2253. bfd_generic_relax_section (abfd, section, link_info, again)
  2254.      bfd *abfd;
  2255.      asection *section;
  2256.      struct bfd_link_info *link_info;
  2257.      boolean *again;
  2258. {
  2259.   *again = false;
  2260.   return true;
  2261. }
  2262.  
  2263. /*
  2264. INTERNAL_FUNCTION
  2265.     bfd_generic_get_relocated_section_contents
  2266.  
  2267. SYNOPSIS
  2268.     bfd_byte *
  2269.        bfd_generic_get_relocated_section_contents (bfd *abfd,
  2270.          struct bfd_link_info *link_info,
  2271.          struct bfd_link_order *link_order,
  2272.          bfd_byte *data,
  2273.          boolean relocateable,
  2274.          asymbol **symbols);
  2275.  
  2276. DESCRIPTION
  2277.     Provides default handling of relocation effort for back ends
  2278.     which can't be bothered to do it efficiently.
  2279.  
  2280. */
  2281.  
  2282. bfd_byte *
  2283. bfd_generic_get_relocated_section_contents (abfd, link_info, link_order, data,
  2284.                         relocateable, symbols)
  2285.      bfd *abfd;
  2286.      struct bfd_link_info *link_info;
  2287.      struct bfd_link_order *link_order;
  2288.      bfd_byte *data;
  2289.      boolean relocateable;
  2290.      asymbol **symbols;
  2291. {
  2292.   /* Get enough memory to hold the stuff */
  2293.   bfd *input_bfd = link_order->u.indirect.section->owner;
  2294.   asection *input_section = link_order->u.indirect.section;
  2295.  
  2296.   long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
  2297.   arelent **reloc_vector = NULL;
  2298.   long reloc_count;
  2299.  
  2300.   if (reloc_size < 0)
  2301.     goto error_return;
  2302.  
  2303.   reloc_vector = (arelent **) bfd_malloc ((size_t) reloc_size);
  2304.   if (reloc_vector == NULL && reloc_size != 0)
  2305.     goto error_return;
  2306.  
  2307.   /* read in the section */
  2308.   if (!bfd_get_section_contents (input_bfd,
  2309.                  input_section,
  2310.                  (PTR) data,
  2311.                  0,
  2312.                  input_section->_raw_size))
  2313.     goto error_return;
  2314.  
  2315.   /* We're not relaxing the section, so just copy the size info */
  2316.   input_section->_cooked_size = input_section->_raw_size;
  2317.   input_section->reloc_done = true;
  2318.  
  2319.   reloc_count = bfd_canonicalize_reloc (input_bfd,
  2320.                     input_section,
  2321.                     reloc_vector,
  2322.                     symbols);
  2323.   if (reloc_count < 0)
  2324.     goto error_return;
  2325.  
  2326.   if (reloc_count > 0)
  2327.     {
  2328.       arelent **parent;
  2329.       for (parent = reloc_vector; *parent != (arelent *) NULL;
  2330.        parent++)
  2331.     {
  2332.       char *error_message = (char *) NULL;
  2333.       bfd_reloc_status_type r =
  2334.         bfd_perform_relocation (input_bfd,
  2335.                     *parent,
  2336.                     (PTR) data,
  2337.                     input_section,
  2338.                     relocateable ? abfd : (bfd *) NULL,
  2339.                     &error_message);
  2340.  
  2341.       if (relocateable)
  2342.         {
  2343.           asection *os = input_section->output_section;
  2344.  
  2345.           /* A partial link, so keep the relocs */
  2346.           os->orelocation[os->reloc_count] = *parent;
  2347.           os->reloc_count++;
  2348.         }
  2349.  
  2350.       if (r != bfd_reloc_ok)
  2351.         {
  2352.           switch (r)
  2353.         {
  2354.         case bfd_reloc_undefined:
  2355.           if (!((*link_info->callbacks->undefined_symbol)
  2356.             (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
  2357.              input_bfd, input_section, (*parent)->address)))
  2358.             goto error_return;
  2359.           break;
  2360.         case bfd_reloc_dangerous:
  2361.           BFD_ASSERT (error_message != (char *) NULL);
  2362.           if (!((*link_info->callbacks->reloc_dangerous)
  2363.             (link_info, error_message, input_bfd, input_section,
  2364.              (*parent)->address)))
  2365.             goto error_return;
  2366.           break;
  2367.         case bfd_reloc_overflow:
  2368.           if (!((*link_info->callbacks->reloc_overflow)
  2369.             (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
  2370.              (*parent)->howto->name, (*parent)->addend,
  2371.              input_bfd, input_section, (*parent)->address)))
  2372.             goto error_return;
  2373.           break;
  2374.         case bfd_reloc_outofrange:
  2375.         default:
  2376.           abort ();
  2377.           break;
  2378.         }
  2379.  
  2380.         }
  2381.     }
  2382.     }
  2383.   if (reloc_vector != NULL)
  2384.     free (reloc_vector);
  2385.   return data;
  2386.  
  2387. error_return:
  2388.   if (reloc_vector != NULL)
  2389.     free (reloc_vector);
  2390.   return NULL;
  2391. }
  2392.