home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / bfd / reloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-04  |  64.8 KB  |  2,267 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., 675 Mass Ave, Cambridge, MA 02139, 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.     {
  721.       bfd_vma check;
  722.  
  723.       /* Get the value that will be used for the relocation, but
  724.      starting at bit position zero.  */
  725.       if (howto->rightshift > howto->bitpos)
  726.     check = relocation >> (howto->rightshift - howto->bitpos);
  727.       else
  728.     check = relocation << (howto->bitpos - howto->rightshift);
  729.       switch (howto->complain_on_overflow)
  730.     {
  731.     case complain_overflow_signed:
  732.       {
  733.         /* Assumes two's complement.  */
  734.         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
  735.         bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
  736.  
  737.         /* The above right shift is incorrect for a signed value.
  738.            Fix it up by forcing on the upper bits.  */
  739.         if (howto->rightshift > howto->bitpos
  740.         && (bfd_signed_vma) relocation < 0)
  741.           check |= ((bfd_vma) - 1
  742.             & ~((bfd_vma) - 1
  743.                 >> (howto->rightshift - howto->bitpos)));
  744.         if ((bfd_signed_vma) check > reloc_signed_max
  745.         || (bfd_signed_vma) check < reloc_signed_min)
  746.           flag = bfd_reloc_overflow;
  747.       }
  748.       break;
  749.     case complain_overflow_unsigned:
  750.       {
  751.         /* Assumes two's complement.  This expression avoids
  752.            overflow if howto->bitsize is the number of bits in
  753.            bfd_vma.  */
  754.         bfd_vma reloc_unsigned_max =
  755.         (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  756.  
  757.         if ((bfd_vma) check > reloc_unsigned_max)
  758.           flag = bfd_reloc_overflow;
  759.       }
  760.       break;
  761.     case complain_overflow_bitfield:
  762.       {
  763.         /* Assumes two's complement.  This expression avoids
  764.            overflow if howto->bitsize is the number of bits in
  765.            bfd_vma.  */
  766.         bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  767.  
  768.         if (((bfd_vma) check & ~reloc_bits) != 0
  769.         && ((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
  770.           {
  771.         /* The above right shift is incorrect for a signed
  772.            value.  See if turning on the upper bits fixes the
  773.            overflow.  */
  774.         if (howto->rightshift > howto->bitpos
  775.             && (bfd_signed_vma) relocation < 0)
  776.           {
  777.             check |= ((bfd_vma) - 1
  778.                   & ~((bfd_vma) - 1
  779.                   >> (howto->rightshift - howto->bitpos)));
  780.             if (((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
  781.               flag = bfd_reloc_overflow;
  782.           }
  783.         else
  784.           flag = bfd_reloc_overflow;
  785.           }
  786.       }
  787.       break;
  788.     default:
  789.       abort ();
  790.     }
  791.     }
  792.  
  793.   /*
  794.     Either we are relocating all the way, or we don't want to apply
  795.     the relocation to the reloc entry (probably because there isn't
  796.     any room in the output format to describe addends to relocs)
  797.     */
  798.  
  799.   /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
  800.      (OSF version 1.3, compiler version 3.11).  It miscompiles the
  801.      following program:
  802.  
  803.      struct str
  804.      {
  805.        unsigned int i0;
  806.      } s = { 0 };
  807.  
  808.      int
  809.      main ()
  810.      {
  811.        unsigned long x;
  812.  
  813.        x = 0x100000000;
  814.        x <<= (unsigned long) s.i0;
  815.        if (x == 0)
  816.      printf ("failed\n");
  817.        else
  818.      printf ("succeeded (%lx)\n", x);
  819.      }
  820.      */
  821.  
  822.   relocation >>= (bfd_vma) howto->rightshift;
  823.  
  824.   /* Shift everything up to where it's going to be used */
  825.  
  826.   relocation <<= (bfd_vma) howto->bitpos;
  827.  
  828.   /* Wait for the day when all have the mask in them */
  829.  
  830.   /* What we do:
  831.      i instruction to be left alone
  832.      o offset within instruction
  833.      r relocation offset to apply
  834.      S src mask
  835.      D dst mask
  836.      N ~dst mask
  837.      A part 1
  838.      B part 2
  839.      R result
  840.  
  841.      Do this:
  842.      i i i i i o o o o o        from bfd_get<size>
  843.      and           S S S S S    to get the size offset we want
  844.      +   r r r r r r r r r r  to get the final value to place
  845.      and           D D D D D  to chop to right size
  846.      -----------------------
  847.      A A A A A
  848.      And this:
  849.      ...   i i i i i o o o o o  from bfd_get<size>
  850.      and   N N N N N            get instruction
  851.      -----------------------
  852.      ...   B B B B B
  853.  
  854.      And then:
  855.      B B B B B
  856.      or              A A A A A
  857.      -----------------------
  858.      R R R R R R R R R R        put into bfd_put<size>
  859.      */
  860.  
  861. #define DOIT(x) \
  862.   x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) +  relocation) & howto->dst_mask))
  863.  
  864.   switch (howto->size)
  865.     {
  866.     case 0:
  867.       {
  868.     char x = bfd_get_8 (abfd, (char *) data + addr);
  869.     DOIT (x);
  870.     bfd_put_8 (abfd, x, (unsigned char *) data + addr);
  871.       }
  872.       break;
  873.  
  874.     case 1:
  875.       if (relocation)
  876.     {
  877.       short x = bfd_get_16 (abfd, (bfd_byte *) data + addr);
  878.       DOIT (x);
  879.       bfd_put_16 (abfd, x, (unsigned char *) data + addr);
  880.     }
  881.       break;
  882.     case 2:
  883.       if (relocation)
  884.     {
  885.       long x = bfd_get_32 (abfd, (bfd_byte *) data + addr);
  886.       DOIT (x);
  887.       bfd_put_32 (abfd, x, (bfd_byte *) data + addr);
  888.     }
  889.       break;
  890.     case -2:
  891.       {
  892.     long x = bfd_get_32 (abfd, (bfd_byte *) data + addr);
  893.     relocation = -relocation;
  894.     DOIT (x);
  895.     bfd_put_32 (abfd, x, (bfd_byte *) data + addr);
  896.       }
  897.       break;
  898.  
  899.     case 3:
  900.       /* Do nothing */
  901.       break;
  902.  
  903.     case 4:
  904. #ifdef BFD64
  905.       if (relocation)
  906.     {
  907.       bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data + addr);
  908.       DOIT (x);
  909.       bfd_put_64 (abfd, x, (bfd_byte *) data + addr);
  910.     }
  911. #else
  912.       abort ();
  913. #endif
  914.       break;
  915.     default:
  916.       return bfd_reloc_other;
  917.     }
  918.  
  919.   return flag;
  920. }
  921.  
  922. /*
  923. FUNCTION
  924.     bfd_install_relocation
  925.  
  926. SYNOPSIS
  927.     bfd_reloc_status_type
  928.                 bfd_install_relocation
  929.                         (bfd *abfd,
  930.                          arelent *reloc_entry,
  931.                          PTR data, bfd_vma data_start,
  932.                          asection *input_section,
  933.              char **error_message);
  934.  
  935. DESCRIPTION
  936.     This looks remarkably like <<bfd_perform_relocation>>, except it
  937.     does not expect that the section contents have been filled in.
  938.     I.e., it's suitable for use when creating, rather than applying
  939.     a relocation.
  940.  
  941.     For now, this function should be considered reserved for the
  942.     assembler.
  943.  
  944. */
  945.  
  946.  
  947. bfd_reloc_status_type
  948. bfd_install_relocation (abfd, reloc_entry, data_start, data_start_offset,
  949.             input_section, error_message)
  950.      bfd *abfd;
  951.      arelent *reloc_entry;
  952.      PTR data_start;
  953.      bfd_vma data_start_offset;
  954.      asection *input_section;
  955.      char **error_message;
  956. {
  957.   bfd_vma relocation;
  958.   bfd_reloc_status_type flag = bfd_reloc_ok;
  959.   bfd_size_type addr = reloc_entry->address;
  960.   bfd_vma output_base = 0;
  961.   reloc_howto_type *howto = reloc_entry->howto;
  962.   asection *reloc_target_output_section;
  963.   asymbol *symbol;
  964.   bfd_byte *data;
  965.  
  966.   symbol = *(reloc_entry->sym_ptr_ptr);
  967.   if (bfd_is_abs_section (symbol->section))
  968.     {
  969.       reloc_entry->address += input_section->output_offset;
  970.       return bfd_reloc_ok;
  971.     }
  972.  
  973.   /* If there is a function supplied to handle this relocation type,
  974.      call it.  It'll return `bfd_reloc_continue' if further processing
  975.      can be done.  */
  976.   if (howto->special_function)
  977.     {
  978.       bfd_reloc_status_type cont;
  979.       /* XXX - The special_function calls haven't been fixed up to deal
  980.      with creating new relocations and section contents.  */
  981.       cont = howto->special_function (abfd, reloc_entry, symbol,
  982.                       /* XXX - Non-portable! */
  983.                       ((bfd_byte *) data_start
  984.                        - data_start_offset),
  985.                       input_section, abfd, error_message);
  986.       if (cont != bfd_reloc_continue)
  987.     return cont;
  988.     }
  989.  
  990.   /* Is the address of the relocation really within the section?  */
  991.   if (reloc_entry->address > input_section->_cooked_size)
  992.     return bfd_reloc_outofrange;
  993.  
  994.   /* Work out which section the relocation is targetted at and the
  995.      initial relocation command value.  */
  996.  
  997.   /* Get symbol value.  (Common symbols are special.)  */
  998.   if (bfd_is_com_section (symbol->section))
  999.     relocation = 0;
  1000.   else
  1001.     relocation = symbol->value;
  1002.  
  1003.  
  1004.   reloc_target_output_section = symbol->section->output_section;
  1005.  
  1006.   /* Convert input-section-relative symbol value to absolute.  */
  1007.   if (howto->partial_inplace == false)
  1008.     output_base = 0;
  1009.   else
  1010.     output_base = reloc_target_output_section->vma;
  1011.  
  1012.   relocation += output_base + symbol->section->output_offset;
  1013.  
  1014.   /* Add in supplied addend.  */
  1015.   relocation += reloc_entry->addend;
  1016.  
  1017.   /* Here the variable relocation holds the final address of the
  1018.      symbol we are relocating against, plus any addend.  */
  1019.  
  1020.   if (howto->pc_relative == true)
  1021.     {
  1022.       /* This is a PC relative relocation.  We want to set RELOCATION
  1023.      to the distance between the address of the symbol and the
  1024.      location.  RELOCATION is already the address of the symbol.
  1025.  
  1026.      We start by subtracting the address of the section containing
  1027.      the location.
  1028.  
  1029.      If pcrel_offset is set, we must further subtract the position
  1030.      of the location within the section.  Some targets arrange for
  1031.      the addend to be the negative of the position of the location
  1032.      within the section; for example, i386-aout does this.  For
  1033.      i386-aout, pcrel_offset is false.  Some other targets do not
  1034.      include the position of the location; for example, m88kbcs,
  1035.      or ELF.  For those targets, pcrel_offset is true.
  1036.  
  1037.      If we are producing relocateable output, then we must ensure
  1038.      that this reloc will be correctly computed when the final
  1039.      relocation is done.  If pcrel_offset is false we want to wind
  1040.      up with the negative of the location within the section,
  1041.      which means we must adjust the existing addend by the change
  1042.      in the location within the section.  If pcrel_offset is true
  1043.      we do not want to adjust the existing addend at all.
  1044.  
  1045.      FIXME: This seems logical to me, but for the case of
  1046.      producing relocateable output it is not what the code
  1047.      actually does.  I don't want to change it, because it seems
  1048.      far too likely that something will break.  */
  1049.  
  1050.       relocation -=
  1051.     input_section->output_section->vma + input_section->output_offset;
  1052.  
  1053.       if (howto->pcrel_offset == true && howto->partial_inplace == true)
  1054.     relocation -= reloc_entry->address;
  1055.     }
  1056.  
  1057.   if (howto->partial_inplace == false)
  1058.     {
  1059.       /* This is a partial relocation, and we want to apply the relocation
  1060.      to the reloc entry rather than the raw data. Modify the reloc
  1061.      inplace to reflect what we now know.  */
  1062.       reloc_entry->addend = relocation;
  1063.       reloc_entry->address += input_section->output_offset;
  1064.       return flag;
  1065.     }
  1066.   else
  1067.     {
  1068.       /* This is a partial relocation, but inplace, so modify the
  1069.      reloc record a bit.
  1070.      
  1071.      If we've relocated with a symbol with a section, change
  1072.      into a ref to the section belonging to the symbol.  */
  1073.       
  1074.       reloc_entry->address += input_section->output_offset;
  1075.       
  1076.       /* WTF?? */
  1077.       if (abfd->xvec->flavour == bfd_target_coff_flavour
  1078.       && strcmp (abfd->xvec->name, "aixcoff-rs6000") != 0
  1079.       && strcmp (abfd->xvec->name, "coff-Intel-little") != 0
  1080.       && strcmp (abfd->xvec->name, "coff-Intel-big") != 0)
  1081.     {
  1082. #if 1
  1083. /* For m68k-coff, the addend was being subtracted twice during
  1084.    relocation with -r.  Removing the line below this comment
  1085.    fixes that problem; see PR 2953.
  1086.          
  1087. However, Ian wrote the following, regarding removing the line below,
  1088. which explains why it is still enabled:  --djm
  1089.          
  1090. If you put a patch like that into BFD you need to check all the COFF
  1091. linkers.  I am fairly certain that patch will break coff-i386 (e.g.,
  1092. SCO); see coff_i386_reloc in coff-i386.c where I worked around the
  1093. problem in a different way.  There may very well be a reason that the
  1094. code works as it does.
  1095.  
  1096. Hmmm.  The first obvious point is that bfd_install_relocation should
  1097. not have any tests that depend upon the flavour.  It's seem like
  1098. entirely the wrong place for such a thing.  The second obvious point
  1099. is that the current code ignores the reloc addend when producing
  1100. relocateable output for COFF.  That's peculiar.  In fact, I really
  1101. have no idea what the point of the line you want to remove is.
  1102.  
  1103. A typical COFF reloc subtracts the old value of the symbol and adds in
  1104. the new value to the location in the object file (if it's a pc
  1105. relative reloc it adds the difference between the symbol value and the
  1106. location).  When relocating we need to preserve that property.
  1107.  
  1108. BFD handles this by setting the addend to the negative of the old
  1109. value of the symbol.  Unfortunately it handles common symbols in a
  1110. non-standard way (it doesn't subtract the old value) but that's a
  1111. different story (we can't change it without losing backward
  1112. compatibility with old object files) (coff-i386 does subtract the old
  1113. value, to be compatible with existing coff-i386 targets, like SCO).
  1114.  
  1115. So everything works fine when not producing relocateable output.  When
  1116. we are producing relocateable output, logically we should do exactly
  1117. what we do when not producing relocateable output.  Therefore, your
  1118. patch is correct.  In fact, it should probably always just set
  1119. reloc_entry->addend to 0 for all cases, since it is, in fact, going to
  1120. add the value into the object file.  This won't hurt the COFF code,
  1121. which doesn't use the addend; I'm not sure what it will do to other
  1122. formats (the thing to check for would be whether any formats both use
  1123. the addend and set partial_inplace).
  1124.  
  1125. When I wanted to make coff-i386 produce relocateable output, I ran
  1126. into the problem that you are running into: I wanted to remove that
  1127. line.  Rather than risk it, I made the coff-i386 relocs use a special
  1128. function; it's coff_i386_reloc in coff-i386.c.  The function
  1129. specifically adds the addend field into the object file, knowing that
  1130. bfd_install_relocation is not going to.  If you remove that line, then
  1131. coff-i386.c will wind up adding the addend field in twice.  It's
  1132. trivial to fix; it just needs to be done.
  1133.  
  1134. The problem with removing the line is just that it may break some
  1135. working code.  With BFD it's hard to be sure of anything.  The right
  1136. way to deal with this is simply to build and test at least all the
  1137. supported COFF targets.  It should be straightforward if time and disk
  1138. space consuming.  For each target:
  1139.     1) build the linker
  1140.     2) generate some executable, and link it using -r (I would
  1141.        probably use paranoia.o and link against newlib/libc.a, which
  1142.        for all the supported targets would be available in
  1143.        /usr/cygnus/progressive/H-host/target/lib/libc.a).
  1144.     3) make the change to reloc.c
  1145.     4) rebuild the linker
  1146.     5) repeat step 2
  1147.     6) if the resulting object files are the same, you have at least
  1148.        made it no worse
  1149.     7) if they are different you have to figure out which version is
  1150.        right
  1151. */
  1152.       relocation -= reloc_entry->addend;
  1153. #endif
  1154.       reloc_entry->addend = 0;
  1155.     }
  1156.       else
  1157.     {
  1158.       reloc_entry->addend = relocation;
  1159.     }
  1160.     }
  1161.  
  1162.   /* FIXME: This overflow checking is incomplete, because the value
  1163.      might have overflowed before we get here.  For a correct check we
  1164.      need to compute the value in a size larger than bitsize, but we
  1165.      can't reasonably do that for a reloc the same size as a host
  1166.      machine word.
  1167.  
  1168.      FIXME: We should also do overflow checking on the result after
  1169.      adding in the value contained in the object file.  */
  1170.   if (howto->complain_on_overflow != complain_overflow_dont)
  1171.     {
  1172.       bfd_vma check;
  1173.  
  1174.       /* Get the value that will be used for the relocation, but
  1175.      starting at bit position zero.  */
  1176.       if (howto->rightshift > howto->bitpos)
  1177.     check = relocation >> (howto->rightshift - howto->bitpos);
  1178.       else
  1179.     check = relocation << (howto->bitpos - howto->rightshift);
  1180.       switch (howto->complain_on_overflow)
  1181.     {
  1182.     case complain_overflow_signed:
  1183.       {
  1184.         /* Assumes two's complement.  */
  1185.         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
  1186.         bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
  1187.  
  1188.         /* The above right shift is incorrect for a signed value.
  1189.            Fix it up by forcing on the upper bits.  */
  1190.         if (howto->rightshift > howto->bitpos
  1191.         && (bfd_signed_vma) relocation < 0)
  1192.           check |= ((bfd_vma) - 1
  1193.             & ~((bfd_vma) - 1
  1194.                 >> (howto->rightshift - howto->bitpos)));
  1195.         if ((bfd_signed_vma) check > reloc_signed_max
  1196.         || (bfd_signed_vma) check < reloc_signed_min)
  1197.           flag = bfd_reloc_overflow;
  1198.       }
  1199.       break;
  1200.     case complain_overflow_unsigned:
  1201.       {
  1202.         /* Assumes two's complement.  This expression avoids
  1203.            overflow if howto->bitsize is the number of bits in
  1204.            bfd_vma.  */
  1205.         bfd_vma reloc_unsigned_max =
  1206.         (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  1207.  
  1208.         if ((bfd_vma) check > reloc_unsigned_max)
  1209.           flag = bfd_reloc_overflow;
  1210.       }
  1211.       break;
  1212.     case complain_overflow_bitfield:
  1213.       {
  1214.         /* Assumes two's complement.  This expression avoids
  1215.            overflow if howto->bitsize is the number of bits in
  1216.            bfd_vma.  */
  1217.         bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  1218.  
  1219.         if (((bfd_vma) check & ~reloc_bits) != 0
  1220.         && ((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
  1221.           {
  1222.         /* The above right shift is incorrect for a signed
  1223.            value.  See if turning on the upper bits fixes the
  1224.            overflow.  */
  1225.         if (howto->rightshift > howto->bitpos
  1226.             && (bfd_signed_vma) relocation < 0)
  1227.           {
  1228.             check |= ((bfd_vma) - 1
  1229.                   & ~((bfd_vma) - 1
  1230.                   >> (howto->rightshift - howto->bitpos)));
  1231.             if (((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
  1232.               flag = bfd_reloc_overflow;
  1233.           }
  1234.         else
  1235.           flag = bfd_reloc_overflow;
  1236.           }
  1237.       }
  1238.       break;
  1239.     default:
  1240.       abort ();
  1241.     }
  1242.     }
  1243.  
  1244.   /*
  1245.     Either we are relocating all the way, or we don't want to apply
  1246.     the relocation to the reloc entry (probably because there isn't
  1247.     any room in the output format to describe addends to relocs)
  1248.     */
  1249.  
  1250.   /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
  1251.      (OSF version 1.3, compiler version 3.11).  It miscompiles the
  1252.      following program:
  1253.  
  1254.      struct str
  1255.      {
  1256.        unsigned int i0;
  1257.      } s = { 0 };
  1258.  
  1259.      int
  1260.      main ()
  1261.      {
  1262.        unsigned long x;
  1263.  
  1264.        x = 0x100000000;
  1265.        x <<= (unsigned long) s.i0;
  1266.        if (x == 0)
  1267.      printf ("failed\n");
  1268.        else
  1269.      printf ("succeeded (%lx)\n", x);
  1270.      }
  1271.      */
  1272.  
  1273.   relocation >>= (bfd_vma) howto->rightshift;
  1274.  
  1275.   /* Shift everything up to where it's going to be used */
  1276.  
  1277.   relocation <<= (bfd_vma) howto->bitpos;
  1278.  
  1279.   /* Wait for the day when all have the mask in them */
  1280.  
  1281.   /* What we do:
  1282.      i instruction to be left alone
  1283.      o offset within instruction
  1284.      r relocation offset to apply
  1285.      S src mask
  1286.      D dst mask
  1287.      N ~dst mask
  1288.      A part 1
  1289.      B part 2
  1290.      R result
  1291.  
  1292.      Do this:
  1293.      i i i i i o o o o o        from bfd_get<size>
  1294.      and           S S S S S    to get the size offset we want
  1295.      +   r r r r r r r r r r  to get the final value to place
  1296.      and           D D D D D  to chop to right size
  1297.      -----------------------
  1298.      A A A A A
  1299.      And this:
  1300.      ...   i i i i i o o o o o  from bfd_get<size>
  1301.      and   N N N N N            get instruction
  1302.      -----------------------
  1303.      ...   B B B B B
  1304.  
  1305.      And then:
  1306.      B B B B B
  1307.      or              A A A A A
  1308.      -----------------------
  1309.      R R R R R R R R R R        put into bfd_put<size>
  1310.      */
  1311.  
  1312. #define DOIT(x) \
  1313.   x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) +  relocation) & howto->dst_mask))
  1314.  
  1315.   data = (bfd_byte *) data_start + (addr - data_start_offset);
  1316.  
  1317.   switch (howto->size)
  1318.     {
  1319.     case 0:
  1320.       {
  1321.     char x = bfd_get_8 (abfd, (char *) data);
  1322.     DOIT (x);
  1323.     bfd_put_8 (abfd, x, (unsigned char *) data);
  1324.       }
  1325.       break;
  1326.  
  1327.     case 1:
  1328.       if (relocation)
  1329.     {
  1330.       short x = bfd_get_16 (abfd, (bfd_byte *) data);
  1331.       DOIT (x);
  1332.       bfd_put_16 (abfd, x, (unsigned char *) data);
  1333.     }
  1334.       break;
  1335.     case 2:
  1336.       if (relocation)
  1337.     {
  1338.       long x = bfd_get_32 (abfd, (bfd_byte *) data);
  1339.       DOIT (x);
  1340.       bfd_put_32 (abfd, x, (bfd_byte *) data);
  1341.     }
  1342.       break;
  1343.     case -2:
  1344.       {
  1345.     long x = bfd_get_32 (abfd, (bfd_byte *) data);
  1346.     relocation = -relocation;
  1347.     DOIT (x);
  1348.     bfd_put_32 (abfd, x, (bfd_byte *) data);
  1349.       }
  1350.       break;
  1351.  
  1352.     case 3:
  1353.       /* Do nothing */
  1354.       break;
  1355.  
  1356.     case 4:
  1357.       if (relocation)
  1358.     {
  1359.       bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data);
  1360.       DOIT (x);
  1361.       bfd_put_64 (abfd, x, (bfd_byte *) data);
  1362.     }
  1363.       break;
  1364.     default:
  1365.       return bfd_reloc_other;
  1366.     }
  1367.  
  1368.   return flag;
  1369. }
  1370.  
  1371. /* This relocation routine is used by some of the backend linkers.
  1372.    They do not construct asymbol or arelent structures, so there is no
  1373.    reason for them to use bfd_perform_relocation.  Also,
  1374.    bfd_perform_relocation is so hacked up it is easier to write a new
  1375.    function than to try to deal with it.
  1376.  
  1377.    This routine does a final relocation.  It should not be used when
  1378.    generating relocateable output.
  1379.  
  1380.    FIXME: This routine ignores any special_function in the HOWTO,
  1381.    since the existing special_function values have been written for
  1382.    bfd_perform_relocation.
  1383.  
  1384.    HOWTO is the reloc howto information.
  1385.    INPUT_BFD is the BFD which the reloc applies to.
  1386.    INPUT_SECTION is the section which the reloc applies to.
  1387.    CONTENTS is the contents of the section.
  1388.    ADDRESS is the address of the reloc within INPUT_SECTION.
  1389.    VALUE is the value of the symbol the reloc refers to.
  1390.    ADDEND is the addend of the reloc.  */
  1391.  
  1392. bfd_reloc_status_type
  1393. _bfd_final_link_relocate (howto, input_bfd, input_section, contents, address,
  1394.               value, addend)
  1395.      reloc_howto_type *howto;
  1396.      bfd *input_bfd;
  1397.      asection *input_section;
  1398.      bfd_byte *contents;
  1399.      bfd_vma address;
  1400.      bfd_vma value;
  1401.      bfd_vma addend;
  1402. {
  1403.   bfd_vma relocation;
  1404.  
  1405.   /* Sanity check the address.  */
  1406.   if (address > input_section->_cooked_size)
  1407.     return bfd_reloc_outofrange;
  1408.  
  1409.   /* This function assumes that we are dealing with a basic relocation
  1410.      against a symbol.  We want to compute the value of the symbol to
  1411.      relocate to.  This is just VALUE, the value of the symbol, plus
  1412.      ADDEND, any addend associated with the reloc.  */
  1413.   relocation = value + addend;
  1414.  
  1415.   /* If the relocation is PC relative, we want to set RELOCATION to
  1416.      the distance between the symbol (currently in RELOCATION) and the
  1417.      location we are relocating.  Some targets (e.g., i386-aout)
  1418.      arrange for the contents of the section to be the negative of the
  1419.      offset of the location within the section; for such targets
  1420.      pcrel_offset is false.  Other targets (e.g., m88kbcs or ELF)
  1421.      simply leave the contents of the section as zero; for such
  1422.      targets pcrel_offset is true.  If pcrel_offset is false we do not
  1423.      need to subtract out the offset of the location within the
  1424.      section (which is just ADDRESS).  */
  1425.   if (howto->pc_relative)
  1426.     {
  1427.       relocation -= (input_section->output_section->vma
  1428.              + input_section->output_offset);
  1429.       if (howto->pcrel_offset)
  1430.     relocation -= address;
  1431.     }
  1432.  
  1433.   return _bfd_relocate_contents (howto, input_bfd, relocation,
  1434.                  contents + address);
  1435. }
  1436.  
  1437. /* Relocate a given location using a given value and howto.  */
  1438.  
  1439. bfd_reloc_status_type
  1440. _bfd_relocate_contents (howto, input_bfd, relocation, location)
  1441.      reloc_howto_type *howto;
  1442.      bfd *input_bfd;
  1443.      bfd_vma relocation;
  1444.      bfd_byte *location;
  1445. {
  1446.   int size;
  1447.   bfd_vma x;
  1448.   boolean overflow;
  1449.  
  1450.   /* If the size is negative, negate RELOCATION.  This isn't very
  1451.      general.  */
  1452.   if (howto->size < 0)
  1453.     relocation = -relocation;
  1454.  
  1455.   /* Get the value we are going to relocate.  */
  1456.   size = bfd_get_reloc_size (howto);
  1457.   switch (size)
  1458.     {
  1459.     default:
  1460.     case 0:
  1461.       abort ();
  1462.     case 1:
  1463.       x = bfd_get_8 (input_bfd, location);
  1464.       break;
  1465.     case 2:
  1466.       x = bfd_get_16 (input_bfd, location);
  1467.       break;
  1468.     case 4:
  1469.       x = bfd_get_32 (input_bfd, location);
  1470.       break;
  1471.     case 8:
  1472. #ifdef BFD64
  1473.       x = bfd_get_64 (input_bfd, location);
  1474. #else
  1475.       abort ();
  1476. #endif
  1477.       break;
  1478.     }
  1479.  
  1480.   /* Check for overflow.  FIXME: We may drop bits during the addition
  1481.      which we don't check for.  We must either check at every single
  1482.      operation, which would be tedious, or we must do the computations
  1483.      in a type larger than bfd_vma, which would be inefficient.  */
  1484.   overflow = false;
  1485.   if (howto->complain_on_overflow != complain_overflow_dont)
  1486.     {
  1487.       bfd_vma check;
  1488.       bfd_signed_vma signed_check;
  1489.       bfd_vma add;
  1490.       bfd_signed_vma signed_add;
  1491.  
  1492.       if (howto->rightshift == 0)
  1493.     {
  1494.       check = relocation;
  1495.       signed_check = (bfd_signed_vma) relocation;
  1496.     }
  1497.       else
  1498.     {
  1499.       /* Drop unwanted bits from the value we are relocating to.  */
  1500.       check = relocation >> howto->rightshift;
  1501.  
  1502.       /* If this is a signed value, the rightshift just dropped
  1503.          leading 1 bits (assuming twos complement).  */
  1504.       if ((bfd_signed_vma) relocation >= 0)
  1505.         signed_check = check;
  1506.       else
  1507.         signed_check = (check
  1508.                 | ((bfd_vma) - 1
  1509.                    & ~((bfd_vma) - 1 >> howto->rightshift)));
  1510.     }
  1511.  
  1512.       /* Get the value from the object file.  */
  1513.       add = x & howto->src_mask;
  1514.  
  1515.       /* Get the value from the object file with an appropriate sign.
  1516.      The expression involving howto->src_mask isolates the upper
  1517.      bit of src_mask.  If that bit is set in the value we are
  1518.      adding, it is negative, and we subtract out that number times
  1519.      two.  If src_mask includes the highest possible bit, then we
  1520.      can not get the upper bit, but that does not matter since
  1521.      signed_add needs no adjustment to become negative in that
  1522.      case.  */
  1523.       signed_add = add;
  1524.       if ((add & (((~howto->src_mask) >> 1) & howto->src_mask)) != 0)
  1525.     signed_add -= (((~howto->src_mask) >> 1) & howto->src_mask) << 1;
  1526.  
  1527.       /* Add the value from the object file, shifted so that it is a
  1528.      straight number.  */
  1529.       if (howto->bitpos == 0)
  1530.     {
  1531.       check += add;
  1532.       signed_check += signed_add;
  1533.     }
  1534.       else
  1535.     {
  1536.       check += add >> howto->bitpos;
  1537.  
  1538.       /* For the signed case we use ADD, rather than SIGNED_ADD,
  1539.          to avoid warnings from SVR4 cc.  This is OK since we
  1540.          explictly handle the sign bits.  */
  1541.       if (signed_add >= 0)
  1542.         signed_check += add >> howto->bitpos;
  1543.       else
  1544.         signed_check += ((add >> howto->bitpos)
  1545.                  | ((bfd_vma) - 1
  1546.                 & ~((bfd_vma) - 1 >> howto->bitpos)));
  1547.     }
  1548.  
  1549.       switch (howto->complain_on_overflow)
  1550.     {
  1551.     case complain_overflow_signed:
  1552.       {
  1553.         /* Assumes two's complement.  */
  1554.         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
  1555.         bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
  1556.  
  1557.         if (signed_check > reloc_signed_max
  1558.         || signed_check < reloc_signed_min)
  1559.           overflow = true;
  1560.       }
  1561.       break;
  1562.     case complain_overflow_unsigned:
  1563.       {
  1564.         /* Assumes two's complement.  This expression avoids
  1565.            overflow if howto->bitsize is the number of bits in
  1566.            bfd_vma.  */
  1567.         bfd_vma reloc_unsigned_max =
  1568.         (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  1569.  
  1570.         if (check > reloc_unsigned_max)
  1571.           overflow = true;
  1572.       }
  1573.       break;
  1574.     case complain_overflow_bitfield:
  1575.       {
  1576.         /* Assumes two's complement.  This expression avoids
  1577.            overflow if howto->bitsize is the number of bits in
  1578.            bfd_vma.  */
  1579.         bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  1580.  
  1581.         if ((check & ~reloc_bits) != 0
  1582.         && (((bfd_vma) signed_check & ~reloc_bits)
  1583.             != (-1 & ~reloc_bits)))
  1584.           overflow = true;
  1585.       }
  1586.       break;
  1587.     default:
  1588.       abort ();
  1589.     }
  1590.     }
  1591.  
  1592.   /* Put RELOCATION in the right bits.  */
  1593.   relocation >>= (bfd_vma) howto->rightshift;
  1594.   relocation <<= (bfd_vma) howto->bitpos;
  1595.  
  1596.   /* Add RELOCATION to the right bits of X.  */
  1597.   x = ((x & ~howto->dst_mask)
  1598.        | (((x & howto->src_mask) + relocation) & howto->dst_mask));
  1599.  
  1600.   /* Put the relocated value back in the object file.  */
  1601.   switch (size)
  1602.     {
  1603.     default:
  1604.     case 0:
  1605.       abort ();
  1606.     case 1:
  1607.       bfd_put_8 (input_bfd, x, location);
  1608.       break;
  1609.     case 2:
  1610.       bfd_put_16 (input_bfd, x, location);
  1611.       break;
  1612.     case 4:
  1613.       bfd_put_32 (input_bfd, x, location);
  1614.       break;
  1615.     case 8:
  1616. #ifdef BFD64
  1617.       bfd_put_64 (input_bfd, x, location);
  1618. #else
  1619.       abort ();
  1620. #endif
  1621.       break;
  1622.     }
  1623.  
  1624.   return overflow ? bfd_reloc_overflow : bfd_reloc_ok;
  1625. }
  1626.  
  1627. /*
  1628. DOCDD
  1629. INODE
  1630.     howto manager,  , typedef arelent, Relocations
  1631.  
  1632. SECTION
  1633.     The howto manager
  1634.  
  1635.     When an application wants to create a relocation, but doesn't
  1636.     know what the target machine might call it, it can find out by
  1637.     using this bit of code.
  1638.  
  1639. */
  1640.  
  1641. /*
  1642. TYPEDEF
  1643.     bfd_reloc_code_type
  1644.  
  1645. DESCRIPTION
  1646.     The insides of a reloc code.  The idea is that, eventually, there
  1647.     will be one enumerator for every type of relocation we ever do.
  1648.     Pass one of these values to <<bfd_reloc_type_lookup>>, and it'll
  1649.     return a howto pointer.
  1650.  
  1651.     This does mean that the application must determine the correct
  1652.     enumerator value; you can't get a howto pointer from a random set
  1653.     of attributes.
  1654.  
  1655. SENUM
  1656.    bfd_reloc_code_real
  1657.  
  1658. ENUM
  1659.   BFD_RELOC_64
  1660. ENUMX
  1661.   BFD_RELOC_32
  1662. ENUMX
  1663.   BFD_RELOC_26
  1664. ENUMX
  1665.   BFD_RELOC_16
  1666. ENUMX
  1667.   BFD_RELOC_14
  1668. ENUMX
  1669.   BFD_RELOC_8
  1670. ENUMDOC
  1671.   Basic absolute relocations of N bits.
  1672.  
  1673. ENUM
  1674.   BFD_RELOC_64_PCREL
  1675. ENUMX
  1676.   BFD_RELOC_32_PCREL
  1677. ENUMX
  1678.   BFD_RELOC_24_PCREL
  1679. ENUMX
  1680.   BFD_RELOC_16_PCREL
  1681. ENUMX
  1682.   BFD_RELOC_12_PCREL
  1683. ENUMX
  1684.   BFD_RELOC_8_PCREL
  1685. ENUMDOC
  1686.   PC-relative relocations.  Sometimes these are relative to the address
  1687. of the relocation itself; sometimes they are relative to the start of
  1688. the section containing the relocation.  It depends on the specific target.
  1689.  
  1690. The 24-bit relocation is used in some Intel 960 configurations.
  1691.  
  1692. ENUM
  1693.   BFD_RELOC_32_BASEREL
  1694. ENUMX
  1695.   BFD_RELOC_16_BASEREL
  1696. ENUMX
  1697.   BFD_RELOC_8_BASEREL
  1698. ENUMDOC
  1699.   Linkage-table relative.
  1700.  
  1701. ENUM
  1702.   BFD_RELOC_8_FFnn
  1703. ENUMDOC
  1704.   Absolute 8-bit relocation, but used to form an address like 0xFFnn.
  1705.  
  1706. ENUM
  1707.   BFD_RELOC_32_PCREL_S2
  1708. ENUMX
  1709.   BFD_RELOC_16_PCREL_S2
  1710. ENUMX
  1711.   BFD_RELOC_23_PCREL_S2
  1712. ENUMDOC
  1713.   These PC-relative relocations are stored as word displacements --
  1714. i.e., byte displacements shifted right two bits.  The 30-bit word
  1715. displacement (<<32_PCREL_S2>> -- 32 bits, shifted 2) is used on the
  1716. SPARC.  (SPARC tools generally refer to this as <<WDISP30>>.)  The
  1717. signed 16-bit displacement is used on the MIPS, and the 23-bit
  1718. displacement is used on the Alpha.
  1719.  
  1720. ENUM
  1721.   BFD_RELOC_HI22
  1722. ENUMX
  1723.   BFD_RELOC_LO10
  1724. ENUMDOC
  1725.   High 22 bits and low 10 bits of 32-bit value, placed into lower bits of
  1726. the target word.  These are used on the SPARC.
  1727.  
  1728. ENUM
  1729.   BFD_RELOC_GPREL16
  1730. ENUMX
  1731.   BFD_RELOC_GPREL32
  1732. ENUMDOC
  1733.   For systems that allocate a Global Pointer register, these are
  1734. displacements off that register.  These relocation types are
  1735. handled specially, because the value the register will have is
  1736. decided relatively late.
  1737.  
  1738.  
  1739. ENUM
  1740.   BFD_RELOC_I960_CALLJ
  1741. ENUMDOC
  1742.   Reloc types used for i960/b.out.
  1743.  
  1744. ENUM
  1745.   BFD_RELOC_NONE
  1746. ENUMX
  1747.   BFD_RELOC_SPARC_WDISP22
  1748. ENUMX
  1749.   BFD_RELOC_SPARC22
  1750. ENUMX
  1751.   BFD_RELOC_SPARC13
  1752. ENUMX
  1753.   BFD_RELOC_SPARC_GOT10
  1754. ENUMX
  1755.   BFD_RELOC_SPARC_GOT13
  1756. ENUMX
  1757.   BFD_RELOC_SPARC_GOT22
  1758. ENUMX
  1759.   BFD_RELOC_SPARC_PC10
  1760. ENUMX
  1761.   BFD_RELOC_SPARC_PC22
  1762. ENUMX
  1763.   BFD_RELOC_SPARC_WPLT30
  1764. ENUMX
  1765.   BFD_RELOC_SPARC_COPY
  1766. ENUMX
  1767.   BFD_RELOC_SPARC_GLOB_DAT
  1768. ENUMX
  1769.   BFD_RELOC_SPARC_JMP_SLOT
  1770. ENUMX
  1771.   BFD_RELOC_SPARC_RELATIVE
  1772. ENUMX
  1773.   BFD_RELOC_SPARC_UA32
  1774. ENUMDOC
  1775.   SPARC ELF relocations.  There is probably some overlap with other
  1776.   relocation types already defined.
  1777.  
  1778. ENUM
  1779.   BFD_RELOC_SPARC_BASE13
  1780. ENUMX
  1781.   BFD_RELOC_SPARC_BASE22
  1782. ENUMDOC
  1783.   I think these are specific to SPARC a.out (e.g., Sun 4).
  1784.  
  1785. ENUMEQ
  1786.   BFD_RELOC_SPARC_64
  1787.   BFD_RELOC_64
  1788. ENUMX
  1789.   BFD_RELOC_SPARC_10
  1790. ENUMX
  1791.   BFD_RELOC_SPARC_11
  1792. ENUMX
  1793.   BFD_RELOC_SPARC_OLO10
  1794. ENUMX
  1795.   BFD_RELOC_SPARC_HH22
  1796. ENUMX
  1797.   BFD_RELOC_SPARC_HM10
  1798. ENUMX
  1799.   BFD_RELOC_SPARC_LM22
  1800. ENUMX
  1801.   BFD_RELOC_SPARC_PC_HH22
  1802. ENUMX
  1803.   BFD_RELOC_SPARC_PC_HM10
  1804. ENUMX
  1805.   BFD_RELOC_SPARC_PC_LM22
  1806. ENUMX
  1807.   BFD_RELOC_SPARC_WDISP16
  1808. ENUMX
  1809.   BFD_RELOC_SPARC_WDISP19
  1810. ENUMX
  1811.   BFD_RELOC_SPARC_GLOB_JMP
  1812. ENUMX
  1813.   BFD_RELOC_SPARC_LO7
  1814. ENUMDOC
  1815.   Some relocations we're using for SPARC V9 -- subject to change.
  1816.  
  1817. ENUM
  1818.   BFD_RELOC_ALPHA_GPDISP_HI16
  1819. ENUMDOC
  1820.   Alpha ECOFF relocations.  Some of these treat the symbol or "addend"
  1821.      in some special way.
  1822.   For GPDISP_HI16 ("gpdisp") relocations, the symbol is ignored when
  1823.      writing; when reading, it will be the absolute section symbol.  The
  1824.      addend is the displacement in bytes of the "lda" instruction from
  1825.      the "ldah" instruction (which is at the address of this reloc).
  1826. ENUM
  1827.   BFD_RELOC_ALPHA_GPDISP_LO16
  1828. ENUMDOC
  1829.   For GPDISP_LO16 ("ignore") relocations, the symbol is handled as
  1830.      with GPDISP_HI16 relocs.  The addend is ignored when writing the
  1831.      relocations out, and is filled in with the file's GP value on
  1832.      reading, for convenience.
  1833.  
  1834. ENUM
  1835.   BFD_RELOC_ALPHA_LITERAL
  1836. ENUMX
  1837.   BFD_RELOC_ALPHA_LITUSE
  1838. ENUMDOC
  1839.   The Alpha LITERAL/LITUSE relocs are produced by a symbol reference;
  1840.      the assembler turns it into a LDQ instruction to load the address of
  1841.      the symbol, and then fills in a register in the real instruction.
  1842.  
  1843.      The LITERAL reloc, at the LDQ instruction, refers to the .lita
  1844.      section symbol.  The addend is ignored when writing, but is filled
  1845.      in with the file's GP value on reading, for convenience, as with the
  1846.      GPDISP_LO16 reloc.
  1847.  
  1848.      The LITUSE reloc, on the instruction using the loaded address, gives
  1849.      information to the linker that it might be able to use to optimize
  1850.      away some literal section references.  The symbol is ignored (read
  1851.      as the absolute section symbol), and the "addend" indicates the type
  1852.      of instruction using the register:
  1853.               1 - "memory" fmt insn
  1854.               2 - byte-manipulation (byte offset reg)
  1855.               3 - jsr (target of branch)
  1856.  
  1857.      The GNU linker currently doesn't do any of this optimizing.
  1858.  
  1859. ENUM
  1860.   BFD_RELOC_ALPHA_HINT
  1861. ENUMDOC
  1862.   The HINT relocation indicates a value that should be filled into the
  1863.      "hint" field of a jmp/jsr/ret instruction, for possible branch-
  1864.      prediction logic which may be provided on some processors.
  1865.  
  1866. ENUM
  1867.   BFD_RELOC_MIPS_JMP
  1868. ENUMDOC
  1869.   Bits 27..2 of the relocation address shifted right 2 bits;
  1870.      simple reloc otherwise.
  1871.  
  1872. ENUM
  1873.   BFD_RELOC_HI16
  1874. ENUMDOC
  1875.   High 16 bits of 32-bit value; simple reloc.
  1876. ENUM
  1877.   BFD_RELOC_HI16_S
  1878. ENUMDOC
  1879.   High 16 bits of 32-bit value but the low 16 bits will be sign
  1880.      extended and added to form the final result.  If the low 16
  1881.      bits form a negative number, we need to add one to the high value
  1882.      to compensate for the borrow when the low bits are added.
  1883. ENUM
  1884.   BFD_RELOC_LO16
  1885. ENUMDOC
  1886.   Low 16 bits.
  1887. ENUM
  1888.   BFD_RELOC_PCREL_HI16_S
  1889. ENUMDOC
  1890.   Like BFD_RELOC_HI16_S, but PC relative.
  1891. ENUM
  1892.   BFD_RELOC_PCREL_LO16
  1893. ENUMDOC
  1894.   Like BFD_RELOC_LO16, but PC relative.
  1895.  
  1896. ENUMEQ
  1897.   BFD_RELOC_MIPS_GPREL
  1898.   BFD_RELOC_GPREL16
  1899. ENUMDOC
  1900.   Relocation relative to the global pointer.
  1901.  
  1902. ENUM
  1903.   BFD_RELOC_MIPS_LITERAL
  1904. ENUMDOC
  1905.   Relocation against a MIPS literal section.
  1906.  
  1907. ENUM
  1908.   BFD_RELOC_MIPS_GOT16
  1909. ENUMX
  1910.   BFD_RELOC_MIPS_CALL16
  1911. ENUMEQX
  1912.   BFD_RELOC_MIPS_GPREL32
  1913.   BFD_RELOC_GPREL32
  1914. ENUMDOC
  1915.   MIPS ELF relocations.
  1916.  
  1917. ENUM
  1918.   BFD_RELOC_386_GOT32
  1919. ENUMX
  1920.   BFD_RELOC_386_PLT32
  1921. ENUMX
  1922.   BFD_RELOC_386_COPY
  1923. ENUMX
  1924.   BFD_RELOC_386_GLOB_DAT
  1925. ENUMX
  1926.   BFD_RELOC_386_JUMP_SLOT
  1927. ENUMX
  1928.   BFD_RELOC_386_RELATIVE
  1929. ENUMX
  1930.   BFD_RELOC_386_GOTOFF
  1931. ENUMX
  1932.   BFD_RELOC_386_GOTPC
  1933. ENUMDOC
  1934.   i386/elf relocations
  1935.  
  1936. ENUM
  1937.   BFD_RELOC_NS32K_IMM_8
  1938. ENUMX
  1939.   BFD_RELOC_NS32K_IMM_16
  1940. ENUMX
  1941.   BFD_RELOC_NS32K_IMM_32
  1942. ENUMX
  1943.   BFD_RELOC_NS32K_IMM_8_PCREL
  1944. ENUMX
  1945.   BFD_RELOC_NS32K_IMM_16_PCREL
  1946. ENUMX
  1947.   BFD_RELOC_NS32K_IMM_32_PCREL
  1948. ENUMX
  1949.   BFD_RELOC_NS32K_DISP_8
  1950. ENUMX
  1951.   BFD_RELOC_NS32K_DISP_16
  1952. ENUMX
  1953.   BFD_RELOC_NS32K_DISP_32
  1954. ENUMX
  1955.   BFD_RELOC_NS32K_DISP_8_PCREL
  1956. ENUMX
  1957.   BFD_RELOC_NS32K_DISP_16_PCREL
  1958. ENUMX
  1959.   BFD_RELOC_NS32K_DISP_32_PCREL
  1960. ENUMDOC
  1961.   ns32k relocations
  1962.  
  1963. ENUM
  1964.   BFD_RELOC_PPC_B26
  1965. ENUMDOC
  1966.   PowerPC/POWER (RS/6000) relocs.
  1967.   26 bit relative branch.  Low two bits must be zero.  High 24
  1968.      bits installed in bits 6 through 29 of instruction.
  1969. ENUM
  1970.   BFD_RELOC_PPC_BA26
  1971. ENUMDOC
  1972.   26 bit absolute branch, like BFD_RELOC_PPC_B26 but absolute.
  1973. ENUM
  1974.   BFD_RELOC_PPC_TOC16
  1975. ENUMDOC
  1976.   16 bit TOC relative reference.
  1977.  
  1978. ENUM
  1979.   BFD_RELOC_CTOR
  1980. ENUMDOC
  1981.   The type of reloc used to build a contructor table - at the moment
  1982.   probably a 32 bit wide absolute relocation, but the target can choose.
  1983.   It generally does map to one of the other relocation types.
  1984.  
  1985. ENUM
  1986.   BFD_RELOC_ARM_PCREL_BRANCH
  1987. ENUMDOC
  1988.   ARM 26 bit pc-relative branch.  The lowest two bits must be zero and are
  1989.   not stored in the instruction.
  1990. ENUM
  1991.   BFD_RELOC_ARM_IMMEDIATE
  1992. ENUMX
  1993.   BFD_RELOC_ARM_OFFSET_IMM
  1994. ENUMX
  1995.   BFD_RELOC_ARM_SHIFT_IMM
  1996. ENUMX
  1997.   BFD_RELOC_ARM_SWI
  1998. ENUMX
  1999.   BFD_RELOC_ARM_MULTI
  2000. ENUMX
  2001.   BFD_RELOC_ARM_CP_OFF_IMM
  2002. ENUMDOC
  2003.   These relocs are only used within the ARM assembler.  They are not
  2004.   (at present) written to any object files.
  2005.  
  2006. COMMENT
  2007.  
  2008. ENDSENUM
  2009.   BFD_RELOC_UNUSED
  2010.  
  2011. CODE_FRAGMENT
  2012. .
  2013. .typedef enum bfd_reloc_code_real bfd_reloc_code_real_type;
  2014. */
  2015.  
  2016.  
  2017. /*
  2018. FUNCTION
  2019.     bfd_reloc_type_lookup
  2020.  
  2021. SYNOPSIS
  2022.     reloc_howto_type *
  2023.     bfd_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code);
  2024.  
  2025. DESCRIPTION
  2026.     Return a pointer to a howto structure which, when
  2027.     invoked, will perform the relocation @var{code} on data from the
  2028.     architecture noted.
  2029.  
  2030. */
  2031.  
  2032.  
  2033. reloc_howto_type *
  2034. bfd_reloc_type_lookup (abfd, code)
  2035.      bfd *abfd;
  2036.      bfd_reloc_code_real_type code;
  2037. {
  2038.   return BFD_SEND (abfd, reloc_type_lookup, (abfd, code));
  2039. }
  2040.  
  2041. static reloc_howto_type bfd_howto_32 =
  2042. HOWTO (0, 00, 2, 32, false, 0, complain_overflow_bitfield, 0, "VRT32", false, 0xffffffff, 0xffffffff, true);
  2043.  
  2044.  
  2045. /*
  2046. INTERNAL_FUNCTION
  2047.     bfd_default_reloc_type_lookup
  2048.  
  2049. SYNOPSIS
  2050.     reloc_howto_type *bfd_default_reloc_type_lookup
  2051.     (bfd *abfd, bfd_reloc_code_real_type  code);
  2052.  
  2053. DESCRIPTION
  2054.     Provides a default relocation lookup routine for any architecture.
  2055.  
  2056.  
  2057. */
  2058.  
  2059. reloc_howto_type *
  2060. bfd_default_reloc_type_lookup (abfd, code)
  2061.      bfd *abfd;
  2062.      bfd_reloc_code_real_type code;
  2063. {
  2064.   switch (code)
  2065.     {
  2066.     case BFD_RELOC_CTOR:
  2067.       /* The type of reloc used in a ctor, which will be as wide as the
  2068.      address - so either a 64, 32, or 16 bitter.  */
  2069.       switch (bfd_get_arch_info (abfd)->bits_per_address)
  2070.     {
  2071.     case 64:
  2072.       BFD_FAIL ();
  2073.     case 32:
  2074.       return &bfd_howto_32;
  2075.     case 16:
  2076.       BFD_FAIL ();
  2077.     default:
  2078.       BFD_FAIL ();
  2079.     }
  2080.     default:
  2081.       BFD_FAIL ();
  2082.     }
  2083.   return (reloc_howto_type *) NULL;
  2084. }
  2085.  
  2086. /*
  2087. FUNCTION
  2088.     bfd_get_reloc_code_name
  2089.  
  2090. SYNOPSIS
  2091.     const char *bfd_get_reloc_code_name (bfd_reloc_code_real_type code);
  2092.  
  2093. DESCRIPTION
  2094.     Provides a printable name for the supplied relocation code.
  2095.     Useful mainly for printing error messages.
  2096. */
  2097.  
  2098. const char *
  2099. bfd_get_reloc_code_name (code)
  2100.      bfd_reloc_code_real_type code;
  2101. {
  2102.   if (code > BFD_RELOC_UNUSED)
  2103.     return 0;
  2104.   return bfd_reloc_code_real_names[(int)code];
  2105. }
  2106.  
  2107. /*
  2108. INTERNAL_FUNCTION
  2109.     bfd_generic_relax_section
  2110.  
  2111. SYNOPSIS
  2112.     boolean bfd_generic_relax_section
  2113.      (bfd *abfd,
  2114.       asection *section,
  2115.       struct bfd_link_info *,
  2116.       boolean *);
  2117.  
  2118. DESCRIPTION
  2119.     Provides default handling for relaxing for back ends which
  2120.     don't do relaxing -- i.e., does nothing.
  2121. */
  2122.  
  2123. /*ARGSUSED*/
  2124. boolean
  2125. bfd_generic_relax_section (abfd, section, link_info, again)
  2126.      bfd *abfd;
  2127.      asection *section;
  2128.      struct bfd_link_info *link_info;
  2129.      boolean *again;
  2130. {
  2131.   *again = false;
  2132.   return true;
  2133. }
  2134.  
  2135. /*
  2136. INTERNAL_FUNCTION
  2137.     bfd_generic_get_relocated_section_contents
  2138.  
  2139. SYNOPSIS
  2140.     bfd_byte *
  2141.        bfd_generic_get_relocated_section_contents (bfd *abfd,
  2142.          struct bfd_link_info *link_info,
  2143.          struct bfd_link_order *link_order,
  2144.          bfd_byte *data,
  2145.          boolean relocateable,
  2146.          asymbol **symbols);
  2147.  
  2148. DESCRIPTION
  2149.     Provides default handling of relocation effort for back ends
  2150.     which can't be bothered to do it efficiently.
  2151.  
  2152. */
  2153.  
  2154. bfd_byte *
  2155. bfd_generic_get_relocated_section_contents (abfd, link_info, link_order, data,
  2156.                         relocateable, symbols)
  2157.      bfd *abfd;
  2158.      struct bfd_link_info *link_info;
  2159.      struct bfd_link_order *link_order;
  2160.      bfd_byte *data;
  2161.      boolean relocateable;
  2162.      asymbol **symbols;
  2163. {
  2164.   /* Get enough memory to hold the stuff */
  2165.   bfd *input_bfd = link_order->u.indirect.section->owner;
  2166.   asection *input_section = link_order->u.indirect.section;
  2167.  
  2168.   long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
  2169.   arelent **reloc_vector = NULL;
  2170.   long reloc_count;
  2171.  
  2172.   if (reloc_size < 0)
  2173.     goto error_return;
  2174.  
  2175.   reloc_vector = (arelent **) malloc (reloc_size);
  2176.   if (reloc_vector == NULL && reloc_size != 0)
  2177.     {
  2178.       bfd_set_error (bfd_error_no_memory);
  2179.       goto error_return;
  2180.     }
  2181.  
  2182.   /* read in the section */
  2183.   if (!bfd_get_section_contents (input_bfd,
  2184.                  input_section,
  2185.                  (PTR) data,
  2186.                  0,
  2187.                  input_section->_raw_size))
  2188.     goto error_return;
  2189.  
  2190.   /* We're not relaxing the section, so just copy the size info */
  2191.   input_section->_cooked_size = input_section->_raw_size;
  2192.   input_section->reloc_done = true;
  2193.  
  2194.   reloc_count = bfd_canonicalize_reloc (input_bfd,
  2195.                     input_section,
  2196.                     reloc_vector,
  2197.                     symbols);
  2198.   if (reloc_count < 0)
  2199.     goto error_return;
  2200.  
  2201.   if (reloc_count > 0)
  2202.     {
  2203.       arelent **parent;
  2204.       for (parent = reloc_vector; *parent != (arelent *) NULL;
  2205.        parent++)
  2206.     {
  2207.       char *error_message = (char *) NULL;
  2208.       bfd_reloc_status_type r =
  2209.         bfd_perform_relocation (input_bfd,
  2210.                     *parent,
  2211.                     (PTR) data,
  2212.                     input_section,
  2213.                     relocateable ? abfd : (bfd *) NULL,
  2214.                     &error_message);
  2215.  
  2216.       if (relocateable)
  2217.         {
  2218.           asection *os = input_section->output_section;
  2219.  
  2220.           /* A partial link, so keep the relocs */
  2221.           os->orelocation[os->reloc_count] = *parent;
  2222.           os->reloc_count++;
  2223.         }
  2224.  
  2225.       if (r != bfd_reloc_ok)
  2226.         {
  2227.           switch (r)
  2228.         {
  2229.         case bfd_reloc_undefined:
  2230.           if (!((*link_info->callbacks->undefined_symbol)
  2231.             (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
  2232.              input_bfd, input_section, (*parent)->address)))
  2233.             goto error_return;
  2234.           break;
  2235.         case bfd_reloc_dangerous:
  2236.           BFD_ASSERT (error_message != (char *) NULL);
  2237.           if (!((*link_info->callbacks->reloc_dangerous)
  2238.             (link_info, error_message, input_bfd, input_section,
  2239.              (*parent)->address)))
  2240.             goto error_return;
  2241.           break;
  2242.         case bfd_reloc_overflow:
  2243.           if (!((*link_info->callbacks->reloc_overflow)
  2244.             (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
  2245.              (*parent)->howto->name, (*parent)->addend,
  2246.              input_bfd, input_section, (*parent)->address)))
  2247.             goto error_return;
  2248.           break;
  2249.         case bfd_reloc_outofrange:
  2250.         default:
  2251.           abort ();
  2252.           break;
  2253.         }
  2254.  
  2255.         }
  2256.     }
  2257.     }
  2258.   if (reloc_vector != NULL)
  2259.     free (reloc_vector);
  2260.   return data;
  2261.  
  2262. error_return:
  2263.   if (reloc_vector != NULL)
  2264.     free (reloc_vector);
  2265.   return NULL;
  2266. }
  2267.