1 |
24 |
jeremybenn |
/* BFD back-end for Intel 386 COFF files.
|
2 |
|
|
Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
|
3 |
225 |
jeremybenn |
2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008
|
4 |
24 |
jeremybenn |
Free Software Foundation, Inc.
|
5 |
|
|
Written by Cygnus Support.
|
6 |
|
|
|
7 |
|
|
This file is part of BFD, the Binary File Descriptor library.
|
8 |
|
|
|
9 |
|
|
This program is free software; you can redistribute it and/or modify
|
10 |
|
|
it under the terms of the GNU General Public License as published by
|
11 |
|
|
the Free Software Foundation; either version 3 of the License, or
|
12 |
|
|
(at your option) any later version.
|
13 |
|
|
|
14 |
|
|
This program is distributed in the hope that it will be useful,
|
15 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17 |
|
|
GNU General Public License for more details.
|
18 |
|
|
|
19 |
|
|
You should have received a copy of the GNU General Public License
|
20 |
|
|
along with this program; if not, write to the Free Software
|
21 |
|
|
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
|
22 |
|
|
MA 02110-1301, USA. */
|
23 |
|
|
|
24 |
|
|
#include "sysdep.h"
|
25 |
|
|
#include "bfd.h"
|
26 |
|
|
#include "libbfd.h"
|
27 |
|
|
|
28 |
|
|
#include "coff/i386.h"
|
29 |
|
|
|
30 |
|
|
#include "coff/internal.h"
|
31 |
|
|
|
32 |
|
|
#ifdef COFF_WITH_PE
|
33 |
|
|
#include "coff/pe.h"
|
34 |
|
|
#endif
|
35 |
|
|
|
36 |
|
|
#ifdef COFF_GO32_EXE
|
37 |
|
|
#include "coff/go32exe.h"
|
38 |
|
|
#endif
|
39 |
|
|
|
40 |
225 |
jeremybenn |
#ifndef bfd_pe_print_pdata
|
41 |
|
|
#define bfd_pe_print_pdata NULL
|
42 |
|
|
#endif
|
43 |
|
|
|
44 |
24 |
jeremybenn |
#include "libcoff.h"
|
45 |
|
|
|
46 |
|
|
static bfd_reloc_status_type coff_i386_reloc
|
47 |
|
|
PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
|
48 |
|
|
static reloc_howto_type *coff_i386_rtype_to_howto
|
49 |
|
|
PARAMS ((bfd *, asection *, struct internal_reloc *,
|
50 |
|
|
struct coff_link_hash_entry *, struct internal_syment *,
|
51 |
|
|
bfd_vma *));
|
52 |
|
|
static reloc_howto_type *coff_i386_reloc_type_lookup
|
53 |
|
|
PARAMS ((bfd *, bfd_reloc_code_real_type));
|
54 |
|
|
|
55 |
|
|
#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
|
56 |
|
|
/* The page size is a guess based on ELF. */
|
57 |
|
|
|
58 |
|
|
#define COFF_PAGE_SIZE 0x1000
|
59 |
|
|
|
60 |
|
|
/* For some reason when using i386 COFF the value stored in the .text
|
61 |
|
|
section for a reference to a common symbol is the value itself plus
|
62 |
|
|
any desired offset. Ian Taylor, Cygnus Support. */
|
63 |
|
|
|
64 |
|
|
/* If we are producing relocatable output, we need to do some
|
65 |
|
|
adjustments to the object file that are not done by the
|
66 |
|
|
bfd_perform_relocation function. This function is called by every
|
67 |
|
|
reloc type to make any required adjustments. */
|
68 |
|
|
|
69 |
|
|
static bfd_reloc_status_type
|
70 |
|
|
coff_i386_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd,
|
71 |
|
|
error_message)
|
72 |
|
|
bfd *abfd;
|
73 |
|
|
arelent *reloc_entry;
|
74 |
|
|
asymbol *symbol;
|
75 |
|
|
PTR data;
|
76 |
|
|
asection *input_section ATTRIBUTE_UNUSED;
|
77 |
|
|
bfd *output_bfd;
|
78 |
|
|
char **error_message ATTRIBUTE_UNUSED;
|
79 |
|
|
{
|
80 |
|
|
symvalue diff;
|
81 |
|
|
|
82 |
|
|
#ifndef COFF_WITH_PE
|
83 |
|
|
if (output_bfd == (bfd *) NULL)
|
84 |
|
|
return bfd_reloc_continue;
|
85 |
|
|
#endif
|
86 |
|
|
|
87 |
|
|
if (bfd_is_com_section (symbol->section))
|
88 |
|
|
{
|
89 |
|
|
#ifndef COFF_WITH_PE
|
90 |
|
|
/* We are relocating a common symbol. The current value in the
|
91 |
|
|
object file is ORIG + OFFSET, where ORIG is the value of the
|
92 |
|
|
common symbol as seen by the object file when it was compiled
|
93 |
|
|
(this may be zero if the symbol was undefined) and OFFSET is
|
94 |
|
|
the offset into the common symbol (normally zero, but may be
|
95 |
|
|
non-zero when referring to a field in a common structure).
|
96 |
|
|
ORIG is the negative of reloc_entry->addend, which is set by
|
97 |
|
|
the CALC_ADDEND macro below. We want to replace the value in
|
98 |
|
|
the object file with NEW + OFFSET, where NEW is the value of
|
99 |
|
|
the common symbol which we are going to put in the final
|
100 |
|
|
object file. NEW is symbol->value. */
|
101 |
|
|
diff = symbol->value + reloc_entry->addend;
|
102 |
|
|
#else
|
103 |
|
|
/* In PE mode, we do not offset the common symbol. */
|
104 |
|
|
diff = reloc_entry->addend;
|
105 |
|
|
#endif
|
106 |
|
|
}
|
107 |
|
|
else
|
108 |
|
|
{
|
109 |
|
|
/* For some reason bfd_perform_relocation always effectively
|
110 |
|
|
ignores the addend for a COFF target when producing
|
111 |
|
|
relocatable output. This seems to be always wrong for 386
|
112 |
|
|
COFF, so we handle the addend here instead. */
|
113 |
|
|
#ifdef COFF_WITH_PE
|
114 |
|
|
if (output_bfd == (bfd *) NULL)
|
115 |
|
|
{
|
116 |
|
|
reloc_howto_type *howto = reloc_entry->howto;
|
117 |
|
|
|
118 |
|
|
/* Although PC relative relocations are very similar between
|
119 |
|
|
PE and non-PE formats, but they are off by 1 << howto->size
|
120 |
|
|
bytes. For the external relocation, PE is very different
|
121 |
|
|
from others. See md_apply_fix3 () in gas/config/tc-i386.c.
|
122 |
|
|
When we link PE and non-PE object files together to
|
123 |
|
|
generate a non-PE executable, we have to compensate it
|
124 |
|
|
here. */
|
125 |
|
|
if (howto->pc_relative && howto->pcrel_offset)
|
126 |
|
|
diff = -(1 << howto->size);
|
127 |
|
|
else if (symbol->flags & BSF_WEAK)
|
128 |
|
|
diff = reloc_entry->addend - symbol->value;
|
129 |
|
|
else
|
130 |
|
|
diff = -reloc_entry->addend;
|
131 |
|
|
}
|
132 |
|
|
else
|
133 |
|
|
#endif
|
134 |
|
|
diff = reloc_entry->addend;
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
#ifdef COFF_WITH_PE
|
138 |
|
|
/* FIXME: How should this case be handled? */
|
139 |
|
|
if (reloc_entry->howto->type == R_IMAGEBASE
|
140 |
|
|
&& output_bfd != NULL
|
141 |
|
|
&& bfd_get_flavour(output_bfd) == bfd_target_coff_flavour)
|
142 |
|
|
diff -= pe_data (output_bfd)->pe_opthdr.ImageBase;
|
143 |
|
|
#endif
|
144 |
|
|
|
145 |
|
|
#define DOIT(x) \
|
146 |
|
|
x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
|
147 |
|
|
|
148 |
|
|
if (diff != 0)
|
149 |
|
|
{
|
150 |
|
|
reloc_howto_type *howto = reloc_entry->howto;
|
151 |
|
|
unsigned char *addr = (unsigned char *) data + reloc_entry->address;
|
152 |
|
|
|
153 |
|
|
switch (howto->size)
|
154 |
|
|
{
|
155 |
|
|
case 0:
|
156 |
|
|
{
|
157 |
|
|
char x = bfd_get_8 (abfd, addr);
|
158 |
|
|
DOIT (x);
|
159 |
|
|
bfd_put_8 (abfd, x, addr);
|
160 |
|
|
}
|
161 |
|
|
break;
|
162 |
|
|
|
163 |
|
|
case 1:
|
164 |
|
|
{
|
165 |
|
|
short x = bfd_get_16 (abfd, addr);
|
166 |
|
|
DOIT (x);
|
167 |
|
|
bfd_put_16 (abfd, (bfd_vma) x, addr);
|
168 |
|
|
}
|
169 |
|
|
break;
|
170 |
|
|
|
171 |
|
|
case 2:
|
172 |
|
|
{
|
173 |
|
|
long x = bfd_get_32 (abfd, addr);
|
174 |
|
|
DOIT (x);
|
175 |
|
|
bfd_put_32 (abfd, (bfd_vma) x, addr);
|
176 |
|
|
}
|
177 |
|
|
break;
|
178 |
|
|
|
179 |
|
|
default:
|
180 |
|
|
abort ();
|
181 |
|
|
}
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
/* Now let bfd_perform_relocation finish everything up. */
|
185 |
|
|
return bfd_reloc_continue;
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
#ifdef COFF_WITH_PE
|
189 |
|
|
/* Return TRUE if this relocation should appear in the output .reloc
|
190 |
|
|
section. */
|
191 |
|
|
|
192 |
|
|
static bfd_boolean in_reloc_p PARAMS ((bfd *, reloc_howto_type *));
|
193 |
|
|
|
194 |
|
|
static bfd_boolean in_reloc_p (abfd, howto)
|
195 |
|
|
bfd * abfd ATTRIBUTE_UNUSED;
|
196 |
|
|
reloc_howto_type *howto;
|
197 |
|
|
{
|
198 |
|
|
return ! howto->pc_relative && howto->type != R_IMAGEBASE;
|
199 |
|
|
}
|
200 |
|
|
#endif /* COFF_WITH_PE */
|
201 |
|
|
|
202 |
|
|
#ifndef PCRELOFFSET
|
203 |
|
|
#define PCRELOFFSET FALSE
|
204 |
|
|
#endif
|
205 |
|
|
|
206 |
|
|
static reloc_howto_type howto_table[] =
|
207 |
|
|
{
|
208 |
|
|
EMPTY_HOWTO (0),
|
209 |
|
|
EMPTY_HOWTO (1),
|
210 |
|
|
EMPTY_HOWTO (2),
|
211 |
|
|
EMPTY_HOWTO (3),
|
212 |
|
|
EMPTY_HOWTO (4),
|
213 |
|
|
EMPTY_HOWTO (5),
|
214 |
|
|
HOWTO (R_DIR32, /* type */
|
215 |
|
|
0, /* rightshift */
|
216 |
|
|
2, /* size (0 = byte, 1 = short, 2 = long) */
|
217 |
|
|
32, /* bitsize */
|
218 |
|
|
FALSE, /* pc_relative */
|
219 |
|
|
0, /* bitpos */
|
220 |
|
|
complain_overflow_bitfield, /* complain_on_overflow */
|
221 |
|
|
coff_i386_reloc, /* special_function */
|
222 |
|
|
"dir32", /* name */
|
223 |
|
|
TRUE, /* partial_inplace */
|
224 |
|
|
0xffffffff, /* src_mask */
|
225 |
|
|
0xffffffff, /* dst_mask */
|
226 |
|
|
TRUE), /* pcrel_offset */
|
227 |
|
|
/* PE IMAGE_REL_I386_DIR32NB relocation (7). */
|
228 |
|
|
HOWTO (R_IMAGEBASE, /* type */
|
229 |
|
|
0, /* rightshift */
|
230 |
|
|
2, /* size (0 = byte, 1 = short, 2 = long) */
|
231 |
|
|
32, /* bitsize */
|
232 |
|
|
FALSE, /* pc_relative */
|
233 |
|
|
0, /* bitpos */
|
234 |
|
|
complain_overflow_bitfield, /* complain_on_overflow */
|
235 |
|
|
coff_i386_reloc, /* special_function */
|
236 |
|
|
"rva32", /* name */
|
237 |
|
|
TRUE, /* partial_inplace */
|
238 |
|
|
0xffffffff, /* src_mask */
|
239 |
|
|
0xffffffff, /* dst_mask */
|
240 |
|
|
FALSE), /* pcrel_offset */
|
241 |
|
|
EMPTY_HOWTO (010),
|
242 |
|
|
EMPTY_HOWTO (011),
|
243 |
|
|
EMPTY_HOWTO (012),
|
244 |
|
|
#ifdef COFF_WITH_PE
|
245 |
|
|
/* 32-bit longword section relative relocation (013). */
|
246 |
|
|
HOWTO (R_SECREL32, /* type */
|
247 |
|
|
0, /* rightshift */
|
248 |
|
|
2, /* size (0 = byte, 1 = short, 2 = long) */
|
249 |
|
|
32, /* bitsize */
|
250 |
|
|
FALSE, /* pc_relative */
|
251 |
|
|
0, /* bitpos */
|
252 |
|
|
complain_overflow_bitfield, /* complain_on_overflow */
|
253 |
|
|
coff_i386_reloc, /* special_function */
|
254 |
|
|
"secrel32", /* name */
|
255 |
|
|
TRUE, /* partial_inplace */
|
256 |
|
|
0xffffffff, /* src_mask */
|
257 |
|
|
0xffffffff, /* dst_mask */
|
258 |
|
|
TRUE), /* pcrel_offset */
|
259 |
|
|
#else
|
260 |
|
|
EMPTY_HOWTO (013),
|
261 |
|
|
#endif
|
262 |
|
|
EMPTY_HOWTO (014),
|
263 |
|
|
EMPTY_HOWTO (015),
|
264 |
|
|
EMPTY_HOWTO (016),
|
265 |
|
|
/* Byte relocation (017). */
|
266 |
|
|
HOWTO (R_RELBYTE, /* type */
|
267 |
|
|
0, /* rightshift */
|
268 |
|
|
0, /* size (0 = byte, 1 = short, 2 = long) */
|
269 |
|
|
8, /* bitsize */
|
270 |
|
|
FALSE, /* pc_relative */
|
271 |
|
|
0, /* bitpos */
|
272 |
|
|
complain_overflow_bitfield, /* complain_on_overflow */
|
273 |
|
|
coff_i386_reloc, /* special_function */
|
274 |
|
|
"8", /* name */
|
275 |
|
|
TRUE, /* partial_inplace */
|
276 |
|
|
0x000000ff, /* src_mask */
|
277 |
|
|
0x000000ff, /* dst_mask */
|
278 |
|
|
PCRELOFFSET), /* pcrel_offset */
|
279 |
|
|
/* 16-bit word relocation (020). */
|
280 |
|
|
HOWTO (R_RELWORD, /* type */
|
281 |
|
|
0, /* rightshift */
|
282 |
|
|
1, /* size (0 = byte, 1 = short, 2 = long) */
|
283 |
|
|
16, /* bitsize */
|
284 |
|
|
FALSE, /* pc_relative */
|
285 |
|
|
0, /* bitpos */
|
286 |
|
|
complain_overflow_bitfield, /* complain_on_overflow */
|
287 |
|
|
coff_i386_reloc, /* special_function */
|
288 |
|
|
"16", /* name */
|
289 |
|
|
TRUE, /* partial_inplace */
|
290 |
|
|
0x0000ffff, /* src_mask */
|
291 |
|
|
0x0000ffff, /* dst_mask */
|
292 |
|
|
PCRELOFFSET), /* pcrel_offset */
|
293 |
|
|
/* 32-bit longword relocation (021). */
|
294 |
|
|
HOWTO (R_RELLONG, /* type */
|
295 |
|
|
0, /* rightshift */
|
296 |
|
|
2, /* size (0 = byte, 1 = short, 2 = long) */
|
297 |
|
|
32, /* bitsize */
|
298 |
|
|
FALSE, /* pc_relative */
|
299 |
|
|
0, /* bitpos */
|
300 |
|
|
complain_overflow_bitfield, /* complain_on_overflow */
|
301 |
|
|
coff_i386_reloc, /* special_function */
|
302 |
|
|
"32", /* name */
|
303 |
|
|
TRUE, /* partial_inplace */
|
304 |
|
|
0xffffffff, /* src_mask */
|
305 |
|
|
0xffffffff, /* dst_mask */
|
306 |
|
|
PCRELOFFSET), /* pcrel_offset */
|
307 |
|
|
/* Byte PC relative relocation (022). */
|
308 |
|
|
HOWTO (R_PCRBYTE, /* type */
|
309 |
|
|
0, /* rightshift */
|
310 |
|
|
0, /* size (0 = byte, 1 = short, 2 = long) */
|
311 |
|
|
8, /* bitsize */
|
312 |
|
|
TRUE, /* pc_relative */
|
313 |
|
|
0, /* bitpos */
|
314 |
|
|
complain_overflow_signed, /* complain_on_overflow */
|
315 |
|
|
coff_i386_reloc, /* special_function */
|
316 |
|
|
"DISP8", /* name */
|
317 |
|
|
TRUE, /* partial_inplace */
|
318 |
|
|
0x000000ff, /* src_mask */
|
319 |
|
|
0x000000ff, /* dst_mask */
|
320 |
|
|
PCRELOFFSET), /* pcrel_offset */
|
321 |
|
|
/* 16-bit word PC relative relocation (023). */
|
322 |
|
|
HOWTO (R_PCRWORD, /* type */
|
323 |
|
|
0, /* rightshift */
|
324 |
|
|
1, /* size (0 = byte, 1 = short, 2 = long) */
|
325 |
|
|
16, /* bitsize */
|
326 |
|
|
TRUE, /* pc_relative */
|
327 |
|
|
0, /* bitpos */
|
328 |
|
|
complain_overflow_signed, /* complain_on_overflow */
|
329 |
|
|
coff_i386_reloc, /* special_function */
|
330 |
|
|
"DISP16", /* name */
|
331 |
|
|
TRUE, /* partial_inplace */
|
332 |
|
|
0x0000ffff, /* src_mask */
|
333 |
|
|
0x0000ffff, /* dst_mask */
|
334 |
|
|
PCRELOFFSET), /* pcrel_offset */
|
335 |
|
|
/* 32-bit longword PC relative relocation (024). */
|
336 |
|
|
HOWTO (R_PCRLONG, /* type */
|
337 |
|
|
0, /* rightshift */
|
338 |
|
|
2, /* size (0 = byte, 1 = short, 2 = long) */
|
339 |
|
|
32, /* bitsize */
|
340 |
|
|
TRUE, /* pc_relative */
|
341 |
|
|
0, /* bitpos */
|
342 |
|
|
complain_overflow_signed, /* complain_on_overflow */
|
343 |
|
|
coff_i386_reloc, /* special_function */
|
344 |
|
|
"DISP32", /* name */
|
345 |
|
|
TRUE, /* partial_inplace */
|
346 |
|
|
0xffffffff, /* src_mask */
|
347 |
|
|
0xffffffff, /* dst_mask */
|
348 |
|
|
PCRELOFFSET) /* pcrel_offset */
|
349 |
|
|
};
|
350 |
|
|
|
351 |
|
|
/* Turn a howto into a reloc nunmber */
|
352 |
|
|
|
353 |
|
|
#define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
|
354 |
|
|
#define BADMAG(x) I386BADMAG(x)
|
355 |
|
|
#define I386 1 /* Customize coffcode.h */
|
356 |
|
|
|
357 |
|
|
#define RTYPE2HOWTO(cache_ptr, dst) \
|
358 |
|
|
((cache_ptr)->howto = \
|
359 |
|
|
((dst)->r_type < sizeof (howto_table) / sizeof (howto_table[0]) \
|
360 |
|
|
? howto_table + (dst)->r_type \
|
361 |
|
|
: NULL))
|
362 |
|
|
|
363 |
|
|
/* For 386 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared
|
364 |
|
|
library. On some other COFF targets STYP_BSS is normally
|
365 |
|
|
STYP_NOLOAD. */
|
366 |
|
|
#define BSS_NOLOAD_IS_SHARED_LIBRARY
|
367 |
|
|
|
368 |
|
|
/* Compute the addend of a reloc. If the reloc is to a common symbol,
|
369 |
|
|
the object file contains the value of the common symbol. By the
|
370 |
|
|
time this is called, the linker may be using a different symbol
|
371 |
|
|
from a different object file with a different value. Therefore, we
|
372 |
|
|
hack wildly to locate the original symbol from this file so that we
|
373 |
|
|
can make the correct adjustment. This macro sets coffsym to the
|
374 |
|
|
symbol from the original file, and uses it to set the addend value
|
375 |
|
|
correctly. If this is not a common symbol, the usual addend
|
376 |
|
|
calculation is done, except that an additional tweak is needed for
|
377 |
|
|
PC relative relocs.
|
378 |
|
|
FIXME: This macro refers to symbols and asect; these are from the
|
379 |
|
|
calling function, not the macro arguments. */
|
380 |
|
|
|
381 |
|
|
#define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
|
382 |
|
|
{ \
|
383 |
|
|
coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
|
384 |
|
|
if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
|
385 |
|
|
coffsym = (obj_symbols (abfd) \
|
386 |
|
|
+ (cache_ptr->sym_ptr_ptr - symbols)); \
|
387 |
|
|
else if (ptr) \
|
388 |
|
|
coffsym = coff_symbol_from (abfd, ptr); \
|
389 |
|
|
if (coffsym != (coff_symbol_type *) NULL \
|
390 |
|
|
&& coffsym->native->u.syment.n_scnum == 0) \
|
391 |
|
|
cache_ptr->addend = - coffsym->native->u.syment.n_value; \
|
392 |
|
|
else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
|
393 |
|
|
&& ptr->section != (asection *) NULL) \
|
394 |
|
|
cache_ptr->addend = - (ptr->section->vma + ptr->value); \
|
395 |
|
|
else \
|
396 |
|
|
cache_ptr->addend = 0; \
|
397 |
|
|
if (ptr && howto_table[reloc.r_type].pc_relative) \
|
398 |
|
|
cache_ptr->addend += asect->vma; \
|
399 |
|
|
}
|
400 |
|
|
|
401 |
|
|
/* We use the special COFF backend linker. For normal i386 COFF, we
|
402 |
|
|
can use the generic relocate_section routine. For PE, we need our
|
403 |
|
|
own routine. */
|
404 |
|
|
|
405 |
|
|
#ifndef COFF_WITH_PE
|
406 |
|
|
|
407 |
|
|
#define coff_relocate_section _bfd_coff_generic_relocate_section
|
408 |
|
|
|
409 |
|
|
#else /* COFF_WITH_PE */
|
410 |
|
|
|
411 |
|
|
/* The PE relocate section routine. The only difference between this
|
412 |
|
|
and the regular routine is that we don't want to do anything for a
|
413 |
|
|
relocatable link. */
|
414 |
|
|
|
415 |
|
|
static bfd_boolean coff_pe_i386_relocate_section
|
416 |
|
|
PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
|
417 |
|
|
struct internal_reloc *, struct internal_syment *, asection **));
|
418 |
|
|
|
419 |
|
|
static bfd_boolean
|
420 |
|
|
coff_pe_i386_relocate_section (output_bfd, info, input_bfd,
|
421 |
|
|
input_section, contents, relocs, syms,
|
422 |
|
|
sections)
|
423 |
|
|
bfd *output_bfd;
|
424 |
|
|
struct bfd_link_info *info;
|
425 |
|
|
bfd *input_bfd;
|
426 |
|
|
asection *input_section;
|
427 |
|
|
bfd_byte *contents;
|
428 |
|
|
struct internal_reloc *relocs;
|
429 |
|
|
struct internal_syment *syms;
|
430 |
|
|
asection **sections;
|
431 |
|
|
{
|
432 |
|
|
if (info->relocatable)
|
433 |
|
|
return TRUE;
|
434 |
|
|
|
435 |
|
|
return _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
|
436 |
|
|
input_section, contents,
|
437 |
|
|
relocs, syms, sections);
|
438 |
|
|
}
|
439 |
|
|
|
440 |
|
|
#define coff_relocate_section coff_pe_i386_relocate_section
|
441 |
|
|
|
442 |
|
|
#endif /* COFF_WITH_PE */
|
443 |
|
|
|
444 |
|
|
/* Convert an rtype to howto for the COFF backend linker. */
|
445 |
|
|
|
446 |
|
|
static reloc_howto_type *
|
447 |
|
|
coff_i386_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
|
448 |
|
|
bfd *abfd ATTRIBUTE_UNUSED;
|
449 |
|
|
asection *sec;
|
450 |
|
|
struct internal_reloc *rel;
|
451 |
|
|
struct coff_link_hash_entry *h;
|
452 |
|
|
struct internal_syment *sym;
|
453 |
|
|
bfd_vma *addendp;
|
454 |
|
|
{
|
455 |
|
|
reloc_howto_type *howto;
|
456 |
|
|
|
457 |
|
|
if (rel->r_type >= sizeof (howto_table) / sizeof (howto_table[0]))
|
458 |
|
|
{
|
459 |
|
|
bfd_set_error (bfd_error_bad_value);
|
460 |
|
|
return NULL;
|
461 |
|
|
}
|
462 |
|
|
|
463 |
|
|
howto = howto_table + rel->r_type;
|
464 |
|
|
|
465 |
|
|
#ifdef COFF_WITH_PE
|
466 |
|
|
/* Cancel out code in _bfd_coff_generic_relocate_section. */
|
467 |
|
|
*addendp = 0;
|
468 |
|
|
#endif
|
469 |
|
|
|
470 |
|
|
if (howto->pc_relative)
|
471 |
|
|
*addendp += sec->vma;
|
472 |
|
|
|
473 |
|
|
if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
|
474 |
|
|
{
|
475 |
|
|
/* This is a common symbol. The section contents include the
|
476 |
|
|
size (sym->n_value) as an addend. The relocate_section
|
477 |
|
|
function will be adding in the final value of the symbol. We
|
478 |
|
|
need to subtract out the current size in order to get the
|
479 |
|
|
correct result. */
|
480 |
|
|
|
481 |
|
|
BFD_ASSERT (h != NULL);
|
482 |
|
|
|
483 |
|
|
#ifndef COFF_WITH_PE
|
484 |
|
|
/* I think we *do* want to bypass this. If we don't, I have
|
485 |
|
|
seen some data parameters get the wrong relocation address.
|
486 |
|
|
If I link two versions with and without this section bypassed
|
487 |
|
|
and then do a binary comparison, the addresses which are
|
488 |
|
|
different can be looked up in the map. The case in which
|
489 |
|
|
this section has been bypassed has addresses which correspond
|
490 |
|
|
to values I can find in the map. */
|
491 |
|
|
*addendp -= sym->n_value;
|
492 |
|
|
#endif
|
493 |
|
|
}
|
494 |
|
|
|
495 |
|
|
#ifndef COFF_WITH_PE
|
496 |
|
|
/* If the output symbol is common (in which case this must be a
|
497 |
|
|
relocatable link), we need to add in the final size of the
|
498 |
|
|
common symbol. */
|
499 |
|
|
if (h != NULL && h->root.type == bfd_link_hash_common)
|
500 |
|
|
*addendp += h->root.u.c.size;
|
501 |
|
|
#endif
|
502 |
|
|
|
503 |
|
|
#ifdef COFF_WITH_PE
|
504 |
|
|
if (howto->pc_relative)
|
505 |
|
|
{
|
506 |
|
|
*addendp -= 4;
|
507 |
|
|
|
508 |
|
|
/* If the symbol is defined, then the generic code is going to
|
509 |
|
|
add back the symbol value in order to cancel out an
|
510 |
|
|
adjustment it made to the addend. However, we set the addend
|
511 |
|
|
to 0 at the start of this function. We need to adjust here,
|
512 |
|
|
to avoid the adjustment the generic code will make. FIXME:
|
513 |
|
|
This is getting a bit hackish. */
|
514 |
|
|
if (sym != NULL && sym->n_scnum != 0)
|
515 |
|
|
*addendp -= sym->n_value;
|
516 |
|
|
}
|
517 |
|
|
|
518 |
|
|
if (rel->r_type == R_IMAGEBASE
|
519 |
|
|
&& (bfd_get_flavour(sec->output_section->owner)
|
520 |
|
|
== bfd_target_coff_flavour))
|
521 |
|
|
{
|
522 |
|
|
*addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
|
523 |
|
|
}
|
524 |
|
|
|
525 |
|
|
BFD_ASSERT (sym != NULL);
|
526 |
|
|
if (rel->r_type == R_SECREL32 && sym != NULL)
|
527 |
|
|
{
|
528 |
|
|
bfd_vma osect_vma;
|
529 |
|
|
|
530 |
|
|
if (h && (h->type == bfd_link_hash_defined
|
531 |
|
|
|| h->type == bfd_link_hash_defweak))
|
532 |
|
|
osect_vma = h->root.u.def.section->output_section->vma;
|
533 |
|
|
else
|
534 |
|
|
{
|
535 |
|
|
asection *sec;
|
536 |
|
|
int i;
|
537 |
|
|
|
538 |
|
|
/* Sigh, the only way to get the section to offset against
|
539 |
|
|
is to find it the hard way. */
|
540 |
|
|
|
541 |
|
|
for (sec = abfd->sections, i = 1; i < sym->n_scnum; i++)
|
542 |
|
|
sec = sec->next;
|
543 |
|
|
|
544 |
|
|
osect_vma = sec->output_section->vma;
|
545 |
|
|
}
|
546 |
|
|
|
547 |
|
|
*addendp -= osect_vma;
|
548 |
|
|
}
|
549 |
|
|
#endif
|
550 |
|
|
|
551 |
|
|
return howto;
|
552 |
|
|
}
|
553 |
|
|
|
554 |
|
|
#define coff_bfd_reloc_type_lookup coff_i386_reloc_type_lookup
|
555 |
|
|
#define coff_bfd_reloc_name_lookup coff_i386_reloc_name_lookup
|
556 |
|
|
|
557 |
|
|
static reloc_howto_type *
|
558 |
|
|
coff_i386_reloc_type_lookup (abfd, code)
|
559 |
|
|
bfd *abfd ATTRIBUTE_UNUSED;
|
560 |
|
|
bfd_reloc_code_real_type code;
|
561 |
|
|
{
|
562 |
|
|
switch (code)
|
563 |
|
|
{
|
564 |
|
|
case BFD_RELOC_RVA:
|
565 |
|
|
return howto_table + R_IMAGEBASE;
|
566 |
|
|
case BFD_RELOC_32:
|
567 |
|
|
return howto_table + R_DIR32;
|
568 |
|
|
case BFD_RELOC_32_PCREL:
|
569 |
|
|
return howto_table + R_PCRLONG;
|
570 |
|
|
case BFD_RELOC_16:
|
571 |
|
|
return howto_table + R_RELWORD;
|
572 |
|
|
case BFD_RELOC_16_PCREL:
|
573 |
|
|
return howto_table + R_PCRWORD;
|
574 |
|
|
case BFD_RELOC_8:
|
575 |
|
|
return howto_table + R_RELBYTE;
|
576 |
|
|
case BFD_RELOC_8_PCREL:
|
577 |
|
|
return howto_table + R_PCRBYTE;
|
578 |
|
|
#ifdef COFF_WITH_PE
|
579 |
|
|
case BFD_RELOC_32_SECREL:
|
580 |
|
|
return howto_table + R_SECREL32;
|
581 |
|
|
#endif
|
582 |
|
|
default:
|
583 |
|
|
BFD_FAIL ();
|
584 |
|
|
return 0;
|
585 |
|
|
}
|
586 |
|
|
}
|
587 |
|
|
|
588 |
|
|
static reloc_howto_type *
|
589 |
|
|
coff_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
|
590 |
|
|
const char *r_name)
|
591 |
|
|
{
|
592 |
|
|
unsigned int i;
|
593 |
|
|
|
594 |
|
|
for (i = 0; i < sizeof (howto_table) / sizeof (howto_table[0]); i++)
|
595 |
|
|
if (howto_table[i].name != NULL
|
596 |
|
|
&& strcasecmp (howto_table[i].name, r_name) == 0)
|
597 |
|
|
return &howto_table[i];
|
598 |
|
|
|
599 |
|
|
return NULL;
|
600 |
|
|
}
|
601 |
|
|
|
602 |
|
|
#define coff_rtype_to_howto coff_i386_rtype_to_howto
|
603 |
|
|
|
604 |
|
|
#ifdef TARGET_UNDERSCORE
|
605 |
|
|
|
606 |
|
|
/* If i386 gcc uses underscores for symbol names, then it does not use
|
607 |
|
|
a leading dot for local labels, so if TARGET_UNDERSCORE is defined
|
608 |
|
|
we treat all symbols starting with L as local. */
|
609 |
|
|
|
610 |
|
|
static bfd_boolean coff_i386_is_local_label_name
|
611 |
|
|
PARAMS ((bfd *, const char *));
|
612 |
|
|
|
613 |
|
|
static bfd_boolean
|
614 |
|
|
coff_i386_is_local_label_name (abfd, name)
|
615 |
|
|
bfd *abfd;
|
616 |
|
|
const char *name;
|
617 |
|
|
{
|
618 |
|
|
if (name[0] == 'L')
|
619 |
|
|
return TRUE;
|
620 |
|
|
|
621 |
|
|
return _bfd_coff_is_local_label_name (abfd, name);
|
622 |
|
|
}
|
623 |
|
|
|
624 |
|
|
#define coff_bfd_is_local_label_name coff_i386_is_local_label_name
|
625 |
|
|
|
626 |
|
|
#endif /* TARGET_UNDERSCORE */
|
627 |
|
|
|
628 |
|
|
#include "coffcode.h"
|
629 |
|
|
|
630 |
|
|
const bfd_target
|
631 |
|
|
#ifdef TARGET_SYM
|
632 |
|
|
TARGET_SYM =
|
633 |
|
|
#else
|
634 |
|
|
i386coff_vec =
|
635 |
|
|
#endif
|
636 |
|
|
{
|
637 |
|
|
#ifdef TARGET_NAME
|
638 |
|
|
TARGET_NAME,
|
639 |
|
|
#else
|
640 |
|
|
"coff-i386", /* name */
|
641 |
|
|
#endif
|
642 |
|
|
bfd_target_coff_flavour,
|
643 |
|
|
BFD_ENDIAN_LITTLE, /* data byte order is little */
|
644 |
|
|
BFD_ENDIAN_LITTLE, /* header byte order is little */
|
645 |
|
|
|
646 |
|
|
(HAS_RELOC | EXEC_P | /* object flags */
|
647 |
|
|
HAS_LINENO | HAS_DEBUG |
|
648 |
|
|
HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
|
649 |
|
|
|
650 |
|
|
(SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
|
651 |
|
|
#ifdef COFF_WITH_PE
|
652 |
|
|
| SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_READONLY
|
653 |
|
|
#endif
|
654 |
|
|
| SEC_CODE | SEC_DATA),
|
655 |
|
|
|
656 |
|
|
#ifdef TARGET_UNDERSCORE
|
657 |
|
|
TARGET_UNDERSCORE, /* leading underscore */
|
658 |
|
|
#else
|
659 |
|
|
0, /* leading underscore */
|
660 |
|
|
#endif
|
661 |
|
|
'/', /* ar_pad_char */
|
662 |
|
|
15, /* ar_max_namelen */
|
663 |
|
|
|
664 |
|
|
bfd_getl64, bfd_getl_signed_64, bfd_putl64,
|
665 |
|
|
bfd_getl32, bfd_getl_signed_32, bfd_putl32,
|
666 |
|
|
bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
|
667 |
|
|
bfd_getl64, bfd_getl_signed_64, bfd_putl64,
|
668 |
|
|
bfd_getl32, bfd_getl_signed_32, bfd_putl32,
|
669 |
|
|
bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
|
670 |
|
|
|
671 |
|
|
/* Note that we allow an object file to be treated as a core file as well. */
|
672 |
|
|
{_bfd_dummy_target, coff_object_p, /* bfd_check_format */
|
673 |
|
|
bfd_generic_archive_p, coff_object_p},
|
674 |
|
|
{bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
|
675 |
|
|
bfd_false},
|
676 |
|
|
{bfd_false, coff_write_object_contents, /* bfd_write_contents */
|
677 |
|
|
_bfd_write_archive_contents, bfd_false},
|
678 |
|
|
|
679 |
|
|
BFD_JUMP_TABLE_GENERIC (coff),
|
680 |
|
|
BFD_JUMP_TABLE_COPY (coff),
|
681 |
|
|
BFD_JUMP_TABLE_CORE (_bfd_nocore),
|
682 |
|
|
BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
|
683 |
|
|
BFD_JUMP_TABLE_SYMBOLS (coff),
|
684 |
|
|
BFD_JUMP_TABLE_RELOCS (coff),
|
685 |
|
|
BFD_JUMP_TABLE_WRITE (coff),
|
686 |
|
|
BFD_JUMP_TABLE_LINK (coff),
|
687 |
|
|
BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
|
688 |
|
|
|
689 |
|
|
NULL,
|
690 |
|
|
|
691 |
|
|
COFF_SWAP_TABLE
|
692 |
|
|
};
|