home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / gnu / djgpp / src / binutils.2 / bfd / reloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-30  |  28.7 KB  |  1,064 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 was as it maintains
  26.     symbols; they are left alone until required, then read in
  27.     en-mass and traslated into an internal form. There is a common
  28.     routine <<bfd_perform_relocation>> which acts upon the
  29.     canonical form to to the actual fixup.
  30.  
  31.     Note that relocations are maintained on a per section basis,
  32.     whilst symbols are maintained on a per BFD basis.
  33.  
  34.     All a back end has to do to fit the BFD interface is to create
  35.     as many <<struct reloc_cache_entry>> as there are relocations
  36.     in a particular section, and fill in the right bits:
  37.  
  38. @menu
  39. @* typedef arelent::
  40. @* howto manager::
  41. @end menu
  42.  
  43. */
  44. #include "bfd.h"
  45. #include "sysdep.h"
  46. #include "libbfd.h"
  47. #include "seclet.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. .       {* Unused *}
  75. .  bfd_reloc_notsupported,
  76. .
  77. .       {* Unsupported relocation size requested. *}
  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. *}
  86. .  bfd_reloc_dangerous
  87. . }
  88. . bfd_reloc_status_type;
  89. .
  90. .
  91. .typedef struct reloc_cache_entry 
  92. .{
  93. .       {* A pointer into the canonical table of pointers  *}
  94. .  struct symbol_cache_entry **sym_ptr_ptr;
  95. .
  96. .       {* offset in section *}
  97. .  bfd_size_type address;
  98. .
  99. .       {* addend for relocation value *}
  100. .  bfd_vma addend;    
  101. .
  102. .       {* Pointer to how to perform the required relocation *}
  103. .  CONST struct reloc_howto_struct *howto;
  104. .
  105. .} arelent;
  106.  
  107. */
  108.  
  109. /*
  110. DESCRIPTION
  111.  
  112.         Here is a description of each of the fields within a relent:
  113.  
  114.         o sym_ptr_ptr
  115.  
  116.         The symbol table pointer points to a pointer to the symbol
  117.         associated with the relocation request. This would naturally
  118.         be the pointer into the table returned by the back end's
  119.         get_symtab action. @xref{Symbols}. The symbol is referenced
  120.         through a pointer to a pointer so that tools like the linker
  121.         can fix up all the symbols of the same name by modifying only
  122.         one pointer. The relocation routine looks in the symbol and
  123.         uses the base of the section the symbol is attached to and the
  124.         value of the symbol as the initial relocation offset. If the
  125.         symbol pointer is zero, then the section provided is looked up.
  126.  
  127.         o address
  128.  
  129.         The address field gives the offset in bytes from the base of
  130.         the section data which owns the relocation record to the first
  131.         byte of relocatable information. The actual data relocated
  132.         will be relative to this point - for example, a relocation
  133.         type which modifies the bottom two bytes of a four byte word
  134.         would not touch the first byte pointed to in a big endian
  135.         world.
  136.     
  137.     o addend
  138.  
  139.     The addend is a value provided by the back end to be added (!)
  140.     to the relocation offset. Its interpretation is dependent upon
  141.     the howto. For example, on the 68k the code:
  142.  
  143.  
  144. |        char foo[];
  145. |        main()
  146. |                {
  147. |                return foo[0x12345678];
  148. |                }
  149.  
  150.         Could be compiled into:
  151.  
  152. |        linkw fp,#-4
  153. |        moveb @@#12345678,d0
  154. |        extbl d0
  155. |        unlk fp
  156. |        rts
  157.  
  158.  
  159.         This could create a reloc pointing to foo, but leave the
  160.         offset in the data (something like)
  161.  
  162.  
  163. |RELOCATION RECORDS FOR [.text]:
  164. |offset   type      value 
  165. |00000006 32        _foo
  166. |
  167. |00000000 4e56 fffc          ; linkw fp,#-4
  168. |00000004 1039 1234 5678     ; moveb @@#12345678,d0
  169. |0000000a 49c0               ; extbl d0
  170. |0000000c 4e5e               ; unlk fp
  171. |0000000e 4e75               ; rts
  172.  
  173.  
  174.         Using coff and an 88k, some instructions don't have enough
  175.         space in them to represent the full address range, and
  176.         pointers have to be loaded in two parts. So you'd get something like:
  177.  
  178.  
  179. |        or.u     r13,r0,hi16(_foo+0x12345678)
  180. |        ld.b     r2,r13,lo16(_foo+0x12345678)
  181. |        jmp      r1
  182.  
  183.  
  184.         This should create two relocs, both pointing to _foo, and with
  185.         0x12340000 in their addend field. The data would consist of:
  186.  
  187.  
  188. |RELOCATION RECORDS FOR [.text]:
  189. |offset   type      value 
  190. |00000002 HVRT16    _foo+0x12340000
  191. |00000006 LVRT16    _foo+0x12340000
  192.  
  193. |00000000 5da05678           ; or.u r13,r0,0x5678
  194. |00000004 1c4d5678           ; ld.b r2,r13,0x5678
  195. |00000008 f400c001           ; jmp r1
  196.  
  197.  
  198.         The relocation routine digs out the value from the data, adds
  199.         it to the addend to get the original offset and then adds the
  200.         value of _foo. Note that all 32 bits have to be kept around
  201.         somewhere, to cope with carry from bit 15 to bit 16.
  202.  
  203.         One further example is the sparc and the a.out format. The
  204.         sparc has a similar problem to the 88k, in that some
  205.         instructions don't have room for an entire offset, but on the
  206.         sparc the parts are created odd sized lumps. The designers of
  207.         the a.out format chose not to use the data within the section
  208.         for storing part of the offset; all the offset is kept within
  209.         the reloc. Any thing in the data should be ignored. 
  210.  
  211. |        save %sp,-112,%sp
  212. |        sethi %hi(_foo+0x12345678),%g2
  213. |        ldsb [%g2+%lo(_foo+0x12345678)],%i0
  214. |        ret
  215. |        restore
  216.  
  217.         Both relocs contains a pointer to foo, and the offsets would
  218.         contain junk.
  219.  
  220.  
  221. |RELOCATION RECORDS FOR [.text]:
  222. |offset   type      value 
  223. |00000004 HI22      _foo+0x12345678
  224. |00000008 LO10      _foo+0x12345678
  225.  
  226. |00000000 9de3bf90     ; save %sp,-112,%sp
  227. |00000004 05000000     ; sethi %hi(_foo+0),%g2
  228. |00000008 f048a000     ; ldsb [%g2+%lo(_foo+0)],%i0
  229. |0000000c 81c7e008     ; ret
  230. |00000010 81e80000     ; restore
  231.  
  232.  
  233.         o howto 
  234.  
  235.         The howto field can be imagined as a
  236.         relocation instruction. It is a pointer to a struct which
  237.         contains information on what to do with all the other
  238.         information in the reloc record and data section. A back end
  239.         would normally have a relocation instruction set and turn
  240.         relocations into pointers to the correct structure on input -
  241.         but it would be possible to create each howto field on demand.
  242.         
  243. */
  244.  
  245.  
  246. /*
  247. SUBSUBSECTION 
  248.         <<reloc_howto_type>>
  249.  
  250.         The <<reloc_howto_type>> is a structure which contains all the
  251.         information that BFD needs to know to tie up a back end's data.
  252.  
  253. CODE_FRAGMENT
  254. .struct symbol_cache_entry;        {* Forward declaration *}
  255. .
  256. .typedef CONST struct reloc_howto_struct 
  257. .{ 
  258. .       {*  The type field has mainly a documetary use - the back end can
  259. .           to what it wants with it, though the normally the back end's
  260. .           external idea of what a reloc number would be would be stored
  261. .           in this field. For example, the a PC relative word relocation
  262. .           in a coff environment would have the type 023 - because that's
  263. .           what the outside world calls a R_PCRWORD reloc. *}
  264. .  unsigned int type;
  265. .
  266. .       {*  The value the final relocation is shifted right by. This drops
  267. .           unwanted data from the relocation.  *}
  268. .  unsigned int rightshift;
  269. .
  270. .       {*  The size of the item to be relocated - 0, is one byte, 1 is 2
  271. .           bytes, 2 is four bytes.  A negative value indicates that the
  272. .        result is to be subtracted from the data.  *}
  273. .  int size;
  274. .
  275. .       {*  Now obsolete?  But m68k-coff still uses it... *}
  276. .  unsigned int bitsize;
  277. .
  278. .       {*  Notes that the relocation is relative to the location in the
  279. .           data section of the addend. The relocation function will
  280. .           subtract from the relocation value the address of the location
  281. .           being relocated. *}
  282. .  boolean pc_relative;
  283. .
  284. .  unsigned int bitpos;
  285. .
  286. .       {*  Now obsolete *}
  287. .  boolean absolute;
  288. .
  289. .       {* Causes the relocation routine to return an error if overflow
  290. .          is detected when relocating. *}
  291. .  boolean complain_on_overflow;
  292. .
  293. .       {* If this field is non null, then the supplied function is
  294. .          called rather than the normal function. This allows really
  295. .          strange relocation methods to be accomodated (e.g., i960 callj
  296. .          instructions). *}
  297. .  bfd_reloc_status_type (*special_function) 
  298. .                    PARAMS ((bfd *abfd,
  299. .                         arelent *reloc_entry,
  300. .                                            struct symbol_cache_entry *symbol,
  301. .                                            PTR data,
  302. .                                            asection *input_section, 
  303. .                                            bfd *output_bfd));
  304. .
  305. .       {* The textual name of the relocation type. *}
  306. .  char *name;
  307. .
  308. .       {* When performing a partial link, some formats must modify the
  309. .          relocations rather than the data - this flag signals this.*}
  310. .  boolean partial_inplace;
  311. .
  312. .       {* The src_mask is used to select what parts of the read in data
  313. .          are to be used in the relocation sum.  E.g., if this was an 8 bit
  314. .          bit of data which we read and relocated, this would be
  315. .          0x000000ff. When we have relocs which have an addend, such as
  316. .          sun4 extended relocs, the value in the offset part of a
  317. .          relocating field is garbage so we never use it. In this case
  318. .          the mask would be 0x00000000. *}
  319. .  bfd_vma src_mask;
  320. .
  321. .       {* The dst_mask is what parts of the instruction are replaced
  322. .          into the instruction. In most cases src_mask == dst_mask,
  323. .          except in the above special case, where dst_mask would be
  324. .          0x000000ff, and src_mask would be 0x00000000.   *}
  325. .  bfd_vma dst_mask;           
  326. .
  327. .       {* When some formats create PC relative instructions, they leave
  328. .          the value of the pc of the place being relocated in the offset
  329. .          slot of the instruction, so that a PC relative relocation can
  330. .          be made just by adding in an ordinary offset (e.g., sun3 a.out).
  331. .          Some formats leave the displacement part of an instruction
  332. .          empty (e.g., m88k bcs), this flag signals the fact.*}
  333. .  boolean pcrel_offset;
  334. .
  335. .} reloc_howto_type;
  336.  
  337. */
  338.  
  339. /*
  340. FUNCTION
  341.     the HOWTO macro
  342.  
  343. DESCRIPTION
  344.     The HOWTO define is horrible and will go away.
  345.  
  346.  
  347. .#define HOWTO(C, R,S,B, P, BI, ABS, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
  348. .  {(unsigned)C,R,S,B, P, BI, ABS,O,SF,NAME,INPLACE,MASKSRC,MASKDST,PC}
  349.  
  350. DESCRIPTION
  351.     And will be replaced with the totally magic way. But for the
  352.     moment, we are compatible, so do it this way..
  353.  
  354.  
  355. .#define NEWHOWTO( FUNCTION, NAME,SIZE,REL,IN) HOWTO(0,0,SIZE,0,REL,0,false,false,FUNCTION, NAME,false,0,0,IN)
  356. .
  357. DESCRIPTION
  358.     Helper routine to turn a symbol into a relocation value.
  359.  
  360. .#define HOWTO_PREPARE(relocation, symbol)      \
  361. .  {                                            \
  362. .  if (symbol != (asymbol *)NULL) {             \
  363. .    if (bfd_is_com_section (symbol->section)) { \
  364. .      relocation = 0;                          \
  365. .    }                                          \
  366. .    else {                                     \
  367. .      relocation = symbol->value;              \
  368. .    }                                          \
  369. .  }                                            \
  370. .}                      
  371.  
  372. */
  373.  
  374. /*
  375. TYPEDEF
  376.     reloc_chain
  377.  
  378. DESCRIPTION
  379.  
  380.     How relocs are tied together
  381.  
  382. .typedef unsigned char bfd_byte;
  383. .
  384. .typedef struct relent_chain {
  385. .  arelent relent;
  386. .  struct   relent_chain *next;
  387. .} arelent_chain;
  388.  
  389. */
  390.  
  391.  
  392.  
  393. /*
  394. FUNCTION 
  395.     bfd_perform_relocation
  396.  
  397. SYNOPSIS
  398.     bfd_reloc_status_type
  399.                 bfd_perform_relocation
  400.                         (bfd * abfd,
  401.                         arelent *reloc_entry,
  402.                         PTR data,
  403.                         asection *input_section,
  404.                         bfd *output_bfd);
  405.  
  406. DESCRIPTION
  407.     If an output_bfd is supplied to this function the generated
  408.     image will be relocatable, the relocations are copied to the
  409.     output file after they have been changed to reflect the new
  410.     state of the world. There are two ways of reflecting the
  411.     results of partial linkage in an output file; by modifying the
  412.     output data in place, and by modifying the relocation record.
  413.     Some native formats (e.g., basic a.out and basic coff) have no
  414.     way of specifying an addend in the relocation type, so the
  415.     addend has to go in the output data.  This is no big deal
  416.     since in these formats the output data slot will always be big
  417.     enough for the addend. Complex reloc types with addends were
  418.     invented to solve just this problem.
  419.  
  420. */
  421.  
  422.  
  423. bfd_reloc_status_type
  424. DEFUN(bfd_perform_relocation,(abfd,
  425.                               reloc_entry,
  426.                               data,
  427.                               input_section,
  428.                               output_bfd),
  429.       bfd *abfd AND
  430.       arelent *reloc_entry AND
  431.       PTR data AND
  432.       asection *input_section AND
  433.       bfd *output_bfd)
  434. {
  435.   bfd_vma relocation;
  436.   bfd_reloc_status_type flag = bfd_reloc_ok;
  437.   bfd_size_type addr = reloc_entry->address ;
  438.   bfd_vma output_base = 0;
  439.   reloc_howto_type *howto = reloc_entry->howto;
  440.   asection *reloc_target_output_section ;
  441.  
  442.   asymbol *symbol;
  443.  
  444.   symbol = *( reloc_entry->sym_ptr_ptr);
  445.   if ((symbol->section == &bfd_abs_section) 
  446.       && output_bfd != (bfd *)NULL) 
  447.   {
  448.     reloc_entry->address += input_section->output_offset;
  449.        
  450.     return bfd_reloc_ok;
  451.        
  452.   }
  453.  
  454.   if ((symbol->section == &bfd_und_section) && output_bfd == (bfd *)NULL) {
  455.     flag = bfd_reloc_undefined;
  456.   }
  457.  
  458.   if (howto->special_function) {
  459.     bfd_reloc_status_type cont;
  460.     cont = howto->special_function(abfd,
  461.                    reloc_entry,
  462.                    symbol,
  463.                    data,
  464.                    input_section,
  465.                    output_bfd);
  466.     if (cont != bfd_reloc_continue) return cont;
  467.   }
  468.  
  469.   /* 
  470.     Work out which section the relocation is targetted at and the
  471.     initial relocation command value.
  472.     */
  473.  
  474.  
  475.   if (bfd_is_com_section (symbol->section)) {
  476.     relocation = 0;
  477.   }
  478.   else {
  479.     relocation = symbol->value;
  480.   }
  481.  
  482.  
  483.   reloc_target_output_section = symbol->section->output_section;
  484.  
  485.   if (output_bfd && howto->partial_inplace==false) {
  486.     output_base = 0;
  487.   }
  488.   else {
  489.     output_base = reloc_target_output_section->vma;
  490.  
  491.   }
  492.  
  493.   relocation += output_base + symbol->section->output_offset;
  494.  
  495.   relocation += reloc_entry->addend;
  496.  
  497.   if(reloc_entry->address > input_section->_cooked_size)
  498.   {
  499.     return bfd_reloc_outofrange;
  500.   }
  501.           
  502.  
  503.   if (howto->pc_relative == true)
  504.   {
  505.     /*
  506.       Anything which started out as pc relative should end up that
  507.       way too. 
  508.       
  509.       There are two ways we can see a pcrel instruction. Sometimes
  510.       the pcrel displacement has been partially calculated, it
  511.       includes the distance from the start of the section to the
  512.       instruction in it (e.g., sun3), and sometimes the field is
  513.       totally blank - e.g., m88kbcs.
  514.       */
  515.  
  516.         
  517.     relocation -= 
  518.      input_section->output_section->vma + input_section->output_offset;
  519.  
  520.     if (howto->pcrel_offset == true) {
  521.       relocation -= reloc_entry->address;
  522.     }
  523.  
  524.   }
  525.  
  526.   if (output_bfd!= (bfd *)NULL) 
  527.   {
  528.     if ( howto->partial_inplace == false)  
  529.     {
  530.       /*
  531.     This is a partial relocation, and we want to apply the relocation
  532.     to the reloc entry rather than the raw data. Modify the reloc
  533.     inplace to reflect what we now know.
  534.     */
  535.       reloc_entry->addend = relocation  ;
  536.       reloc_entry->address +=  input_section->output_offset;
  537.       return flag;
  538.     }
  539.     else 
  540.     {
  541.       /* This is a partial relocation, but inplace, so modify the
  542.      reloc record a bit. 
  543.      
  544.      If we've relocated with a symbol with a section, change
  545.      into a ref to  the section belonging to the symbol
  546.      */
  547.  
  548.       reloc_entry->address += input_section->output_offset;
  549.  
  550.       if (abfd->xvec->flavour == bfd_target_coff_flavour) 
  551.       {
  552.     relocation -= reloc_entry->addend;
  553.     reloc_entry->addend = 0;
  554.       }
  555.       else
  556.       {
  557.     reloc_entry->addend = relocation  ;
  558.       }
  559.     }
  560.   }
  561.   else 
  562.   {
  563.     reloc_entry->addend = 0;
  564.   }
  565.   
  566.  
  567.   /* 
  568.     Either we are relocating all the way, or we don't want to apply
  569.     the relocation to the reloc entry (probably because there isn't
  570.     any room in the output format to describe addends to relocs)
  571.     */
  572.   relocation >>= howto->rightshift;
  573.  
  574.   /* Shift everything up to where it's going to be used */
  575.    
  576.   relocation <<= howto->bitpos;
  577.  
  578.   /* Wait for the day when all have the mask in them */
  579.  
  580.   /* What we do:
  581.      i instruction to be left alone
  582.      o offset within instruction
  583.      r relocation offset to apply
  584.      S src mask
  585.      D dst mask
  586.      N ~dst mask
  587.      A part 1
  588.      B part 2
  589.      R result
  590.      
  591.      Do this:
  592.      i i i i i o o o o o        from bfd_get<size>
  593.      and           S S S S S    to get the size offset we want
  594.      +   r r r r r r r r r r  to get the final value to place
  595.      and           D D D D D  to chop to right size
  596.      -----------------------
  597.      A A A A A 
  598.      And this:
  599.      ...   i i i i i o o o o o  from bfd_get<size>
  600.      and   N N N N N            get instruction
  601.      -----------------------
  602.      ...   B B B B B
  603.      
  604.      And then:       
  605.      B B B B B       
  606.      or              A A A A A     
  607.      -----------------------
  608.      R R R R R R R R R R        put into bfd_put<size>
  609.      */
  610.  
  611. #define DOIT(x) \
  612.   x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) +  relocation) & howto->dst_mask))
  613.  
  614.    switch (howto->size)
  615.      {
  616.      case 0:
  617.        {
  618.      char x = bfd_get_8(abfd, (char *)data + addr);
  619.      DOIT(x);
  620.      bfd_put_8(abfd,x, (unsigned char *) data + addr);
  621.        }
  622.        break;
  623.  
  624.      case 1:
  625.        if (relocation)
  626.      {
  627.        short x = bfd_get_16(abfd, (bfd_byte *)data + addr);
  628.        DOIT(x);
  629.        bfd_put_16(abfd, x,   (unsigned char *)data + addr);
  630.      }
  631.        break;
  632.      case 2:
  633.        if (relocation)
  634.      {
  635.        long  x = bfd_get_32 (abfd, (bfd_byte *) data + addr);
  636.        DOIT (x);
  637.        bfd_put_32 (abfd, x, (bfd_byte *)data + addr);
  638.      }
  639.        break;
  640.      case -2:
  641.        {
  642.      long  x = bfd_get_32(abfd, (bfd_byte *) data + addr);
  643.      relocation = -relocation;
  644.      DOIT(x);
  645.      bfd_put_32(abfd,x,    (bfd_byte *)data + addr);
  646.        }
  647.        break;
  648.  
  649.      case 3:
  650.  
  651.        /* Do nothing */
  652.        break;
  653.      default:
  654.        return bfd_reloc_other;
  655.      }
  656.  
  657.   return flag;
  658. }
  659.  
  660.  
  661.  
  662. /*
  663. DOCDD
  664. INODE
  665.     howto manager,  , typedef arelent, Relocations
  666.  
  667. SECTION
  668.     The howto manager 
  669.  
  670.     When an application wants to create a relocation, but doesn't
  671.     know what the target machine might call it, it can find out by
  672.     using this bit of code.
  673.  
  674. */
  675.  
  676. /*
  677. TYPEDEF
  678.     bfd_reloc_code_type
  679.  
  680. DESCRIPTION
  681.     The insides of a reloc code
  682.  
  683. CODE_FRAGMENT
  684. .
  685. .typedef enum bfd_reloc_code_real 
  686. .
  687. .{
  688. .       {* 16 bits wide, simple reloc *}
  689. .  BFD_RELOC_16,        
  690. .
  691. .       {* 8 bits wide, but used to form an address like 0xffnn *}
  692. .  BFD_RELOC_8_FFnn,
  693. .
  694. .       {* 8 bits wide, simple *}
  695. .  BFD_RELOC_8,
  696. .
  697. .       {* 8 bits wide, pc relative *}
  698. .  BFD_RELOC_8_PCREL,
  699. .
  700. .       {* The type of reloc used to build a contructor table - at the
  701. .          moment probably a 32 bit wide abs address, but the cpu can
  702. .          choose. *}
  703. .
  704. .  BFD_RELOC_CTOR,
  705. .
  706. .       {* 32 bits wide, simple reloc *}
  707. .  BFD_RELOC_32,
  708. .    {* 32 bits, PC-relative *}
  709. .  BFD_RELOC_32_PCREL,
  710. .
  711. .    {* High 22 bits of 32-bit value; simple reloc.  *}
  712. .  BFD_RELOC_HI22,
  713. .    {* Low 10 bits.  *}
  714. .  BFD_RELOC_LO10,
  715. .
  716. .    {* Reloc types used for i960/b.out.  *}
  717. .  BFD_RELOC_24_PCREL,
  718. .  BFD_RELOC_I960_CALLJ,
  719. .
  720. .  BFD_RELOC_16_PCREL,
  721. .    {* 32-bit pc-relative, shifted right 2 bits (i.e., 30-bit
  722. .       word displacement, e.g. for SPARC) *}
  723. .  BFD_RELOC_32_PCREL_S2,
  724. .
  725. .  {* now for the sparc/elf codes *}
  726. .  BFD_RELOC_NONE,        {* actually used *}
  727. .  BFD_RELOC_SPARC_WDISP22,
  728. .  BFD_RELOC_SPARC22,
  729. .  BFD_RELOC_SPARC13,
  730. .  BFD_RELOC_SPARC_BASE13,
  731. .  BFD_RELOC_SPARC_GOT10,
  732. .  BFD_RELOC_SPARC_GOT13,
  733. .  BFD_RELOC_SPARC_GOT22,
  734. .  BFD_RELOC_SPARC_PC10,
  735. .  BFD_RELOC_SPARC_PC22,
  736. .  BFD_RELOC_SPARC_WPLT30,
  737. .  BFD_RELOC_SPARC_COPY,
  738. .  BFD_RELOC_SPARC_GLOB_DAT,
  739. .  BFD_RELOC_SPARC_JMP_SLOT,
  740. .  BFD_RELOC_SPARC_RELATIVE,
  741. .  BFD_RELOC_SPARC_UA32,
  742. .
  743. .  {* this one is a.out specific? *}
  744. .  BFD_RELOC_SPARC_BASE22,
  745. .
  746. .       {* Bits 27..2 of the relocation address shifted right 2 bits;
  747. .         simple reloc otherwise.  *}
  748. .  BFD_RELOC_MIPS_JMP,
  749. .
  750. .       {* signed 16-bit pc-relative, shifted right 2 bits (e.g. for MIPS) *}
  751. .  BFD_RELOC_16_PCREL_S2,
  752. .
  753. .       {* High 16 bits of 32-bit value; simple reloc.  *}
  754. .  BFD_RELOC_HI16,
  755. .       {* High 16 bits of 32-bit value but the low 16 bits will be sign
  756. .          extended and added to form the final result.  If the low 16
  757. .          bits form a negative number, we need to add one to the high value
  758. .          to compensate for the borrow when the low bits are added.  *}
  759. .  BFD_RELOC_HI16_S,
  760. .       {* Low 16 bits.  *}
  761. .  BFD_RELOC_LO16,
  762. .
  763. .    {* 16 bit relocation relative to the global pointer.  *}
  764. .  BFD_RELOC_MIPS_GPREL,
  765. .
  766. .       {* These are, so far, specific to HPPA processors.  I'm not sure that
  767. .       some don't duplicate other reloc types, such as BFD_RELOC_32 and
  768. .       _32_PCREL.  Also, many more were in the list I got that don't
  769. .       fit in well in the model BFD uses, so I've omitted them for now.
  770. .       If we do make this reloc type get used for code that really does
  771. .       implement the funky reloc types, they'll have to be added to this
  772. .       list.   *}
  773. .  BFD_RELOC_HPPA_32,
  774. .  BFD_RELOC_HPPA_11,
  775. .  BFD_RELOC_HPPA_14,
  776. .  BFD_RELOC_HPPA_17,
  777. .  BFD_RELOC_HPPA_L21,
  778. .  BFD_RELOC_HPPA_R11,
  779. .  BFD_RELOC_HPPA_R14,
  780. .  BFD_RELOC_HPPA_R17,
  781. .  BFD_RELOC_HPPA_LS21,
  782. .  BFD_RELOC_HPPA_RS11,
  783. .  BFD_RELOC_HPPA_RS14,
  784. .  BFD_RELOC_HPPA_RS17,
  785. .  BFD_RELOC_HPPA_LD21,
  786. .  BFD_RELOC_HPPA_RD11,
  787. .  BFD_RELOC_HPPA_RD14,
  788. .  BFD_RELOC_HPPA_RD17,
  789. .  BFD_RELOC_HPPA_LR21,
  790. .  BFD_RELOC_HPPA_RR14,
  791. .  BFD_RELOC_HPPA_RR17,
  792. .  BFD_RELOC_HPPA_GOTOFF_11,
  793. .  BFD_RELOC_HPPA_GOTOFF_14,
  794. .  BFD_RELOC_HPPA_GOTOFF_L21,
  795. .  BFD_RELOC_HPPA_GOTOFF_R11,
  796. .  BFD_RELOC_HPPA_GOTOFF_R14,
  797. .  BFD_RELOC_HPPA_GOTOFF_LS21,
  798. .  BFD_RELOC_HPPA_GOTOFF_RS11,
  799. .  BFD_RELOC_HPPA_GOTOFF_RS14,
  800. .  BFD_RELOC_HPPA_GOTOFF_LD21,
  801. .  BFD_RELOC_HPPA_GOTOFF_RD11,
  802. .  BFD_RELOC_HPPA_GOTOFF_RD14,
  803. .  BFD_RELOC_HPPA_GOTOFF_LR21,
  804. .  BFD_RELOC_HPPA_GOTOFF_RR14,
  805. .  BFD_RELOC_HPPA_DLT_32,
  806. .  BFD_RELOC_HPPA_DLT_11,
  807. .  BFD_RELOC_HPPA_DLT_14,
  808. .  BFD_RELOC_HPPA_DLT_L21,
  809. .  BFD_RELOC_HPPA_DLT_R11,
  810. .  BFD_RELOC_HPPA_DLT_R14,
  811. .  BFD_RELOC_HPPA_ABS_CALL_11,
  812. .  BFD_RELOC_HPPA_ABS_CALL_14,
  813. .  BFD_RELOC_HPPA_ABS_CALL_17,
  814. .  BFD_RELOC_HPPA_ABS_CALL_L21,
  815. .  BFD_RELOC_HPPA_ABS_CALL_R11,
  816. .  BFD_RELOC_HPPA_ABS_CALL_R14,
  817. .  BFD_RELOC_HPPA_ABS_CALL_R17,
  818. .  BFD_RELOC_HPPA_ABS_CALL_LS21,
  819. .  BFD_RELOC_HPPA_ABS_CALL_RS11,
  820. .  BFD_RELOC_HPPA_ABS_CALL_RS14,
  821. .  BFD_RELOC_HPPA_ABS_CALL_RS17,
  822. .  BFD_RELOC_HPPA_ABS_CALL_LD21,
  823. .  BFD_RELOC_HPPA_ABS_CALL_RD11,
  824. .  BFD_RELOC_HPPA_ABS_CALL_RD14,
  825. .  BFD_RELOC_HPPA_ABS_CALL_RD17,
  826. .  BFD_RELOC_HPPA_ABS_CALL_LR21,
  827. .  BFD_RELOC_HPPA_ABS_CALL_RR14,
  828. .  BFD_RELOC_HPPA_ABS_CALL_RR17,
  829. .  BFD_RELOC_HPPA_PCREL_CALL_11,
  830. .  BFD_RELOC_HPPA_PCREL_CALL_14,
  831. .  BFD_RELOC_HPPA_PCREL_CALL_17,
  832. .  BFD_RELOC_HPPA_PCREL_CALL_L21,
  833. .  BFD_RELOC_HPPA_PCREL_CALL_R11,
  834. .  BFD_RELOC_HPPA_PCREL_CALL_R14,
  835. .  BFD_RELOC_HPPA_PCREL_CALL_R17,
  836. .  BFD_RELOC_HPPA_PCREL_CALL_LS21,
  837. .  BFD_RELOC_HPPA_PCREL_CALL_RS11,
  838. .  BFD_RELOC_HPPA_PCREL_CALL_RS14,
  839. .  BFD_RELOC_HPPA_PCREL_CALL_RS17,
  840. .  BFD_RELOC_HPPA_PCREL_CALL_LD21,
  841. .  BFD_RELOC_HPPA_PCREL_CALL_RD11,
  842. .  BFD_RELOC_HPPA_PCREL_CALL_RD14,
  843. .  BFD_RELOC_HPPA_PCREL_CALL_RD17,
  844. .  BFD_RELOC_HPPA_PCREL_CALL_LR21,
  845. .  BFD_RELOC_HPPA_PCREL_CALL_RR14,
  846. .  BFD_RELOC_HPPA_PCREL_CALL_RR17,
  847. .  BFD_RELOC_HPPA_PLABEL_32,
  848. .  BFD_RELOC_HPPA_PLABEL_11,
  849. .  BFD_RELOC_HPPA_PLABEL_14,
  850. .  BFD_RELOC_HPPA_PLABEL_L21,
  851. .  BFD_RELOC_HPPA_PLABEL_R11,
  852. .  BFD_RELOC_HPPA_PLABEL_R14,
  853. .  BFD_RELOC_HPPA_UNWIND_ENTRY,
  854. .  BFD_RELOC_HPPA_UNWIND_ENTRIES,
  855. .
  856. .  {* this must be the highest numeric value *}
  857. .  BFD_RELOC_UNUSED
  858. . } bfd_reloc_code_real_type;
  859. */
  860.  
  861.  
  862.  
  863. /*
  864. SECTION
  865.     bfd_reloc_type_lookup
  866.  
  867. SYNOPSIS
  868.     CONST struct reloc_howto_struct *
  869.     bfd_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code);
  870.  
  871. DESCRIPTION
  872.     This routine returns a pointer to a howto struct which when
  873.     invoked, will perform the supplied relocation on data from the
  874.     architecture noted.
  875.  
  876. */
  877.  
  878.  
  879. CONST struct reloc_howto_struct *
  880. DEFUN(bfd_reloc_type_lookup,(abfd, code),
  881.       bfd *abfd AND
  882.       bfd_reloc_code_real_type code)
  883. {
  884.   return BFD_SEND (abfd, reloc_type_lookup, (abfd, code));
  885. }
  886.  
  887. static reloc_howto_type bfd_howto_32 =
  888.  HOWTO(0, 00,2,32,false,0,false,true,0,"VRT32", false,0xffffffff,0xffffffff,true);
  889.  
  890.  
  891. /*
  892. INTERNAL_FUNCTION
  893.     bfd_default_reloc_type_lookup
  894.  
  895. SYNOPSIS
  896.     CONST struct reloc_howto_struct *bfd_default_reloc_type_lookup
  897.     (bfd *abfd AND
  898.          bfd_reloc_code_real_type  code);
  899.  
  900. DESCRIPTION
  901.     Provides a default relocation lookup routine for any architecture.
  902.  
  903.  
  904. */
  905.  
  906. CONST struct reloc_howto_struct *
  907. DEFUN(bfd_default_reloc_type_lookup, (abfd, code),
  908.       bfd *abfd AND
  909.       bfd_reloc_code_real_type code)
  910. {
  911.   switch (code) 
  912.     {
  913.     case BFD_RELOC_CTOR:
  914.       /* The type of reloc used in a ctor, which will be as wide as the
  915.      address - so either a 64, 32, or 16 bitter.. */
  916.       switch (bfd_get_arch_info (abfd)->bits_per_address) {
  917.       case 64:
  918.     BFD_FAIL();
  919.       case 32:
  920.     return &bfd_howto_32;
  921.       case 16:
  922.     BFD_FAIL();
  923.       default:
  924.     BFD_FAIL();
  925.       }
  926.     default:
  927.       BFD_FAIL();
  928.     }
  929.   return (CONST struct reloc_howto_struct *)NULL;
  930. }
  931.  
  932.  
  933. /*
  934. INTERNAL_FUNCTION
  935.     bfd_generic_relax_section
  936.  
  937. SYNOPSIS
  938.     boolean bfd_generic_relax_section
  939.      (bfd *abfd,
  940.       asection *section,
  941.       asymbol **symbols);
  942.  
  943. DESCRIPTION
  944.     Provides default handling for relaxing for back ends which
  945.     don't do relaxing -- i.e., does nothing.
  946. */
  947.  
  948. boolean
  949. DEFUN(bfd_generic_relax_section,(abfd, section, symbols),
  950.       bfd *abfd AND
  951.       asection *section AND
  952.       asymbol **symbols)
  953. {
  954.   
  955.   return false;
  956.   
  957. }
  958.  
  959.         
  960. /*
  961. INTERNAL_FUNCTION
  962.     bfd_generic_get_relocated_section_contents
  963.  
  964. SYNOPSIS
  965.     bfd_byte *
  966.        bfd_generic_get_relocated_section_contents (bfd *abfd,
  967.          struct bfd_seclet *seclet,
  968.          bfd_byte *data,
  969.          boolean relocateable);
  970.  
  971. DESCRIPTION
  972.     Provides default handling of relocation effort for back ends
  973.     which can't be bothered to do it efficiently.
  974.  
  975. */
  976.  
  977. bfd_byte *
  978. DEFUN(bfd_generic_get_relocated_section_contents,(abfd,
  979.                           seclet,
  980.                           data,
  981.                           relocateable),
  982.       bfd *abfd AND
  983.       struct bfd_seclet *seclet AND
  984.       bfd_byte *data AND
  985.       boolean relocateable)
  986. {
  987.   extern bfd_error_vector_type bfd_error_vector;
  988.  
  989.   /* Get enough memory to hold the stuff */
  990.   bfd *input_bfd = seclet->u.indirect.section->owner;
  991.   asection *input_section = seclet->u.indirect.section;
  992.  
  993.  
  994.  
  995.   bfd_size_type reloc_size = bfd_get_reloc_upper_bound(input_bfd,
  996.                                input_section);
  997.   arelent **reloc_vector = (arelent **) alloca(reloc_size);
  998.   
  999.   /* read in the section */
  1000.   bfd_get_section_contents(input_bfd,
  1001.                input_section,
  1002.                data,
  1003.                0,
  1004.                input_section->_raw_size);
  1005.   
  1006. /* We're not relaxing the section, so just copy the size info */
  1007.   input_section->_cooked_size = input_section->_raw_size;
  1008.   input_section->reloc_done = true;
  1009.   
  1010.  
  1011.   if (bfd_canonicalize_reloc(input_bfd, 
  1012.                  input_section,
  1013.                  reloc_vector,
  1014.                  seclet->u.indirect.symbols) )
  1015.   {
  1016.     arelent **parent;
  1017.     for (parent = reloc_vector;  * parent != (arelent *)NULL;
  1018.      parent++) 
  1019.     { 
  1020.       bfd_reloc_status_type r=
  1021.        bfd_perform_relocation(input_bfd,
  1022.                   *parent,
  1023.                   data,
  1024.                   input_section,
  1025.                   relocateable ? abfd : (bfd *) NULL);
  1026.       
  1027.       if (relocateable)
  1028.     {
  1029.       asection *os = input_section->output_section;
  1030.  
  1031.       /* A partial link, so keep the relocs */
  1032.       os->orelocation[os->reloc_count] = *parent;
  1033.       os->reloc_count++;
  1034.     }
  1035.  
  1036.       if (r != bfd_reloc_ok) 
  1037.       {
  1038.     switch (r)
  1039.     {
  1040.     case bfd_reloc_undefined:
  1041.       bfd_error_vector.undefined_symbol(*parent, seclet);
  1042.       break;
  1043.     case bfd_reloc_dangerous: 
  1044.       bfd_error_vector.reloc_dangerous(*parent, seclet);
  1045.       break;
  1046.     case bfd_reloc_outofrange:
  1047.     case bfd_reloc_overflow:
  1048.       bfd_error_vector.reloc_value_truncated(*parent, seclet);
  1049.       break;
  1050.     default:
  1051.       abort();
  1052.       break;
  1053.     }
  1054.  
  1055.       }
  1056.     }    
  1057.   }
  1058.  
  1059.  
  1060.   return data;
  1061.  
  1062.   
  1063. }
  1064.