Support R_PPC64_ADDR64_LOCAL
[deliverable/binutils-gdb.git] / bfd / ppcboot.c
CommitLineData
252b5132 1/* BFD back-end for PPCbug boot records.
aa820537 2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
2c3fc389 3 2007, 2008, 2009, 2011, 2012 Free Software Foundation, Inc.
252b5132
RH
4 Written by Michael Meissner, Cygnus Support, <meissner@cygnus.com>
5
cd123cb7 6 This file is part of BFD, the Binary File Descriptor library.
252b5132 7
cd123cb7
NC
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
252b5132 12
cd123cb7
NC
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
252b5132 22
252b5132
RH
23
24/* This is a BFD backend which may be used to write PowerPCBug boot objects.
25 It may only be used for output, not input. The intention is that this may
26 be used as an output format for objcopy in order to generate raw binary
27 data.
28
29 This is very simple. The only complication is that the real data
30 will start at some address X, and in some cases we will not want to
31 include X zeroes just to get to that point. Since the start
32 address is not meaningful for this object file format, we use it
33 instead to indicate the number of zeroes to skip at the start of
34 the file. objcopy cooperates by specially setting the start
35 address to zero by default. */
36
3db64b00 37#include "sysdep.h"
3882b010 38#include "safe-ctype.h"
252b5132 39#include "bfd.h"
252b5132
RH
40#include "libbfd.h"
41
42/* PPCbug location structure */
2c3fc389
NC
43typedef struct ppcboot_location
44{
252b5132
RH
45 bfd_byte ind;
46 bfd_byte head;
47 bfd_byte sector;
48 bfd_byte cylinder;
49} ppcboot_location_t;
50
51/* PPCbug partition table layout */
2c3fc389
NC
52typedef struct ppcboot_partition
53{
252b5132
RH
54 ppcboot_location_t partition_begin; /* partition begin */
55 ppcboot_location_t partition_end; /* partition end */
56 bfd_byte sector_begin[4]; /* 32-bit start RBA (zero-based), little endian */
57 bfd_byte sector_length[4]; /* 32-bit RBA count (one-based), little endian */
58} ppcboot_partition_t;
59
60/* PPCbug boot layout. */
2c3fc389
NC
61typedef struct ppcboot_hdr
62{
252b5132
RH
63 bfd_byte pc_compatibility[446]; /* x86 instruction field */
64 ppcboot_partition_t partition[4]; /* partition information */
65 bfd_byte signature[2]; /* 0x55 and 0xaa */
66 bfd_byte entry_offset[4]; /* entry point offset, little endian */
67 bfd_byte length[4]; /* load image length, little endian */
68 bfd_byte flags; /* flag field */
69 bfd_byte os_id; /* OS_ID */
70 char partition_name[32]; /* partition name */
71 bfd_byte reserved1[470]; /* reserved */
8c763017
PB
72}
73#ifdef __GNUC__
74 __attribute__ ((packed))
75#endif
76ppcboot_hdr_t;
252b5132
RH
77
78/* Signature bytes for last 2 bytes of the 512 byte record */
79#define SIGNATURE0 0x55
80#define SIGNATURE1 0xaa
81
82/* PowerPC boot type */
83#define PPC_IND 0x41
84
85/* Information needed for ppcboot header */
2c3fc389
NC
86typedef struct ppcboot_data
87{
252b5132
RH
88 ppcboot_hdr_t header; /* raw header */
89 asection *sec; /* single section */
90} ppcboot_data_t;
91
92/* Any bfd we create by reading a ppcboot file has three symbols:
93 a start symbol, an end symbol, and an absolute length symbol. */
94#define PPCBOOT_SYMS 3
95
2c3fc389 96#define ppcboot_set_tdata(abfd, ptr) ((abfd)->tdata.any = (ptr))
252b5132
RH
97#define ppcboot_get_tdata(abfd) ((ppcboot_data_t *) ((abfd)->tdata.any))
98\f
99/* Create a ppcboot object. Invoked via bfd_set_format. */
100
b34976b6 101static bfd_boolean
2c3fc389 102ppcboot_mkobject (bfd *abfd)
252b5132
RH
103{
104 if (!ppcboot_get_tdata (abfd))
dc810e39
AM
105 {
106 bfd_size_type amt = sizeof (ppcboot_data_t);
107 ppcboot_set_tdata (abfd, bfd_zalloc (abfd, amt));
108 }
252b5132 109
b34976b6 110 return TRUE;
252b5132
RH
111}
112
113\f
114/* Set the architecture to PowerPC */
b34976b6 115static bfd_boolean
2c3fc389
NC
116ppcboot_set_arch_mach (bfd *abfd,
117 enum bfd_architecture arch,
118 unsigned long machine)
252b5132
RH
119{
120 if (arch == bfd_arch_unknown)
121 arch = bfd_arch_powerpc;
122
123 else if (arch != bfd_arch_powerpc)
b34976b6 124 return FALSE;
252b5132
RH
125
126 return bfd_default_set_arch_mach (abfd, arch, machine);
127}
128
129\f
130/* Any file may be considered to be a ppcboot file, provided the target
131 was not defaulted. That is, it must be explicitly specified as
132 being ppcboot. */
133
134static const bfd_target *
2c3fc389 135ppcboot_object_p (bfd *abfd)
252b5132
RH
136{
137 struct stat statbuf;
138 asection *sec;
139 ppcboot_hdr_t hdr;
140 size_t i;
141 ppcboot_data_t *tdata;
117ed4f8 142 flagword flags;
252b5132
RH
143
144 BFD_ASSERT (sizeof (ppcboot_hdr_t) == 1024);
145
146 if (abfd->target_defaulted)
147 {
148 bfd_set_error (bfd_error_wrong_format);
149 return NULL;
150 }
151
152 /* Find the file size. */
153 if (bfd_stat (abfd, &statbuf) < 0)
154 {
155 bfd_set_error (bfd_error_system_call);
156 return NULL;
157 }
158
159 if ((size_t) statbuf.st_size < sizeof (ppcboot_hdr_t))
160 {
161 bfd_set_error (bfd_error_wrong_format);
162 return NULL;
163 }
164
2c3fc389 165 if (bfd_bread (&hdr, (bfd_size_type) sizeof (hdr), abfd)
dc810e39 166 != sizeof (hdr))
252b5132
RH
167 {
168 if (bfd_get_error () != bfd_error_system_call)
169 bfd_set_error (bfd_error_wrong_format);
170
171 return NULL;
172 }
173
174 /* Now do some basic checks. */
175 for (i = 0; i < sizeof (hdr.pc_compatibility); i++)
176 if (hdr.pc_compatibility[i])
177 {
178 bfd_set_error (bfd_error_wrong_format);
179 return NULL;
180 }
181
182 if (hdr.signature[0] != SIGNATURE0 || hdr.signature[1] != SIGNATURE1)
183 {
184 bfd_set_error (bfd_error_wrong_format);
185 return NULL;
186 }
187
188 if (hdr.partition[0].partition_end.ind != PPC_IND)
189 {
190 bfd_set_error (bfd_error_wrong_format);
191 return NULL;
192 }
193
194 abfd->symcount = PPCBOOT_SYMS;
195
196 /* One data section. */
117ed4f8
AM
197 flags = SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_CODE | SEC_HAS_CONTENTS;
198 sec = bfd_make_section_with_flags (abfd, ".data", flags);
252b5132
RH
199 if (sec == NULL)
200 return NULL;
252b5132 201 sec->vma = 0;
eea6121a 202 sec->size = statbuf.st_size - sizeof (ppcboot_hdr_t);
252b5132
RH
203 sec->filepos = sizeof (ppcboot_hdr_t);
204
205 ppcboot_mkobject (abfd);
206 tdata = ppcboot_get_tdata (abfd);
207 tdata->sec = sec;
2c3fc389 208 memcpy (&tdata->header, &hdr, sizeof (ppcboot_hdr_t));
252b5132 209
dc810e39 210 ppcboot_set_arch_mach (abfd, bfd_arch_powerpc, 0L);
252b5132
RH
211 return abfd->xvec;
212}
213
214#define ppcboot_close_and_cleanup _bfd_generic_close_and_cleanup
215#define ppcboot_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
216#define ppcboot_new_section_hook _bfd_generic_new_section_hook
217
218\f
219/* Get contents of the only section. */
220
b34976b6 221static bfd_boolean
2c3fc389
NC
222ppcboot_get_section_contents (bfd *abfd,
223 asection *section ATTRIBUTE_UNUSED,
224 void * location,
225 file_ptr offset,
226 bfd_size_type count)
252b5132 227{
dc810e39
AM
228 if (bfd_seek (abfd, offset + (file_ptr) sizeof (ppcboot_hdr_t), SEEK_SET) != 0
229 || bfd_bread (location, count, abfd) != count)
b34976b6
AM
230 return FALSE;
231 return TRUE;
252b5132
RH
232}
233
234\f
235/* Return the amount of memory needed to read the symbol table. */
236
237static long
2c3fc389 238ppcboot_get_symtab_upper_bound (bfd *abfd ATTRIBUTE_UNUSED)
252b5132
RH
239{
240 return (PPCBOOT_SYMS + 1) * sizeof (asymbol *);
241}
242
243\f
244/* Create a symbol name based on the bfd's filename. */
245
246static char *
2c3fc389 247mangle_name (bfd *abfd, char *suffix)
252b5132 248{
dc810e39 249 bfd_size_type size;
252b5132
RH
250 char *buf;
251 char *p;
252
253 size = (strlen (bfd_get_filename (abfd))
254 + strlen (suffix)
255 + sizeof "_ppcboot__");
256
257 buf = (char *) bfd_alloc (abfd, size);
258 if (buf == NULL)
259 return "";
260
261 sprintf (buf, "_ppcboot_%s_%s", bfd_get_filename (abfd), suffix);
262
263 /* Change any non-alphanumeric characters to underscores. */
264 for (p = buf; *p; p++)
3882b010 265 if (! ISALNUM (*p))
252b5132
RH
266 *p = '_';
267
268 return buf;
269}
270
271\f
272/* Return the symbol table. */
273
274static long
2c3fc389 275ppcboot_canonicalize_symtab (bfd *abfd, asymbol **alocation)
252b5132
RH
276{
277 asection *sec = ppcboot_get_tdata (abfd)->sec;
278 asymbol *syms;
279 unsigned int i;
dc810e39 280 bfd_size_type amt = PPCBOOT_SYMS * sizeof (asymbol);
252b5132 281
dc810e39 282 syms = (asymbol *) bfd_alloc (abfd, amt);
252b5132 283 if (syms == NULL)
b34976b6 284 return FALSE;
252b5132
RH
285
286 /* Start symbol. */
287 syms[0].the_bfd = abfd;
288 syms[0].name = mangle_name (abfd, "start");
289 syms[0].value = 0;
290 syms[0].flags = BSF_GLOBAL;
291 syms[0].section = sec;
292 syms[0].udata.p = NULL;
293
294 /* End symbol. */
295 syms[1].the_bfd = abfd;
296 syms[1].name = mangle_name (abfd, "end");
eea6121a 297 syms[1].value = sec->size;
252b5132
RH
298 syms[1].flags = BSF_GLOBAL;
299 syms[1].section = sec;
300 syms[1].udata.p = NULL;
301
302 /* Size symbol. */
303 syms[2].the_bfd = abfd;
304 syms[2].name = mangle_name (abfd, "size");
eea6121a 305 syms[2].value = sec->size;
252b5132
RH
306 syms[2].flags = BSF_GLOBAL;
307 syms[2].section = bfd_abs_section_ptr;
308 syms[2].udata.p = NULL;
309
310 for (i = 0; i < PPCBOOT_SYMS; i++)
311 *alocation++ = syms++;
312 *alocation = NULL;
313
314 return PPCBOOT_SYMS;
315}
316
3f3c5c34 317#define ppcboot_make_empty_symbol _bfd_generic_make_empty_symbol
252b5132
RH
318#define ppcboot_print_symbol _bfd_nosymbols_print_symbol
319
320/* Get information about a symbol. */
321
322static void
2c3fc389
NC
323ppcboot_get_symbol_info (bfd *ignore_abfd ATTRIBUTE_UNUSED,
324 asymbol *symbol,
325 symbol_info *ret)
252b5132
RH
326{
327 bfd_symbol_info (symbol, ret);
328}
329
3c9458e9
NC
330#define ppcboot_bfd_is_target_special_symbol \
331 ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
252b5132
RH
332#define ppcboot_bfd_is_local_label_name bfd_generic_is_local_label_name
333#define ppcboot_get_lineno _bfd_nosymbols_get_lineno
334#define ppcboot_find_nearest_line _bfd_nosymbols_find_nearest_line
4ab527b0 335#define ppcboot_find_inliner_info _bfd_nosymbols_find_inliner_info
252b5132
RH
336#define ppcboot_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
337#define ppcboot_read_minisymbols _bfd_generic_read_minisymbols
338#define ppcboot_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
252b5132
RH
339\f
340/* Write section contents of a ppcboot file. */
341
b34976b6 342static bfd_boolean
2c3fc389
NC
343ppcboot_set_section_contents (bfd *abfd,
344 asection *sec,
345 const void * data,
346 file_ptr offset,
347 bfd_size_type size)
252b5132
RH
348{
349 if (! abfd->output_has_begun)
350 {
351 bfd_vma low;
352 asection *s;
353
354 /* The lowest section VMA sets the virtual address of the start
355 of the file. We use the set the file position of all the
356 sections. */
357 low = abfd->sections->vma;
358 for (s = abfd->sections->next; s != NULL; s = s->next)
359 if (s->vma < low)
360 low = s->vma;
361
362 for (s = abfd->sections; s != NULL; s = s->next)
363 s->filepos = s->vma - low;
364
b34976b6 365 abfd->output_has_begun = TRUE;
252b5132
RH
366 }
367
368 return _bfd_generic_set_section_contents (abfd, sec, data, offset, size);
369}
370
371\f
372static int
a6b96beb
AM
373ppcboot_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
374 struct bfd_link_info *info ATTRIBUTE_UNUSED)
252b5132
RH
375{
376 return sizeof (ppcboot_hdr_t);
377}
378
379\f
380/* Print out the program headers. */
381
b34976b6 382static bfd_boolean
2c3fc389 383ppcboot_bfd_print_private_bfd_data (bfd *abfd, void * farg)
252b5132
RH
384{
385 FILE *f = (FILE *)farg;
386 ppcboot_data_t *tdata = ppcboot_get_tdata (abfd);
2c3fc389
NC
387 long entry_offset = bfd_getl_signed_32 (tdata->header.entry_offset);
388 long length = bfd_getl_signed_32 (tdata->header.length);
252b5132
RH
389 int i;
390
391 fprintf (f, _("\nppcboot header:\n"));
0af1713e
AM
392 fprintf (f, _("Entry offset = 0x%.8lx (%ld)\n"),
393 (unsigned long) entry_offset, entry_offset);
394 fprintf (f, _("Length = 0x%.8lx (%ld)\n"),
395 (unsigned long) length, length);
252b5132
RH
396
397 if (tdata->header.flags)
398 fprintf (f, _("Flag field = 0x%.2x\n"), tdata->header.flags);
399
400 if (tdata->header.os_id)
401 fprintf (f, "OS_ID = 0x%.2x\n", tdata->header.os_id);
402
403 if (tdata->header.partition_name)
404 fprintf (f, _("Partition name = \"%s\"\n"), tdata->header.partition_name);
405
406 for (i = 0; i < 4; i++)
407 {
2c3fc389
NC
408 long sector_begin = bfd_getl_signed_32 (tdata->header.partition[i].sector_begin);
409 long sector_length = bfd_getl_signed_32 (tdata->header.partition[i].sector_length);
252b5132
RH
410
411 /* Skip all 0 entries */
412 if (!tdata->header.partition[i].partition_begin.ind
413 && !tdata->header.partition[i].partition_begin.head
414 && !tdata->header.partition[i].partition_begin.sector
415 && !tdata->header.partition[i].partition_begin.cylinder
416 && !tdata->header.partition[i].partition_end.ind
417 && !tdata->header.partition[i].partition_end.head
418 && !tdata->header.partition[i].partition_end.sector
419 && !tdata->header.partition[i].partition_end.cylinder
420 && !sector_begin && !sector_length)
421 continue;
422
423 fprintf (f, _("\nPartition[%d] start = { 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x }\n"), i,
424 tdata->header.partition[i].partition_begin.ind,
425 tdata->header.partition[i].partition_begin.head,
426 tdata->header.partition[i].partition_begin.sector,
427 tdata->header.partition[i].partition_begin.cylinder);
428
429 fprintf (f, _("Partition[%d] end = { 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x }\n"), i,
430 tdata->header.partition[i].partition_end.ind,
431 tdata->header.partition[i].partition_end.head,
432 tdata->header.partition[i].partition_end.sector,
433 tdata->header.partition[i].partition_end.cylinder);
434
0af1713e
AM
435 fprintf (f, _("Partition[%d] sector = 0x%.8lx (%ld)\n"),
436 i, (unsigned long) sector_begin, sector_begin);
437 fprintf (f, _("Partition[%d] length = 0x%.8lx (%ld)\n"),
438 i, (unsigned long) sector_length, sector_length);
252b5132
RH
439 }
440
441 fprintf (f, "\n");
b34976b6 442 return TRUE;
252b5132
RH
443}
444
445\f
446#define ppcboot_bfd_get_relocated_section_contents \
447 bfd_generic_get_relocated_section_contents
448#define ppcboot_bfd_relax_section bfd_generic_relax_section
449#define ppcboot_bfd_gc_sections bfd_generic_gc_sections
ae17ab41 450#define ppcboot_bfd_lookup_section_flags bfd_generic_lookup_section_flags
8550eb6e 451#define ppcboot_bfd_merge_sections bfd_generic_merge_sections
72adc230 452#define ppcboot_bfd_is_group_section bfd_generic_is_group_section
e61463e1 453#define ppcboot_bfd_discard_group bfd_generic_discard_group
082b7297
L
454#define ppcboot_section_already_linked \
455 _bfd_generic_section_already_linked
3023e3f6 456#define ppcboot_bfd_define_common_symbol bfd_generic_define_common_symbol
252b5132 457#define ppcboot_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
e2d34d7d 458#define ppcboot_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
252b5132 459#define ppcboot_bfd_link_add_symbols _bfd_generic_link_add_symbols
2d653fc7 460#define ppcboot_bfd_link_just_syms _bfd_generic_link_just_syms
1338dd10
PB
461#define ppcboot_bfd_copy_link_hash_symbol_type \
462 _bfd_generic_copy_link_hash_symbol_type
252b5132
RH
463#define ppcboot_bfd_final_link _bfd_generic_final_link
464#define ppcboot_bfd_link_split_section _bfd_generic_link_split_section
465#define ppcboot_get_section_contents_in_window \
466 _bfd_generic_get_section_contents_in_window
467
468#define ppcboot_bfd_copy_private_bfd_data _bfd_generic_bfd_copy_private_bfd_data
469#define ppcboot_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
470#define ppcboot_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
471#define ppcboot_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
80fccad2 472#define ppcboot_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
252b5132
RH
473#define ppcboot_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
474#define ppcboot_bfd_print_private_bfd_dat ppcboot_bfd_print_private_bfd_data
475
476const bfd_target ppcboot_vec =
477{
478 "ppcboot", /* name */
479 bfd_target_unknown_flavour, /* flavour */
480 BFD_ENDIAN_BIG, /* byteorder is big endian for code */
481 BFD_ENDIAN_LITTLE, /* header_byteorder */
482 EXEC_P, /* object_flags */
483 (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE | SEC_DATA
484 | SEC_ROM | SEC_HAS_CONTENTS), /* section_flags */
485 0, /* symbol_leading_char */
486 ' ', /* ar_pad_char */
487 16, /* ar_max_namelen */
0aabe54e 488 0, /* match priority. */
252b5132
RH
489 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
490 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
491 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
492 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
493 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
494 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
495 { /* bfd_check_format */
496 _bfd_dummy_target,
497 ppcboot_object_p, /* bfd_check_format */
498 _bfd_dummy_target,
499 _bfd_dummy_target,
500 },
501 { /* bfd_set_format */
502 bfd_false,
503 ppcboot_mkobject,
504 bfd_false,
505 bfd_false,
506 },
507 { /* bfd_write_contents */
508 bfd_false,
509 bfd_true,
510 bfd_false,
511 bfd_false,
512 },
513
514 BFD_JUMP_TABLE_GENERIC (ppcboot),
515 BFD_JUMP_TABLE_COPY (ppcboot),
516 BFD_JUMP_TABLE_CORE (_bfd_nocore),
517 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
518 BFD_JUMP_TABLE_SYMBOLS (ppcboot),
72f6ea61 519 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
252b5132
RH
520 BFD_JUMP_TABLE_WRITE (ppcboot),
521 BFD_JUMP_TABLE_LINK (ppcboot),
522 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
523
c3c89269 524 NULL,
892339ee 525
252b5132
RH
526 NULL
527};
This page took 0.886349 seconds and 4 git commands to generate.