home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / bfd / reloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-11  |  48.8 KB  |  1,636 lines

  1. /* BFD support for handling relocation entries.
  2.    Copyright (C) 1990, 1991, 1992, 1993 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. #include "bfd.h"
  45. #include "sysdep.h"
  46. #include "bfdlink.h"
  47. #include "libbfd.h"
  48. /*
  49. DOCDD
  50. INODE
  51.     typedef arelent, howto manager, Relocations, Relocations
  52.  
  53. SUBSECTION
  54.     typedef arelent
  55.  
  56.     This is the structure of a relocation entry:
  57.  
  58. CODE_FRAGMENT
  59. .
  60. .typedef enum bfd_reloc_status
  61. .{
  62. .       {* No errors detected *}
  63. .  bfd_reloc_ok,
  64. .
  65. .       {* The relocation was performed, but there was an overflow. *}
  66. .  bfd_reloc_overflow,
  67. .
  68. .       {* The address to relocate was not within the section supplied. *}
  69. .  bfd_reloc_outofrange,
  70. .
  71. .       {* Used by special functions *}
  72. .  bfd_reloc_continue,
  73. .
  74. .       {* Unsupported relocation size requested. *}
  75. .  bfd_reloc_notsupported,
  76. .
  77. .       {* Unused *}
  78. .  bfd_reloc_other,
  79. .
  80. .       {* The symbol to relocate against was undefined. *}
  81. .  bfd_reloc_undefined,
  82. .
  83. .       {* The relocation was performed, but may not be ok - presently
  84. .          generated only when linking i960 coff files with i960 b.out
  85. .          symbols.  If this type is returned, the error_message argument
  86. .          to bfd_perform_relocation will be set.  *}
  87. .  bfd_reloc_dangerous
  88. . }
  89. . bfd_reloc_status_type;
  90. .
  91. .
  92. .typedef struct reloc_cache_entry
  93. .{
  94. .       {* A pointer into the canonical table of pointers  *}
  95. .  struct symbol_cache_entry **sym_ptr_ptr;
  96. .
  97. .       {* offset in section *}
  98. .  bfd_size_type address;
  99. .
  100. .       {* addend for relocation value *}
  101. .  bfd_vma addend;
  102. .
  103. .       {* Pointer to how to perform the required relocation *}
  104. .  const struct reloc_howto_struct *howto;
  105. .
  106. .} arelent;
  107.  
  108. */
  109.  
  110. /*
  111. DESCRIPTION
  112.  
  113.         Here is a description of each of the fields within an <<arelent>>:
  114.  
  115.         o <<sym_ptr_ptr>>
  116.  
  117.         The symbol table pointer points to a pointer to the symbol
  118.         associated with the relocation request.  It is
  119.         the pointer into the table returned by the back end's
  120.         <<get_symtab>> action. @xref{Symbols}. The symbol is referenced
  121.         through a pointer to a pointer so that tools like the linker
  122.         can fix up all the symbols of the same name by modifying only
  123.         one pointer. The relocation routine looks in the symbol and
  124.         uses the base of the section the symbol is attached to and the
  125.         value of the symbol as the initial relocation offset. If the
  126.         symbol pointer is zero, then the section provided is looked up.
  127.  
  128.         o <<address>>
  129.  
  130.         The <<address>> field gives the offset in bytes from the base of
  131.         the section data which owns the relocation record to the first
  132.         byte of relocatable information. The actual data relocated
  133.         will be relative to this point; for example, a relocation
  134.         type which modifies the bottom two bytes of a four byte word
  135.         would not touch the first byte pointed to in a big endian
  136.         world.
  137.     
  138.     o <<addend>>
  139.  
  140.     The <<addend>> is a value provided by the back end to be added (!)
  141.     to the relocation offset. Its interpretation is dependent upon
  142.     the howto. For example, on the 68k the code:
  143.  
  144.  
  145. |        char foo[];
  146. |        main()
  147. |                {
  148. |                return foo[0x12345678];
  149. |                }
  150.  
  151.         Could be compiled into:
  152.  
  153. |        linkw fp,#-4
  154. |        moveb @@#12345678,d0
  155. |        extbl d0
  156. |        unlk fp
  157. |        rts
  158.  
  159.  
  160.         This could create a reloc pointing to <<foo>>, but leave the
  161.         offset in the data, something like:
  162.  
  163.  
  164. |RELOCATION RECORDS FOR [.text]:
  165. |offset   type      value
  166. |00000006 32        _foo
  167. |
  168. |00000000 4e56 fffc          ; linkw fp,#-4
  169. |00000004 1039 1234 5678     ; moveb @@#12345678,d0
  170. |0000000a 49c0               ; extbl d0
  171. |0000000c 4e5e               ; unlk fp
  172. |0000000e 4e75               ; rts
  173.  
  174.  
  175.         Using coff and an 88k, some instructions don't have enough
  176.         space in them to represent the full address range, and
  177.         pointers have to be loaded in two parts. So you'd get something like:
  178.  
  179.  
  180. |        or.u     r13,r0,hi16(_foo+0x12345678)
  181. |        ld.b     r2,r13,lo16(_foo+0x12345678)
  182. |        jmp      r1
  183.  
  184.  
  185.         This should create two relocs, both pointing to <<_foo>>, and with
  186.         0x12340000 in their addend field. The data would consist of:
  187.  
  188.  
  189. |RELOCATION RECORDS FOR [.text]:
  190. |offset   type      value
  191. |00000002 HVRT16    _foo+0x12340000
  192. |00000006 LVRT16    _foo+0x12340000
  193. |
  194. |00000000 5da05678           ; or.u r13,r0,0x5678
  195. |00000004 1c4d5678           ; ld.b r2,r13,0x5678
  196. |00000008 f400c001           ; jmp r1
  197.  
  198.  
  199.         The relocation routine digs out the value from the data, adds
  200.         it to the addend to get the original offset, and then adds the
  201.         value of <<_foo>>. Note that all 32 bits have to be kept around
  202.         somewhere, to cope with carry from bit 15 to bit 16.
  203.  
  204.         One further example is the sparc and the a.out format. The
  205.         sparc has a similar problem to the 88k, in that some
  206.         instructions don't have room for an entire offset, but on the
  207.         sparc the parts are created in odd sized lumps. The designers of
  208.         the a.out format chose to not use the data within the section
  209.         for storing part of the offset; all the offset is kept within
  210.         the reloc. Anything in the data should be ignored.
  211.  
  212. |        save %sp,-112,%sp
  213. |        sethi %hi(_foo+0x12345678),%g2
  214. |        ldsb [%g2+%lo(_foo+0x12345678)],%i0
  215. |        ret
  216. |        restore
  217.  
  218.         Both relocs contain a pointer to <<foo>>, and the offsets
  219.         contain junk.
  220.  
  221.  
  222. |RELOCATION RECORDS FOR [.text]:
  223. |offset   type      value
  224. |00000004 HI22      _foo+0x12345678
  225. |00000008 LO10      _foo+0x12345678
  226. |
  227. |00000000 9de3bf90     ; save %sp,-112,%sp
  228. |00000004 05000000     ; sethi %hi(_foo+0),%g2
  229. |00000008 f048a000     ; ldsb [%g2+%lo(_foo+0)],%i0
  230. |0000000c 81c7e008     ; ret
  231. |00000010 81e80000     ; restore
  232.  
  233.  
  234.         o <<howto>>
  235.  
  236.         The <<howto>> field can be imagined as a
  237.         relocation instruction. It is a pointer to a structure which
  238.         contains information on what to do with all of the other
  239.         information in the reloc record and data section. A back end
  240.         would normally have a relocation instruction set and turn
  241.         relocations into pointers to the correct structure on input -
  242.         but it would be possible to create each howto field on demand.
  243.  
  244. */
  245.  
  246. /*
  247. SUBSUBSECTION
  248.     <<enum complain_overflow>>
  249.  
  250.     Indicates what sort of overflow checking should be done when
  251.     performing a relocation.
  252.  
  253. CODE_FRAGMENT
  254. .
  255. .enum complain_overflow
  256. .{
  257. .    {* Do not complain on overflow. *}
  258. .  complain_overflow_dont,
  259. .
  260. .    {* Complain if the bitfield overflows, whether it is considered
  261. .       as signed or unsigned. *}
  262. .  complain_overflow_bitfield,
  263. .
  264. .    {* Complain if the value overflows when considered as signed
  265. .       number. *}
  266. .  complain_overflow_signed,
  267. .
  268. .    {* Complain if the value overflows when considered as an
  269. .       unsigned number. *}
  270. .  complain_overflow_unsigned
  271. .};
  272.  
  273. */
  274.  
  275. /*
  276. SUBSUBSECTION
  277.         <<reloc_howto_type>>
  278.  
  279.         The <<reloc_howto_type>> is a structure which contains all the
  280.         information that libbfd needs to know to tie up a back end's data.
  281.  
  282. CODE_FRAGMENT
  283. .struct symbol_cache_entry;        {* Forward declaration *}
  284. .
  285. .typedef unsigned char bfd_byte;
  286. .typedef struct reloc_howto_struct reloc_howto_type;
  287. .
  288. .struct reloc_howto_struct
  289. .{
  290. .       {*  The type field has mainly a documetary use - the back end can
  291. .           do what it wants with it, though normally the back end's
  292. .           external idea of what a reloc number is stored
  293. .           in this field. For example, a PC relative word relocation
  294. .           in a coff environment has the type 023 - because that's
  295. .           what the outside world calls a R_PCRWORD reloc. *}
  296. .  unsigned int type;
  297. .
  298. .       {*  The value the final relocation is shifted right by. This drops
  299. .           unwanted data from the relocation.  *}
  300. .  unsigned int rightshift;
  301. .
  302. .    {*  The size of the item to be relocated.  This is *not* a
  303. .        power-of-two measure.  To get the number of bytes operated
  304. .        on by a type of relocation, use bfd_get_reloc_size.  *}
  305. .  int size;
  306. .
  307. .       {*  The number of bits in the item to be relocated.  This is used
  308. .        when doing overflow checking.  *}
  309. .  unsigned int bitsize;
  310. .
  311. .       {*  Notes that the relocation is relative to the location in the
  312. .           data section of the addend. The relocation function will
  313. .           subtract from the relocation value the address of the location
  314. .           being relocated. *}
  315. .  boolean pc_relative;
  316. .
  317. .    {*  The bit position of the reloc value in the destination.
  318. .        The relocated value is left shifted by this amount. *}
  319. .  unsigned int bitpos;
  320. .
  321. .    {* What type of overflow error should be checked for when
  322. .       relocating. *}
  323. .  enum complain_overflow complain_on_overflow;
  324. .
  325. .       {* If this field is non null, then the supplied function is
  326. .          called rather than the normal function. This allows really
  327. .          strange relocation methods to be accomodated (e.g., i960 callj
  328. .          instructions). *}
  329. .  bfd_reloc_status_type (*special_function)
  330. .                    PARAMS ((bfd *abfd,
  331. .                         arelent *reloc_entry,
  332. .                                            struct symbol_cache_entry *symbol,
  333. .                                            PTR data,
  334. .                                            asection *input_section,
  335. .                                            bfd *output_bfd,
  336. .                                            char **error_message));
  337. .
  338. .       {* The textual name of the relocation type. *}
  339. .  char *name;
  340. .
  341. .       {* When performing a partial link, some formats must modify the
  342. .          relocations rather than the data - this flag signals this.*}
  343. .  boolean partial_inplace;
  344. .
  345. .       {* The src_mask selects which parts of the read in data
  346. .          are to be used in the relocation sum.  E.g., if this was an 8 bit
  347. .          bit of data which we read and relocated, this would be
  348. .          0x000000ff. When we have relocs which have an addend, such as
  349. .          sun4 extended relocs, the value in the offset part of a
  350. .          relocating field is garbage so we never use it. In this case
  351. .          the mask would be 0x00000000. *}
  352. .  bfd_vma src_mask;
  353. .
  354. .       {* The dst_mask selects which parts of the instruction are replaced
  355. .          into the instruction. In most cases src_mask == dst_mask,
  356. .          except in the above special case, where dst_mask would be
  357. .          0x000000ff, and src_mask would be 0x00000000.   *}
  358. .  bfd_vma dst_mask;
  359. .
  360. .       {* When some formats create PC relative instructions, they leave
  361. .          the value of the pc of the place being relocated in the offset
  362. .          slot of the instruction, so that a PC relative relocation can
  363. .          be made just by adding in an ordinary offset (e.g., sun3 a.out).
  364. .          Some formats leave the displacement part of an instruction
  365. .          empty (e.g., m88k bcs); this flag signals the fact.*}
  366. .  boolean pcrel_offset;
  367. .
  368. .};
  369.  
  370. */
  371.  
  372. /*
  373. FUNCTION
  374.     The HOWTO Macro
  375.  
  376. DESCRIPTION
  377.     The HOWTO define is horrible and will go away.
  378.  
  379.  
  380. .#define HOWTO(C, R,S,B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
  381. .  {(unsigned)C,R,S,B, P, BI, O,SF,NAME,INPLACE,MASKSRC,MASKDST,PC}
  382.  
  383. DESCRIPTION
  384.     And will be replaced with the totally magic way. But for the
  385.     moment, we are compatible, so do it this way.
  386.  
  387.  
  388. .#define NEWHOWTO( FUNCTION, NAME,SIZE,REL,IN) HOWTO(0,0,SIZE,0,REL,0,complain_overflow_dont,FUNCTION, NAME,false,0,0,IN)
  389. .
  390. DESCRIPTION
  391.     Helper routine to turn a symbol into a relocation value.
  392.  
  393. .#define HOWTO_PREPARE(relocation, symbol)      \
  394. .  {                                            \
  395. .  if (symbol != (asymbol *)NULL) {             \
  396. .    if (bfd_is_com_section (symbol->section)) { \
  397. .      relocation = 0;                          \
  398. .    }                                          \
  399. .    else {                                     \
  400. .      relocation = symbol->value;              \
  401. .    }                                          \
  402. .  }                                            \
  403. .}
  404.  
  405. */
  406.  
  407. /*
  408. FUNCTION
  409.     bfd_get_reloc_size
  410.  
  411. SYNOPSIS
  412.     int bfd_get_reloc_size (const reloc_howto_type *);
  413.  
  414. DESCRIPTION
  415.     For a reloc_howto_type that operates on a fixed number of bytes,
  416.     this returns the number of bytes operated on.
  417.  */
  418.  
  419. int
  420. bfd_get_reloc_size (howto)
  421.      const reloc_howto_type *howto;
  422. {
  423.   switch (howto->size)
  424.     {
  425.     case 0: return 1;
  426.     case 1: return 2;
  427.     case 2: return 4;
  428.     case 3: return 0;
  429.     case 4: return 8;
  430.     case -2: return 4;
  431.     default: abort ();
  432.     }
  433. }
  434.  
  435. /*
  436. TYPEDEF
  437.     arelent_chain
  438.  
  439. DESCRIPTION
  440.  
  441.     How relocs are tied together in an <<asection>>:
  442.  
  443. .typedef struct relent_chain {
  444. .  arelent relent;
  445. .  struct   relent_chain *next;
  446. .} arelent_chain;
  447.  
  448. */
  449.  
  450.  
  451.  
  452. /*
  453. FUNCTION
  454.     bfd_perform_relocation
  455.  
  456. SYNOPSIS
  457.     bfd_reloc_status_type
  458.                 bfd_perform_relocation
  459.                         (bfd *abfd,
  460.                          arelent *reloc_entry,
  461.                          PTR data,
  462.                          asection *input_section,
  463.                          bfd *output_bfd,
  464.              char **error_message);
  465.  
  466. DESCRIPTION
  467.     If @var{output_bfd} is supplied to this function, the
  468.     generated image will be relocatable; the relocations are
  469.     copied to the output file after they have been changed to
  470.     reflect the new state of the world. There are two ways of
  471.     reflecting the results of partial linkage in an output file:
  472.     by modifying the output data in place, and by modifying the
  473.     relocation record.  Some native formats (e.g., basic a.out and
  474.     basic coff) have no way of specifying an addend in the
  475.     relocation type, so the addend has to go in the output data.
  476.     This is no big deal since in these formats the output data
  477.     slot will always be big enough for the addend. Complex reloc
  478.     types with addends were invented to solve just this problem.
  479.     The @var{error_message} argument is set to an error message if
  480.     this return @code{bfd_reloc_dangerous}.
  481.  
  482. */
  483.  
  484.  
  485. bfd_reloc_status_type
  486. bfd_perform_relocation (abfd, reloc_entry, data, input_section, output_bfd,
  487.             error_message)
  488.      bfd *abfd;
  489.      arelent *reloc_entry;
  490.      PTR data;
  491.      asection *input_section;
  492.      bfd *output_bfd;
  493.      char **error_message;
  494. {
  495.   bfd_vma relocation;
  496.   bfd_reloc_status_type flag = bfd_reloc_ok;
  497.   bfd_size_type addr = reloc_entry->address;
  498.   bfd_vma output_base = 0;
  499.   const reloc_howto_type *howto = reloc_entry->howto;
  500.   asection *reloc_target_output_section;
  501.   asymbol *symbol;
  502.  
  503.   symbol = *(reloc_entry->sym_ptr_ptr);
  504.   if (bfd_is_abs_section (symbol->section)
  505.       && output_bfd != (bfd *) NULL)
  506.     {
  507.       reloc_entry->address += input_section->output_offset;
  508.       return bfd_reloc_ok;
  509.     }
  510.  
  511.   /* If we are not producing relocateable output, return an error if
  512.      the symbol is not defined.  An undefined weak symbol is
  513.      considered to have a value of zero (SVR4 ABI, p. 4-27).  */
  514.   if (bfd_is_und_section (symbol->section)
  515.       && (symbol->flags & BSF_WEAK) == 0
  516.       && output_bfd == (bfd *) NULL)
  517.     flag = bfd_reloc_undefined;
  518.  
  519.   /* If there is a function supplied to handle this relocation type,
  520.      call it.  It'll return `bfd_reloc_continue' if further processing
  521.      can be done.  */
  522.   if (howto->special_function)
  523.     {
  524.       bfd_reloc_status_type cont;
  525.       cont = howto->special_function (abfd, reloc_entry, symbol, data,
  526.                       input_section, output_bfd,
  527.                       error_message);
  528.       if (cont != bfd_reloc_continue)
  529.     return cont;
  530.     }
  531.  
  532.   /* Is the address of the relocation really within the section?  */
  533.   if (reloc_entry->address > input_section->_cooked_size)
  534.     return bfd_reloc_outofrange;
  535.  
  536.   /* Work out which section the relocation is targetted at and the
  537.      initial relocation command value.  */
  538.  
  539.   /* Get symbol value.  (Common symbols are special.)  */
  540.   if (bfd_is_com_section (symbol->section))
  541.     relocation = 0;
  542.   else
  543.     relocation = symbol->value;
  544.  
  545.  
  546.   reloc_target_output_section = symbol->section->output_section;
  547.  
  548.   /* Convert input-section-relative symbol value to absolute.  */
  549.   if (output_bfd && howto->partial_inplace == false)
  550.     output_base = 0;
  551.   else
  552.     output_base = reloc_target_output_section->vma;
  553.  
  554.   relocation += output_base + symbol->section->output_offset;
  555.  
  556.   /* Add in supplied addend.  */
  557.   relocation += reloc_entry->addend;
  558.  
  559.   /* Here the variable relocation holds the final address of the
  560.      symbol we are relocating against, plus any addend.  */
  561.  
  562.   if (howto->pc_relative == true)
  563.     {
  564.       /* This is a PC relative relocation.  We want to set RELOCATION
  565.      to the distance between the address of the symbol and the
  566.      location.  RELOCATION is already the address of the symbol.
  567.  
  568.      We start by subtracting the address of the section containing
  569.      the location.
  570.  
  571.      If pcrel_offset is set, we must further subtract the position
  572.      of the location within the section.  Some targets arrange for
  573.      the addend to be the negative of the position of the location
  574.      within the section; for example, i386-aout does this.  For
  575.      i386-aout, pcrel_offset is false.  Some other targets do not
  576.      include the position of the location; for example, m88kbcs,
  577.      or ELF.  For those targets, pcrel_offset is true.
  578.  
  579.      If we are producing relocateable output, then we must ensure
  580.      that this reloc will be correctly computed when the final
  581.      relocation is done.  If pcrel_offset is false we want to wind
  582.      up with the negative of the location within the section,
  583.      which means we must adjust the existing addend by the change
  584.      in the location within the section.  If pcrel_offset is true
  585.      we do not want to adjust the existing addend at all.
  586.  
  587.      FIXME: This seems logical to me, but for the case of
  588.      producing relocateable output it is not what the code
  589.      actually does.  I don't want to change it, because it seems
  590.      far too likely that something will break.  */
  591.  
  592.       relocation -=
  593.     input_section->output_section->vma + input_section->output_offset;
  594.  
  595.       if (howto->pcrel_offset == true)
  596.     relocation -= reloc_entry->address;
  597.     }
  598.  
  599.   if (output_bfd != (bfd *) NULL)
  600.     {
  601.       if (howto->partial_inplace == false)
  602.     {
  603.       /* This is a partial relocation, and we want to apply the relocation
  604.          to the reloc entry rather than the raw data. Modify the reloc
  605.          inplace to reflect what we now know.  */
  606.       reloc_entry->addend = relocation;
  607.       reloc_entry->address += input_section->output_offset;
  608.       return flag;
  609.     }
  610.       else
  611.     {
  612.       /* This is a partial relocation, but inplace, so modify the
  613.          reloc record a bit.
  614.  
  615.          If we've relocated with a symbol with a section, change
  616.          into a ref to the section belonging to the symbol.  */
  617.  
  618.       reloc_entry->address += input_section->output_offset;
  619.  
  620.       /* WTF?? */
  621.       if (abfd->xvec->flavour == bfd_target_coff_flavour
  622.           && strcmp (abfd->xvec->name, "aixcoff-rs6000") != 0
  623.           && strcmp (abfd->xvec->name, "coff-Intel-little") != 0
  624.           && strcmp (abfd->xvec->name, "coff-Intel-big") != 0)
  625.         {
  626. #if 1
  627.           /* For m68k-coff, the addend was being subtracted twice during
  628.          relocation with -r.  Removing the line below this comment
  629.          fixes that problem; see PR 2953.
  630.  
  631. However, Ian wrote the following, regarding removing the line below,
  632. which explains why it is still enabled:  --djm
  633.  
  634. If you put a patch like that into BFD you need to check all the COFF
  635. linkers.  I am fairly certain that patch will break coff-i386 (e.g.,
  636. SCO); see coff_i386_reloc in coff-i386.c where I worked around the
  637. problem in a different way.  There may very well be a reason that the
  638. code works as it does.
  639.  
  640. Hmmm.  The first obvious point is that bfd_perform_relocation should
  641. not have any tests that depend upon the flavour.  It's seem like
  642. entirely the wrong place for such a thing.  The second obvious point
  643. is that the current code ignores the reloc addend when producing
  644. relocateable output for COFF.  That's peculiar.  In fact, I really
  645. have no idea what the point of the line you want to remove is.
  646.  
  647. A typical COFF reloc subtracts the old value of the symbol and adds in
  648. the new value to the location in the object file (if it's a pc
  649. relative reloc it adds the difference between the symbol value and the
  650. location).  When relocating we need to preserve that property.
  651.  
  652. BFD handles this by setting the addend to the negative of the old
  653. value of the symbol.  Unfortunately it handles common symbols in a
  654. non-standard way (it doesn't subtract the old value) but that's a
  655. different story (we can't change it without losing backward
  656. compatibility with old object files) (coff-i386 does subtract the old
  657. value, to be compatible with existing coff-i386 targets, like SCO).
  658.  
  659. So everything works fine when not producing relocateable output.  When
  660. we are producing relocateable output, logically we should do exactly
  661. what we do when not producing relocateable output.  Therefore, your
  662. patch is correct.  In fact, it should probably always just set
  663. reloc_entry->addend to 0 for all cases, since it is, in fact, going to
  664. add the value into the object file.  This won't hurt the COFF code,
  665. which doesn't use the addend; I'm not sure what it will do to other
  666. formats (the thing to check for would be whether any formats both use
  667. the addend and set partial_inplace).
  668.  
  669. When I wanted to make coff-i386 produce relocateable output, I ran
  670. into the problem that you are running into: I wanted to remove that
  671. line.  Rather than risk it, I made the coff-i386 relocs use a special
  672. function; it's coff_i386_reloc in coff-i386.c.  The function
  673. specifically adds the addend field into the object file, knowing that
  674. bfd_perform_relocation is not going to.  If you remove that line, then
  675. coff-i386.c will wind up adding the addend field in twice.  It's
  676. trivial to fix; it just needs to be done.
  677.  
  678. The problem with removing the line is just that it may break some
  679. working code.  With BFD it's hard to be sure of anything.  The right
  680. way to deal with this is simply to build and test at least all the
  681. supported COFF targets.  It should be straightforward if time and disk
  682. space consuming.  For each target:
  683.     1) build the linker
  684.     2) generate some executable, and link it using -r (I would
  685.        probably use paranoia.o and link against newlib/libc.a, which
  686.        for all the supported targets would be available in
  687.        /usr/cygnus/progressive/H-host/target/lib/libc.a).
  688.     3) make the change to reloc.c
  689.     4) rebuild the linker
  690.     5) repeat step 2
  691.     6) if the resulting object files are the same, you have at least
  692.        made it no worse
  693.     7) if they are different you have to figure out which version is
  694.        right
  695. */
  696.           relocation -= reloc_entry->addend;
  697. #endif
  698.           reloc_entry->addend = 0;
  699.         }
  700.       else
  701.         {
  702.           reloc_entry->addend = relocation;
  703.         }
  704.     }
  705.     }
  706.   else
  707.     {
  708.       reloc_entry->addend = 0;
  709.     }
  710.  
  711.   /* FIXME: This overflow checking is incomplete, because the value
  712.      might have overflowed before we get here.  For a correct check we
  713.      need to compute the value in a size larger than bitsize, but we
  714.      can't reasonably do that for a reloc the same size as a host
  715.      machine word.
  716.      FIXME: We should also do overflow checking on the result after
  717.      adding in the value contained in the object file.  */
  718.   if (howto->complain_on_overflow != complain_overflow_dont)
  719.     {
  720.       bfd_vma check;
  721.  
  722.       /* Get the value that will be used for the relocation, but
  723.      starting at bit position zero.  */
  724.       if (howto->rightshift > howto->bitpos)
  725.     check = relocation >> (howto->rightshift - howto->bitpos);
  726.       else
  727.     check = relocation << (howto->bitpos - howto->rightshift);
  728.       switch (howto->complain_on_overflow)
  729.     {
  730.     case complain_overflow_signed:
  731.       {
  732.         /* Assumes two's complement.  */
  733.         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
  734.         bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
  735.  
  736.         /* The above right shift is incorrect for a signed value.
  737.            Fix it up by forcing on the upper bits.  */
  738.         if (howto->rightshift > howto->bitpos
  739.         && (bfd_signed_vma) relocation < 0)
  740.           check |= ((bfd_vma) - 1
  741.             & ~((bfd_vma) - 1
  742.                 >> (howto->rightshift - howto->bitpos)));
  743.         if ((bfd_signed_vma) check > reloc_signed_max
  744.         || (bfd_signed_vma) check < reloc_signed_min)
  745.           flag = bfd_reloc_overflow;
  746.       }
  747.       break;
  748.     case complain_overflow_unsigned:
  749.       {
  750.         /* Assumes two's complement.  This expression avoids
  751.            overflow if howto->bitsize is the number of bits in
  752.            bfd_vma.  */
  753.         bfd_vma reloc_unsigned_max =
  754.         (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  755.  
  756.         if ((bfd_vma) check > reloc_unsigned_max)
  757.           flag = bfd_reloc_overflow;
  758.       }
  759.       break;
  760.     case complain_overflow_bitfield:
  761.       {
  762.         /* Assumes two's complement.  This expression avoids
  763.            overflow if howto->bitsize is the number of bits in
  764.            bfd_vma.  */
  765.         bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  766.  
  767.         if (((bfd_vma) check & ~reloc_bits) != 0
  768.         && ((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
  769.           {
  770.         /* The above right shift is incorrect for a signed
  771.            value.  See if turning on the upper bits fixes the
  772.            overflow.  */
  773.         if (howto->rightshift > howto->bitpos
  774.             && (bfd_signed_vma) relocation < 0)
  775.           {
  776.             check |= ((bfd_vma) - 1
  777.                   & ~((bfd_vma) - 1
  778.                   >> (howto->rightshift - howto->bitpos)));
  779.             if (((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
  780.               flag = bfd_reloc_overflow;
  781.           }
  782.         else
  783.           flag = bfd_reloc_overflow;
  784.           }
  785.       }
  786.       break;
  787.     default:
  788.       abort ();
  789.     }
  790.     }
  791.  
  792.   /*
  793.     Either we are relocating all the way, or we don't want to apply
  794.     the relocation to the reloc entry (probably because there isn't
  795.     any room in the output format to describe addends to relocs)
  796.     */
  797.  
  798.   /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
  799.      (OSF version 1.3, compiler version 3.11).  It miscompiles the
  800.      following program:
  801.  
  802.      struct str
  803.      {
  804.        unsigned int i0;
  805.      } s = { 0 };
  806.  
  807.      int
  808.      main ()
  809.      {
  810.        unsigned long x;
  811.  
  812.        x = 0x100000000;
  813.        x <<= (unsigned long) s.i0;
  814.        if (x == 0)
  815.      printf ("failed\n");
  816.        else
  817.      printf ("succeeded (%lx)\n", x);
  818.      }
  819.      */
  820.  
  821.   relocation >>= (bfd_vma) howto->rightshift;
  822.  
  823.   /* Shift everything up to where it's going to be used */
  824.  
  825.   relocation <<= (bfd_vma) howto->bitpos;
  826.  
  827.   /* Wait for the day when all have the mask in them */
  828.  
  829.   /* What we do:
  830.      i instruction to be left alone
  831.      o offset within instruction
  832.      r relocation offset to apply
  833.      S src mask
  834.      D dst mask
  835.      N ~dst mask
  836.      A part 1
  837.      B part 2
  838.      R result
  839.  
  840.      Do this:
  841.      i i i i i o o o o o        from bfd_get<size>
  842.      and           S S S S S    to get the size offset we want
  843.      +   r r r r r r r r r r  to get the final value to place
  844.      and           D D D D D  to chop to right size
  845.      -----------------------
  846.      A A A A A
  847.      And this:
  848.      ...   i i i i i o o o o o  from bfd_get<size>
  849.      and   N N N N N            get instruction
  850.      -----------------------
  851.      ...   B B B B B
  852.  
  853.      And then:
  854.      B B B B B
  855.      or              A A A A A
  856.      -----------------------
  857.      R R R R R R R R R R        put into bfd_put<size>
  858.      */
  859.  
  860. #define DOIT(x) \
  861.   x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) +  relocation) & howto->dst_mask))
  862.  
  863.   switch (howto->size)
  864.     {
  865.     case 0:
  866.       {
  867.     char x = bfd_get_8 (abfd, (char *) data + addr);
  868.     DOIT (x);
  869.     bfd_put_8 (abfd, x, (unsigned char *) data + addr);
  870.       }
  871.       break;
  872.  
  873.     case 1:
  874.       if (relocation)
  875.     {
  876.       short x = bfd_get_16 (abfd, (bfd_byte *) data + addr);
  877.       DOIT (x);
  878.       bfd_put_16 (abfd, x, (unsigned char *) data + addr);
  879.     }
  880.       break;
  881.     case 2:
  882.       if (relocation)
  883.     {
  884.       long x = bfd_get_32 (abfd, (bfd_byte *) data + addr);
  885.       DOIT (x);
  886.       bfd_put_32 (abfd, x, (bfd_byte *) data + addr);
  887.     }
  888.       break;
  889.     case -2:
  890.       {
  891.     long x = bfd_get_32 (abfd, (bfd_byte *) data + addr);
  892.     relocation = -relocation;
  893.     DOIT (x);
  894.     bfd_put_32 (abfd, x, (bfd_byte *) data + addr);
  895.       }
  896.       break;
  897.  
  898.     case 3:
  899.       /* Do nothing */
  900.       break;
  901.  
  902.     case 4:
  903. #ifdef BFD64
  904.       if (relocation)
  905.     {
  906.       bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data + addr);
  907.       DOIT (x);
  908.       bfd_put_64 (abfd, x, (bfd_byte *) data + addr);
  909.     }
  910. #else
  911.       abort ();
  912. #endif
  913.       break;
  914.     default:
  915.       return bfd_reloc_other;
  916.     }
  917.  
  918.   return flag;
  919. }
  920.  
  921. /* This relocation routine is used by some of the backend linkers.
  922.    They do not construct asymbol or arelent structures, so there is no
  923.    reason for them to use bfd_perform_relocation.  Also,
  924.    bfd_perform_relocation is so hacked up it is easier to write a new
  925.    function than to try to deal with it.
  926.  
  927.    This routine does a final relocation.  It should not be used when
  928.    generating relocateable output.
  929.  
  930.    FIXME: This routine ignores any special_function in the HOWTO,
  931.    since the existing special_function values have been written for
  932.    bfd_perform_relocation.
  933.  
  934.    HOWTO is the reloc howto information.
  935.    INPUT_BFD is the BFD which the reloc applies to.
  936.    INPUT_SECTION is the section which the reloc applies to.
  937.    CONTENTS is the contents of the section.
  938.    ADDRESS is the address of the reloc within INPUT_SECTION.
  939.    VALUE is the value of the symbol the reloc refers to.
  940.    ADDEND is the addend of the reloc.  */
  941.  
  942. bfd_reloc_status_type
  943. _bfd_final_link_relocate (howto, input_bfd, input_section, contents, address,
  944.               value, addend)
  945.      const reloc_howto_type *howto;
  946.      bfd *input_bfd;
  947.      asection *input_section;
  948.      bfd_byte *contents;
  949.      bfd_vma address;
  950.      bfd_vma value;
  951.      bfd_vma addend;
  952. {
  953.   bfd_vma relocation;
  954.  
  955.   /* Sanity check the address.  */
  956.   if (address > input_section->_cooked_size)
  957.     return bfd_reloc_outofrange;
  958.  
  959.   /* This function assumes that we are dealing with a basic relocation
  960.      against a symbol.  We want to compute the value of the symbol to
  961.      relocate to.  This is just VALUE, the value of the symbol, plus
  962.      ADDEND, any addend associated with the reloc.  */
  963.   relocation = value + addend;
  964.  
  965.   /* If the relocation is PC relative, we want to set RELOCATION to
  966.      the distance between the symbol (currently in RELOCATION) and the
  967.      location we are relocating.  Some targets (e.g., i386-aout)
  968.      arrange for the contents of the section to be the negative of the
  969.      offset of the location within the section; for such targets
  970.      pcrel_offset is false.  Other targets (e.g., m88kbcs or ELF)
  971.      simply leave the contents of the section as zero; for such
  972.      targets pcrel_offset is true.  If pcrel_offset is false we do not
  973.      need to subtract out the offset of the location within the
  974.      section (which is just ADDRESS).  */
  975.   if (howto->pc_relative)
  976.     {
  977.       relocation -= (input_section->output_section->vma
  978.              + input_section->output_offset);
  979.       if (howto->pcrel_offset)
  980.     relocation -= address;
  981.     }
  982.  
  983.   return _bfd_relocate_contents (howto, input_bfd, relocation,
  984.                  contents + address);
  985. }
  986.  
  987. /* Relocate a given location using a given value and howto.  */
  988.  
  989. bfd_reloc_status_type
  990. _bfd_relocate_contents (howto, input_bfd, relocation, location)
  991.      const reloc_howto_type *howto;
  992.      bfd *input_bfd;
  993.      bfd_vma relocation;
  994.      bfd_byte *location;
  995. {
  996.   int size;
  997.   bfd_vma x;
  998.   boolean overflow;
  999.  
  1000.   /* If the size is negative, negate RELOCATION.  This isn't very
  1001.      general.  */
  1002.   if (howto->size < 0)
  1003.     relocation = -relocation;
  1004.  
  1005.   /* Get the value we are going to relocate.  */
  1006.   size = bfd_get_reloc_size (howto);
  1007.   switch (size)
  1008.     {
  1009.     default:
  1010.     case 0:
  1011.       abort ();
  1012.     case 1:
  1013.       x = bfd_get_8 (input_bfd, location);
  1014.       break;
  1015.     case 2:
  1016.       x = bfd_get_16 (input_bfd, location);
  1017.       break;
  1018.     case 4:
  1019.       x = bfd_get_32 (input_bfd, location);
  1020.       break;
  1021.     case 8:
  1022. #ifdef BFD64
  1023.       x = bfd_get_64 (input_bfd, location);
  1024. #else
  1025.       abort ();
  1026. #endif
  1027.       break;
  1028.     }
  1029.  
  1030.   /* Check for overflow.  FIXME: We may drop bits during the addition
  1031.      which we don't check for.  We must either check at every single
  1032.      operation, which would be tedious, or we must do the computations
  1033.      in a type larger than bfd_vma, which would be inefficient.  */
  1034.   overflow = false;
  1035.   if (howto->complain_on_overflow != complain_overflow_dont)
  1036.     {
  1037.       bfd_vma check;
  1038.       bfd_signed_vma signed_check;
  1039.       bfd_vma add;
  1040.       bfd_signed_vma signed_add;
  1041.  
  1042.       if (howto->rightshift == 0)
  1043.     {
  1044.       check = relocation;
  1045.       signed_check = (bfd_signed_vma) relocation;
  1046.     }
  1047.       else
  1048.     {
  1049.       /* Drop unwanted bits from the value we are relocating to.  */
  1050.       check = relocation >> howto->rightshift;
  1051.  
  1052.       /* If this is a signed value, the rightshift just dropped
  1053.          leading 1 bits (assuming twos complement).  */
  1054.       if ((bfd_signed_vma) relocation >= 0)
  1055.         signed_check = check;
  1056.       else
  1057.         signed_check = (check
  1058.                 | ((bfd_vma) - 1
  1059.                    & ~((bfd_vma) - 1 >> howto->rightshift)));
  1060.     }
  1061.  
  1062.       /* Get the value from the object file.  */
  1063.       add = x & howto->src_mask;
  1064.  
  1065.       /* Get the value from the object file with an appropriate sign.
  1066.      The expression involving howto->src_mask isolates the upper
  1067.      bit of src_mask.  If that bit is set in the value we are
  1068.      adding, it is negative, and we subtract out that number times
  1069.      two.  If src_mask includes the highest possible bit, then we
  1070.      can not get the upper bit, but that does not matter since
  1071.      signed_add needs no adjustment to become negative in that
  1072.      case.  */
  1073.       signed_add = add;
  1074.       if ((add & (((~howto->src_mask) >> 1) & howto->src_mask)) != 0)
  1075.     signed_add -= (((~howto->src_mask) >> 1) & howto->src_mask) << 1;
  1076.  
  1077.       /* Add the value from the object file, shifted so that it is a
  1078.      straight number.  */
  1079.       if (howto->bitpos == 0)
  1080.     {
  1081.       check += add;
  1082.       signed_check += signed_add;
  1083.     }
  1084.       else
  1085.     {
  1086.       check += add >> howto->bitpos;
  1087.  
  1088.       /* For the signed case we use ADD, rather than SIGNED_ADD,
  1089.          to avoid warnings from SVR4 cc.  This is OK since we
  1090.          explictly handle the sign bits.  */
  1091.       if (signed_add >= 0)
  1092.         signed_check += add >> howto->bitpos;
  1093.       else
  1094.         signed_check += ((add >> howto->bitpos)
  1095.                  | ((bfd_vma) - 1
  1096.                 & ~((bfd_vma) - 1 >> howto->bitpos)));
  1097.     }
  1098.  
  1099.       switch (howto->complain_on_overflow)
  1100.     {
  1101.     case complain_overflow_signed:
  1102.       {
  1103.         /* Assumes two's complement.  */
  1104.         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
  1105.         bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
  1106.  
  1107.         if (signed_check > reloc_signed_max
  1108.         || signed_check < reloc_signed_min)
  1109.           overflow = true;
  1110.       }
  1111.       break;
  1112.     case complain_overflow_unsigned:
  1113.       {
  1114.         /* Assumes two's complement.  This expression avoids
  1115.            overflow if howto->bitsize is the number of bits in
  1116.            bfd_vma.  */
  1117.         bfd_vma reloc_unsigned_max =
  1118.         (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  1119.  
  1120.         if (check > reloc_unsigned_max)
  1121.           overflow = true;
  1122.       }
  1123.       break;
  1124.     case complain_overflow_bitfield:
  1125.       {
  1126.         /* Assumes two's complement.  This expression avoids
  1127.            overflow if howto->bitsize is the number of bits in
  1128.            bfd_vma.  */
  1129.         bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  1130.  
  1131.         if ((check & ~reloc_bits) != 0
  1132.         && (((bfd_vma) signed_check & ~reloc_bits)
  1133.             != (-1 & ~reloc_bits)))
  1134.           overflow = true;
  1135.       }
  1136.       break;
  1137.     default:
  1138.       abort ();
  1139.     }
  1140.     }
  1141.  
  1142.   /* Put RELOCATION in the right bits.  */
  1143.   relocation >>= (bfd_vma) howto->rightshift;
  1144.   relocation <<= (bfd_vma) howto->bitpos;
  1145.  
  1146.   /* Add RELOCATION to the right bits of X.  */
  1147.   x = ((x & ~howto->dst_mask)
  1148.        | (((x & howto->src_mask) + relocation) & howto->dst_mask));
  1149.  
  1150.   /* Put the relocated value back in the object file.  */
  1151.   switch (size)
  1152.     {
  1153.     default:
  1154.     case 0:
  1155.       abort ();
  1156.     case 1:
  1157.       bfd_put_8 (input_bfd, x, location);
  1158.       break;
  1159.     case 2:
  1160.       bfd_put_16 (input_bfd, x, location);
  1161.       break;
  1162.     case 4:
  1163.       bfd_put_32 (input_bfd, x, location);
  1164.       break;
  1165.     case 8:
  1166. #ifdef BFD64
  1167.       bfd_put_64 (input_bfd, x, location);
  1168. #else
  1169.       abort ();
  1170. #endif
  1171.       break;
  1172.     }
  1173.  
  1174.   return overflow ? bfd_reloc_overflow : bfd_reloc_ok;
  1175. }
  1176.  
  1177. /*
  1178. DOCDD
  1179. INODE
  1180.     howto manager,  , typedef arelent, Relocations
  1181.  
  1182. SECTION
  1183.     The howto manager
  1184.  
  1185.     When an application wants to create a relocation, but doesn't
  1186.     know what the target machine might call it, it can find out by
  1187.     using this bit of code.
  1188.  
  1189. */
  1190.  
  1191. /*
  1192. TYPEDEF
  1193.     bfd_reloc_code_type
  1194.  
  1195. DESCRIPTION
  1196.     The insides of a reloc code.  The idea is that, eventually, there
  1197.     will be one enumerator for every type of relocation we ever do.
  1198.     Pass one of these values to <<bfd_reloc_type_lookup>>, and it'll
  1199.     return a howto pointer.
  1200.  
  1201.     This does mean that the application must determine the correct
  1202.     enumerator value; you can't get a howto pointer from a random set
  1203.     of attributes.
  1204.  
  1205. CODE_FRAGMENT
  1206. .
  1207. .typedef enum bfd_reloc_code_real
  1208. .{
  1209. .  {* Basic absolute relocations *}
  1210. .  BFD_RELOC_64,
  1211. .  BFD_RELOC_32,
  1212. .  BFD_RELOC_26,
  1213. .  BFD_RELOC_16,
  1214. .  BFD_RELOC_14,
  1215. .  BFD_RELOC_8,
  1216. .
  1217. .  {* PC-relative relocations *}
  1218. .  BFD_RELOC_64_PCREL,
  1219. .  BFD_RELOC_32_PCREL,
  1220. .  BFD_RELOC_24_PCREL,    {* used by i960 *}
  1221. .  BFD_RELOC_16_PCREL,
  1222. .  BFD_RELOC_8_PCREL,
  1223. .
  1224. .  {* Linkage-table relative *}
  1225. .  BFD_RELOC_32_BASEREL,
  1226. .  BFD_RELOC_16_BASEREL,
  1227. .  BFD_RELOC_8_BASEREL,
  1228. .
  1229. .  {* The type of reloc used to build a contructor table - at the moment
  1230. .     probably a 32 bit wide abs address, but the cpu can choose. *}
  1231. .  BFD_RELOC_CTOR,
  1232. .
  1233. .  {* 8 bits wide, but used to form an address like 0xffnn *}
  1234. .  BFD_RELOC_8_FFnn,
  1235. .
  1236. .  {* 32-bit pc-relative, shifted right 2 bits (i.e., 30-bit
  1237. .     word displacement, e.g. for SPARC) *}
  1238. .  BFD_RELOC_32_PCREL_S2,
  1239. .  {* signed 16-bit pc-relative, shifted right 2 bits (e.g. for MIPS) *}
  1240. .  BFD_RELOC_16_PCREL_S2,
  1241. .  {* this is used on the Alpha *}
  1242. .  BFD_RELOC_23_PCREL_S2,
  1243. .
  1244. .  {* High 22 bits of 32-bit value, placed into lower 22 bits of
  1245. .     target word; simple reloc.  *}
  1246. .  BFD_RELOC_HI22,
  1247. .  {* Low 10 bits.  *}
  1248. .  BFD_RELOC_LO10,
  1249. .
  1250. .  {* For systems that allocate a Global Pointer register, these are
  1251. .     displacements off that register.  These relocation types are
  1252. .     handled specially, because the value the register will have is
  1253. .     decided relatively late.  *}
  1254. .  BFD_RELOC_GPREL16,
  1255. .  BFD_RELOC_GPREL32,
  1256. .
  1257. .  {* Reloc types used for i960/b.out.  *}
  1258. .  BFD_RELOC_I960_CALLJ,
  1259. .
  1260. .  {* now for the sparc/elf codes *}
  1261. .  BFD_RELOC_NONE,        {* actually used *}
  1262. .  BFD_RELOC_SPARC_WDISP22,
  1263. .  BFD_RELOC_SPARC22,
  1264. .  BFD_RELOC_SPARC13,
  1265. .  BFD_RELOC_SPARC_GOT10,
  1266. .  BFD_RELOC_SPARC_GOT13,
  1267. .  BFD_RELOC_SPARC_GOT22,
  1268. .  BFD_RELOC_SPARC_PC10,
  1269. .  BFD_RELOC_SPARC_PC22,
  1270. .  BFD_RELOC_SPARC_WPLT30,
  1271. .  BFD_RELOC_SPARC_COPY,
  1272. .  BFD_RELOC_SPARC_GLOB_DAT,
  1273. .  BFD_RELOC_SPARC_JMP_SLOT,
  1274. .  BFD_RELOC_SPARC_RELATIVE,
  1275. .  BFD_RELOC_SPARC_UA32,
  1276. .
  1277. .  {* these are a.out specific? *}
  1278. .  BFD_RELOC_SPARC_BASE13,
  1279. .  BFD_RELOC_SPARC_BASE22,
  1280. .
  1281. .  {* some relocations we're using for sparc v9
  1282. .     -- subject to change *}
  1283. .  BFD_RELOC_SPARC_10,
  1284. .  BFD_RELOC_SPARC_11,
  1285. .#define  BFD_RELOC_SPARC_64 BFD_RELOC_64
  1286. .  BFD_RELOC_SPARC_OLO10,
  1287. .  BFD_RELOC_SPARC_HH22,
  1288. .  BFD_RELOC_SPARC_HM10,
  1289. .  BFD_RELOC_SPARC_LM22,
  1290. .  BFD_RELOC_SPARC_PC_HH22,
  1291. .  BFD_RELOC_SPARC_PC_HM10,
  1292. .  BFD_RELOC_SPARC_PC_LM22,
  1293. .  BFD_RELOC_SPARC_WDISP16,
  1294. .  BFD_RELOC_SPARC_WDISP19,
  1295. .  BFD_RELOC_SPARC_GLOB_JMP,
  1296. .  BFD_RELOC_SPARC_LO7,
  1297. .
  1298. .  {* Alpha ECOFF relocations.  Some of these treat the symbol or "addend"
  1299. .     in some special way.  *}
  1300. .  {* For GPDISP_HI16 ("gpdisp") relocations, the symbol is ignored when
  1301. .     writing; when reading, it will be the absolute section symbol.  The
  1302. .     addend is the displacement in bytes of the "lda" instruction from
  1303. .     the "ldah" instruction (which is at the address of this reloc).  *}
  1304. .  BFD_RELOC_ALPHA_GPDISP_HI16,
  1305. .  {* For GPDISP_LO16 ("ignore") relocations, the symbol is handled as
  1306. .     with GPDISP_HI16 relocs.  The addend is ignored when writing the
  1307. .     relocations out, and is filled in with the file's GP value on
  1308. .     reading, for convenience.  *}
  1309. .  BFD_RELOC_ALPHA_GPDISP_LO16,
  1310. .
  1311. .  {* The Alpha LITERAL/LITUSE relocs are produced by a symbol reference;
  1312. .     the assembler turns it into a LDQ instruction to load the address of
  1313. .     the symbol, and then fills in a register in the real instruction.
  1314. .
  1315. .     The LITERAL reloc, at the LDQ instruction, refers to the .lita
  1316. .     section symbol.  The addend is ignored when writing, but is filled
  1317. .     in with the file's GP value on reading, for convenience, as with the
  1318. .     GPDISP_LO16 reloc.
  1319. .
  1320. .     The LITUSE reloc, on the instruction using the loaded address, gives
  1321. .     information to the linker that it might be able to use to optimize
  1322. .     away some literal section references.  The symbol is ignored (read
  1323. .     as the absolute section symbol), and the "addend" indicates the type
  1324. .     of instruction using the register:
  1325. .              1 - "memory" fmt insn
  1326. .              2 - byte-manipulation (byte offset reg)
  1327. .              3 - jsr (target of branch)
  1328. .
  1329. .     The GNU linker currently doesn't do any of this optimizing.  *}
  1330. .  BFD_RELOC_ALPHA_LITERAL,
  1331. .  BFD_RELOC_ALPHA_LITUSE,
  1332. .
  1333. .  {* The HINT relocation indicates a value that should be filled into the
  1334. .     "hint" field of a jmp/jsr/ret instruction, for possible branch-
  1335. .     prediction logic which may be provided on some processors.  *}
  1336. .  BFD_RELOC_ALPHA_HINT,
  1337. .
  1338. .  {* Bits 27..2 of the relocation address shifted right 2 bits;
  1339. .     simple reloc otherwise.  *}
  1340. .  BFD_RELOC_MIPS_JMP,
  1341. .
  1342. .  {* High 16 bits of 32-bit value; simple reloc.  *}
  1343. .  BFD_RELOC_HI16,
  1344. .  {* High 16 bits of 32-bit value but the low 16 bits will be sign
  1345. .     extended and added to form the final result.  If the low 16
  1346. .     bits form a negative number, we need to add one to the high value
  1347. .     to compensate for the borrow when the low bits are added.  *}
  1348. .  BFD_RELOC_HI16_S,
  1349. .  {* Low 16 bits.  *}
  1350. .  BFD_RELOC_LO16,
  1351. .  {* Like BFD_RELOC_HI16_S, but PC relative.  *}
  1352. .  BFD_RELOC_PCREL_HI16_S,
  1353. .  {* Like BFD_RELOC_LO16, but PC relative.  *}
  1354. .  BFD_RELOC_PCREL_LO16,
  1355. .
  1356. .  {* relocation relative to the global pointer.  *}
  1357. .#define BFD_RELOC_MIPS_GPREL BFD_RELOC_GPREL16
  1358. .
  1359. .  {* Relocation against a MIPS literal section.  *}
  1360. .  BFD_RELOC_MIPS_LITERAL,
  1361. .
  1362. .  {* MIPS ELF relocations.  *}
  1363. .  BFD_RELOC_MIPS_GOT16,
  1364. .  BFD_RELOC_MIPS_CALL16,
  1365. .#define BFD_RELOC_MIPS_GPREL32 BFD_RELOC_GPREL32
  1366. .
  1367. .  {* i386/elf relocations *}
  1368. .  BFD_RELOC_386_GOT32,
  1369. .  BFD_RELOC_386_PLT32,
  1370. .  BFD_RELOC_386_COPY,
  1371. .  BFD_RELOC_386_GLOB_DAT,
  1372. .  BFD_RELOC_386_JUMP_SLOT,
  1373. .  BFD_RELOC_386_RELATIVE,
  1374. .  BFD_RELOC_386_GOTOFF,
  1375. .  BFD_RELOC_386_GOTPC,
  1376. .
  1377. .  {* ns32k relocations *}
  1378. .  BFD_RELOC_NS32K_IMM_8,
  1379. .  BFD_RELOC_NS32K_IMM_16,
  1380. .  BFD_RELOC_NS32K_IMM_32,
  1381. .  BFD_RELOC_NS32K_IMM_8_PCREL,
  1382. .  BFD_RELOC_NS32K_IMM_16_PCREL,
  1383. .  BFD_RELOC_NS32K_IMM_32_PCREL,
  1384. .  BFD_RELOC_NS32K_DISP_8,
  1385. .  BFD_RELOC_NS32K_DISP_16,
  1386. .  BFD_RELOC_NS32K_DISP_32,
  1387. .  BFD_RELOC_NS32K_DISP_8_PCREL,
  1388. .  BFD_RELOC_NS32K_DISP_16_PCREL,
  1389. .  BFD_RELOC_NS32K_DISP_32_PCREL,
  1390. .
  1391. .  {* PowerPC/POWER (RS/6000) relocs.  *}
  1392. .  {* 26 bit relative branch.  Low two bits must be zero.  High 24
  1393. .     bits installed in bits 6 through 29 of instruction.  *}
  1394. .  BFD_RELOC_PPC_B26,
  1395. .  {* 26 bit absolute branch, like BFD_RELOC_PPC_B26 but absolute.  *}
  1396. .  BFD_RELOC_PPC_BA26,
  1397. .  {* 16 bit TOC relative reference.  *}
  1398. .  BFD_RELOC_PPC_TOC16,
  1399. .
  1400. .  {* this must be the highest numeric value *}
  1401. .  BFD_RELOC_UNUSED
  1402. . } bfd_reloc_code_real_type;
  1403. */
  1404.  
  1405.  
  1406. /*
  1407. FUNCTION
  1408.     bfd_reloc_type_lookup
  1409.  
  1410. SYNOPSIS
  1411.     const struct reloc_howto_struct *
  1412.     bfd_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code);
  1413.  
  1414. DESCRIPTION
  1415.     Return a pointer to a howto structure which, when
  1416.     invoked, will perform the relocation @var{code} on data from the
  1417.     architecture noted.
  1418.  
  1419. */
  1420.  
  1421.  
  1422. const struct reloc_howto_struct *
  1423. bfd_reloc_type_lookup (abfd, code)
  1424.      bfd *abfd;
  1425.      bfd_reloc_code_real_type code;
  1426. {
  1427.   return BFD_SEND (abfd, reloc_type_lookup, (abfd, code));
  1428. }
  1429.  
  1430. static reloc_howto_type bfd_howto_32 =
  1431. HOWTO (0, 00, 2, 32, false, 0, complain_overflow_bitfield, 0, "VRT32", false, 0xffffffff, 0xffffffff, true);
  1432.  
  1433.  
  1434. /*
  1435. INTERNAL_FUNCTION
  1436.     bfd_default_reloc_type_lookup
  1437.  
  1438. SYNOPSIS
  1439.     const struct reloc_howto_struct *bfd_default_reloc_type_lookup
  1440.     (bfd *abfd, bfd_reloc_code_real_type  code);
  1441.  
  1442. DESCRIPTION
  1443.     Provides a default relocation lookup routine for any architecture.
  1444.  
  1445.  
  1446. */
  1447.  
  1448. const struct reloc_howto_struct *
  1449. bfd_default_reloc_type_lookup (abfd, code)
  1450.      bfd *abfd;
  1451.      bfd_reloc_code_real_type code;
  1452. {
  1453.   switch (code)
  1454.     {
  1455.     case BFD_RELOC_CTOR:
  1456.       /* The type of reloc used in a ctor, which will be as wide as the
  1457.      address - so either a 64, 32, or 16 bitter.  */
  1458.       switch (bfd_get_arch_info (abfd)->bits_per_address)
  1459.     {
  1460.     case 64:
  1461.       BFD_FAIL ();
  1462.     case 32:
  1463.       return &bfd_howto_32;
  1464.     case 16:
  1465.       BFD_FAIL ();
  1466.     default:
  1467.       BFD_FAIL ();
  1468.     }
  1469.     default:
  1470.       BFD_FAIL ();
  1471.     }
  1472.   return (const struct reloc_howto_struct *) NULL;
  1473. }
  1474.  
  1475.  
  1476. /*
  1477. INTERNAL_FUNCTION
  1478.     bfd_generic_relax_section
  1479.  
  1480. SYNOPSIS
  1481.     boolean bfd_generic_relax_section
  1482.      (bfd *abfd,
  1483.       asection *section,
  1484.       struct bfd_link_info *,
  1485.       boolean *);
  1486.  
  1487. DESCRIPTION
  1488.     Provides default handling for relaxing for back ends which
  1489.     don't do relaxing -- i.e., does nothing.
  1490. */
  1491.  
  1492. /*ARGSUSED*/
  1493. boolean
  1494. bfd_generic_relax_section (abfd, section, link_info, again)
  1495.      bfd *abfd;
  1496.      asection *section;
  1497.      struct bfd_link_info *link_info;
  1498.      boolean *again;
  1499. {
  1500.   *again = false;
  1501.   return true;
  1502. }
  1503.  
  1504. /*
  1505. INTERNAL_FUNCTION
  1506.     bfd_generic_get_relocated_section_contents
  1507.  
  1508. SYNOPSIS
  1509.     bfd_byte *
  1510.        bfd_generic_get_relocated_section_contents (bfd *abfd,
  1511.          struct bfd_link_info *link_info,
  1512.          struct bfd_link_order *link_order,
  1513.          bfd_byte *data,
  1514.          boolean relocateable,
  1515.          asymbol **symbols);
  1516.  
  1517. DESCRIPTION
  1518.     Provides default handling of relocation effort for back ends
  1519.     which can't be bothered to do it efficiently.
  1520.  
  1521. */
  1522.  
  1523. bfd_byte *
  1524. bfd_generic_get_relocated_section_contents (abfd, link_info, link_order, data,
  1525.                         relocateable, symbols)
  1526.      bfd *abfd;
  1527.      struct bfd_link_info *link_info;
  1528.      struct bfd_link_order *link_order;
  1529.      bfd_byte *data;
  1530.      boolean relocateable;
  1531.      asymbol **symbols;
  1532. {
  1533.   /* Get enough memory to hold the stuff */
  1534.   bfd *input_bfd = link_order->u.indirect.section->owner;
  1535.   asection *input_section = link_order->u.indirect.section;
  1536.  
  1537.   long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
  1538.   arelent **reloc_vector = NULL;
  1539.   long reloc_count;
  1540.  
  1541.   if (reloc_size < 0)
  1542.     goto error_return;
  1543.  
  1544.   reloc_vector = (arelent **) malloc (reloc_size);
  1545.   if (reloc_vector == NULL && reloc_size != 0)
  1546.     {
  1547.       bfd_set_error (bfd_error_no_memory);
  1548.       goto error_return;
  1549.     }
  1550.  
  1551.   /* read in the section */
  1552.   if (!bfd_get_section_contents (input_bfd,
  1553.                  input_section,
  1554.                  (PTR) data,
  1555.                  0,
  1556.                  input_section->_raw_size))
  1557.     goto error_return;
  1558.  
  1559.   /* We're not relaxing the section, so just copy the size info */
  1560.   input_section->_cooked_size = input_section->_raw_size;
  1561.   input_section->reloc_done = true;
  1562.  
  1563.   reloc_count = bfd_canonicalize_reloc (input_bfd,
  1564.                     input_section,
  1565.                     reloc_vector,
  1566.                     symbols);
  1567.   if (reloc_count < 0)
  1568.     goto error_return;
  1569.  
  1570.   if (reloc_count > 0)
  1571.     {
  1572.       arelent **parent;
  1573.       for (parent = reloc_vector; *parent != (arelent *) NULL;
  1574.        parent++)
  1575.     {
  1576.       char *error_message = (char *) NULL;
  1577.       bfd_reloc_status_type r =
  1578.         bfd_perform_relocation (input_bfd,
  1579.                     *parent,
  1580.                     (PTR) data,
  1581.                     input_section,
  1582.                     relocateable ? abfd : (bfd *) NULL,
  1583.                     &error_message);
  1584.  
  1585.       if (relocateable)
  1586.         {
  1587.           asection *os = input_section->output_section;
  1588.  
  1589.           /* A partial link, so keep the relocs */
  1590.           os->orelocation[os->reloc_count] = *parent;
  1591.           os->reloc_count++;
  1592.         }
  1593.  
  1594.       if (r != bfd_reloc_ok)
  1595.         {
  1596.           switch (r)
  1597.         {
  1598.         case bfd_reloc_undefined:
  1599.           if (!((*link_info->callbacks->undefined_symbol)
  1600.             (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
  1601.              input_bfd, input_section, (*parent)->address)))
  1602.             goto error_return;
  1603.           break;
  1604.         case bfd_reloc_dangerous:
  1605.           BFD_ASSERT (error_message != (char *) NULL);
  1606.           if (!((*link_info->callbacks->reloc_dangerous)
  1607.             (link_info, error_message, input_bfd, input_section,
  1608.              (*parent)->address)))
  1609.             goto error_return;
  1610.           break;
  1611.         case bfd_reloc_overflow:
  1612.           if (!((*link_info->callbacks->reloc_overflow)
  1613.             (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
  1614.              (*parent)->howto->name, (*parent)->addend,
  1615.              input_bfd, input_section, (*parent)->address)))
  1616.             goto error_return;
  1617.           break;
  1618.         case bfd_reloc_outofrange:
  1619.         default:
  1620.           abort ();
  1621.           break;
  1622.         }
  1623.  
  1624.         }
  1625.     }
  1626.     }
  1627.   if (reloc_vector != NULL)
  1628.     free (reloc_vector);
  1629.   return data;
  1630.  
  1631. error_return:
  1632.   if (reloc_vector != NULL)
  1633.     free (reloc_vector);
  1634.   return NULL;
  1635. }
  1636.