1 /* BFD back-end for mmo objects (MMIX-specific object-format).
2 Copyright 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
4 Written by Hans-Peter Nilsson (hp@bitrange.com).
5 Infrastructure and other bits originally copied from srec.c and
8 This file is part of BFD, the Binary File Descriptor library.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 The mmo object format is used exclusively together with Professor
29 Donald E.@: Knuth's educational 64-bit processor MMIX. The simulator
30 @command{mmix} which is available at
31 @url{http://www-cs-faculty.stanford.edu/~knuth/programs/mmix.tar.gz}
32 understands this format. That package also includes a combined
33 assembler and linker called @command{mmixal}. The mmo format has
34 no advantages feature-wise compared to e.g. ELF. It is a simple
35 non-relocatable object format with no support for archives or
36 debugging information, except for symbol value information and
37 line numbers (which is not yet implemented in BFD). See
38 @url{http://www-cs-faculty.stanford.edu/~knuth/mmix.html} for more
39 information about MMIX. The ELF format is used for intermediate
40 object files in the BFD implementation.
42 @c We want to xref the symbol table node. A feature in "chew"
43 @c requires that "commands" do not contain spaces in the
44 @c arguments. Hence the hyphen in "Symbol-table".
48 @* mmo section mapping::
52 File layout, Symbol-table, mmo, mmo
56 The mmo file contents is not partitioned into named sections as
57 with e.g.@: ELF. Memory areas is formed by specifying the
58 location of the data that follows. Only the memory area
59 @samp{0x0000@dots{}00} to @samp{0x01ff@dots{}ff} is executable, so
60 it is used for code (and constants) and the area
61 @samp{0x2000@dots{}00} to @samp{0x20ff@dots{}ff} is used for
62 writable data. @xref{mmo section mapping}.
64 There is provision for specifying ``special data'' of 65536
65 different types. We use type 80 (decimal), arbitrarily chosen the
66 same as the ELF <<e_machine>> number for MMIX, filling it with
67 section information normally found in ELF objects. @xref{mmo
70 Contents is entered as 32-bit words, xor:ed over previous
71 contents, always zero-initialized. A word that starts with the
72 byte @samp{0x98} forms a command called a @samp{lopcode}, where
73 the next byte distinguished between the thirteen lopcodes. The
74 two remaining bytes, called the @samp{Y} and @samp{Z} fields, or
75 the @samp{YZ} field (a 16-bit big-endian number), are used for
76 various purposes different for each lopcode. As documented in
77 @url{http://www-cs-faculty.stanford.edu/~knuth/mmixal-intro.ps.gz},
82 0x98000001. The next word is contents, regardless of whether it
83 starts with 0x98 or not.
86 0x9801YYZZ, where @samp{Z} is 1 or 2. This is a location
87 directive, setting the location for the next data to the next
88 32-bit word (for @math{Z = 1}) or 64-bit word (for @math{Z = 2}),
89 plus @math{Y * 2^56}. Normally @samp{Y} is 0 for the text segment
90 and 2 for the data segment.
93 0x9802YYZZ. Increase the current location by @samp{YZ} bytes.
96 0x9803YYZZ, where @samp{Z} is 1 or 2. Store the current location
97 as 64 bits into the location pointed to by the next 32-bit
98 (@math{Z = 1}) or 64-bit (@math{Z = 2}) word, plus @math{Y *
102 0x9804YYZZ. @samp{YZ} is stored into the current location plus
106 0x980500ZZ. @samp{Z} is 16 or 24. A value @samp{L} derived from
107 the following 32-bit word are used in a manner similar to
108 @samp{YZ} in lop_fixr: it is xor:ed into the current location
109 minus @math{4 * L}. The first byte of the word is 0 or 1. If it
110 is 1, then @math{L = (@var{lowest 24 bits of word}) - 2^Z}, if 0,
111 then @math{L = (@var{lowest 24 bits of word})}.
114 0x9806YYZZ. @samp{Y} is the file number, @samp{Z} is count of
115 32-bit words. Set the file number to @samp{Y} and the line
116 counter to 0. The next @math{Z * 4} bytes contain the file name,
117 padded with zeros if the count is not a multiple of four. The
118 same @samp{Y} may occur multiple times, but @samp{Z} must be 0 for
119 all but the first occurrence.
122 0x9807YYZZ. @samp{YZ} is the line number. Together with
123 lop_file, it forms the source location for the next 32-bit word.
124 Note that for each non-lopcode 32-bit word, line numbers are
125 assumed incremented by one.
128 0x9808YYZZ. @samp{YZ} is the type number. Data until the next
129 lopcode other than lop_quote forms special data of type @samp{YZ}.
130 @xref{mmo section mapping}.
132 Other types than 80, (or type 80 with a content that does not
133 parse) is stored in sections named <<.MMIX.spec_data.@var{n}>>
134 where @var{n} is the @samp{YZ}-type. The flags for such a
135 sections say not to allocate or load the data. The vma is 0.
136 Contents of multiple occurrences of special data @var{n} is
137 concatenated to the data of the previous lop_spec @var{n}s. The
138 location in data or code at which the lop_spec occurred is lost.
141 0x980901ZZ. The first lopcode in a file. The @samp{Z} field forms the
142 length of header information in 32-bit words, where the first word
143 tells the time in seconds since @samp{00:00:00 GMT Jan 1 1970}.
146 0x980a00ZZ. @math{Z > 32}. This lopcode follows after all
147 content-generating lopcodes in a program. The @samp{Z} field
148 denotes the value of @samp{rG} at the beginning of the program.
149 The following @math{256 - Z} big-endian 64-bit words are loaded
150 into global registers @samp{$G} @dots{} @samp{$255}.
153 0x980b0000. The next-to-last lopcode in a program. Must follow
154 immediately after the lop_post lopcode and its data. After this
155 lopcode follows all symbols in a compressed format
156 (@pxref{Symbol-table}).
159 0x980cYYZZ. The last lopcode in a program. It must follow the
160 lop_stab lopcode and its data. The @samp{YZ} field contains the
161 number of 32-bit words of symbol table information after the
162 preceding lop_stab lopcode.
165 Note that the lopcode "fixups"; <<lop_fixr>>, <<lop_fixrx>> and
166 <<lop_fixo>> are not generated by BFD, but are handled. They are
167 generated by <<mmixal>>.
170 This trivial one-label, one-instruction file:
174 can be represented this way in mmo:
176 | 0x98090101 - lop_pre, one 32-bit word with timestamp.
178 | 0x98010002 - lop_loc, text segment, using a 64-bit address.
179 | Note that mmixal does not emit this for the file above.
180 | 0x00000000 - Address, high 32 bits.
181 | 0x00000000 - Address, low 32 bits.
182 | 0x98060002 - lop_file, 2 32-bit words for file-name.
183 | 0x74657374 - "test"
184 | 0x2e730000 - ".s\0\0"
185 | 0x98070001 - lop_line, line 1.
186 | 0x00010203 - TRAP 1,2,3
187 | 0x980a00ff - lop_post, setting $255 to 0.
190 | 0x980b0000 - lop_stab for ":Main" = 0, serial 1.
191 | 0x203a4040 @xref{Symbol-table}.
196 | 0x980c0005 - lop_end; symbol table contained five 32-bit words. */
201 #include "libiberty.h"
202 #include "elf/mmix.h"
203 #include "opcode/mmix.h"
220 #define LOP_QUOTE_NEXT ((LOP << 24) | (LOP_QUOTE << 16) | 1)
221 #define SPEC_DATA_SECTION 80
222 #define LOP_SPEC_SECTION \
223 ((LOP << 24) | (LOP_SPEC << 16) | SPEC_DATA_SECTION)
225 /* Must be a power of two. If you change this to be >= 64k, you need a
226 new test-case; the ld test b-loc64k.d touches chunk-size problem areas. */
227 #define MMO_SEC_CONTENTS_CHUNK_SIZE (1 << 15)
229 /* An arbitrary number for the maximum length section name size. */
230 #define MAX_SECTION_NAME_SIZE (1024 * 1024)
232 /* A quite arbitrary number for the maximum length section size. */
233 #define MAX_ARTIFICIAL_SECTION_SIZE (1024 * 1024 * 1024)
235 #define MMO3_WCHAR 0x80
236 #define MMO3_LEFT 0x40
237 #define MMO3_MIDDLE 0x20
238 #define MMO3_RIGHT 0x10
239 #define MMO3_TYPEBITS 0xf
240 #define MMO3_REGQUAL_BITS 0xf
243 #define MMO3_SYMBITS 0x2f
245 /* Put these everywhere in new code. */
246 #define FATAL_DEBUG \
247 _bfd_abort (__FILE__, __LINE__, \
248 "Internal: Non-debugged code (test-case missing)")
250 #define BAD_CASE(x) \
251 _bfd_abort (__FILE__, __LINE__, \
254 enum mmo_sym_type { mmo_reg_sym, mmo_undef_sym, mmo_data_sym, mmo_abs_sym};
256 /* When scanning the mmo file, a linked list of mmo_symbol
257 structures is built to represent the symbol table (if there is
262 struct mmo_symbol *next;
265 enum mmo_sym_type sym_type;
269 struct mmo_data_list_struct
271 struct mmo_data_list_struct *next;
274 bfd_size_type allocated_size;
278 typedef struct mmo_data_list_struct mmo_data_list_type;
280 struct mmo_symbol_trie
282 struct mmo_symbol_trie *left;
283 struct mmo_symbol_trie *right;
284 struct mmo_symbol_trie *middle;
288 /* A zero name means there's nothing here. */
289 struct mmo_symbol sym;
292 /* The mmo tdata information. */
294 struct mmo_data_struct
296 struct mmo_symbol *symbols;
297 struct mmo_symbol *symtail;
300 /* File representation of time (NULL) when this file was created. */
303 /* When we're reading bytes recursively, check this occasionally.
304 Also holds write errors. */
305 bfd_boolean have_error;
307 /* Max symbol length that may appear in the lop_stab table. Note that
308 this table might just hold a subset of symbols for not-really large
309 programs, as it can only be 65536 * 4 bytes large. */
310 int max_symbol_length;
312 /* Here's the symbol we build in lop_stab. */
313 char *lop_stab_symbol;
315 /* Index into lop_stab_symbol for the next character when parsing the
316 symbol information. */
319 /* When creating arbitrary sections, we need to count section numbers. */
322 /* When writing or reading byte-wise, we need to count the bytes
323 within a 32-bit word. */
326 /* We also need a buffer to hold the bytes we count reading or writing. */
330 typedef struct mmo_data_struct tdata_type;
332 struct mmo_section_data_struct
334 mmo_data_list_type *head;
335 mmo_data_list_type *tail;
338 #define mmo_section_data(sec) \
339 ((struct mmo_section_data_struct *) (sec)->used_by_bfd)
341 /* These structures are used in bfd_map_over_sections constructs. */
343 /* Used when writing out sections; all but the register contents section
344 which is stored in reg_section. */
345 struct mmo_write_sec_info
347 asection *reg_section;
351 /* Used when trying to find a section corresponding to addr. */
352 struct mmo_find_sec_info
358 static bfd_boolean mmo_bfd_copy_private_bfd_data (bfd *, bfd *);
359 static void mmo_write_section_unless_reg_contents (bfd *, asection *, void *);
360 static void mmo_find_sec_w_addr (bfd *, asection *, void *);
361 static void mmo_find_sec_w_addr_grow (bfd *, asection *, void *);
362 static asection *mmo_make_section (bfd *, const char *);
363 static void mmo_get_symbol_info (bfd *, asymbol *, symbol_info *);
364 static void mmo_print_symbol (bfd *, void *, asymbol *,
365 bfd_print_symbol_type);
366 static void mmo_init (void);
367 static bfd_boolean mmo_mkobject (bfd *);
368 static bfd_boolean mmo_scan (bfd *);
369 static asection *mmo_decide_section (bfd *, bfd_vma);
370 static asection *mmo_get_generic_spec_data_section (bfd *, int);
371 static asection *mmo_get_spec_section (bfd *, int);
372 static INLINE bfd_byte *mmo_get_loc (asection *, bfd_vma, int);
373 static void mmo_xore_64 (asection *, bfd_vma vma, bfd_vma value);
374 static void mmo_xore_32 (asection *, bfd_vma vma, unsigned int);
375 static void mmo_xore_16 (asection *, bfd_vma vma, unsigned int);
376 static const bfd_target *mmo_object_p (bfd *);
377 static void mmo_map_set_sizes (bfd *, asection *, void *);
378 static bfd_boolean mmo_get_symbols (bfd *);
379 static bfd_boolean mmo_create_symbol (bfd *, const char *, bfd_vma,
380 enum mmo_sym_type, unsigned int);
381 static bfd_boolean mmo_get_section_contents (bfd *, asection *, void *,
382 file_ptr, bfd_size_type);
383 static long mmo_get_symtab_upper_bound (bfd *);
384 static long mmo_canonicalize_symtab (bfd *, asymbol **);
385 static void mmo_get_symbol_info (bfd *, asymbol *, symbol_info *);
386 static void mmo_print_symbol (bfd *, void *, asymbol *,
387 bfd_print_symbol_type);
388 static bfd_boolean mmo_set_section_contents (bfd *, sec_ptr, const void *,
389 file_ptr, bfd_size_type);
390 static int mmo_sizeof_headers (bfd *, bfd_boolean);
391 static long mmo_get_reloc_upper_bound (bfd *, asection *);
392 static bfd_boolean mmo_internal_write_header (bfd *);
393 static bfd_boolean mmo_internal_write_post (bfd *, int, asection *);
394 static bfd_boolean mmo_internal_add_3_sym (bfd *, struct mmo_symbol_trie *,
395 const struct mmo_symbol *);
396 static unsigned int mmo_internal_3_length (bfd *, struct mmo_symbol_trie *);
397 static void mmo_internal_3_dump (bfd *, struct mmo_symbol_trie *);
398 static void mmo_beb128_out (bfd *, int, int);
399 static bfd_boolean mmo_internal_write_section (bfd *, asection *);
400 static void mmo_write_tetra (bfd *, unsigned int);
401 static void mmo_write_tetra_raw (bfd *, unsigned int);
402 static void mmo_write_octa (bfd *, bfd_vma);
403 static void mmo_write_octa_raw (bfd *, bfd_vma);
404 static bfd_boolean mmo_write_chunk (bfd *, const bfd_byte *, unsigned int);
405 static bfd_boolean mmo_flush_chunk (bfd *);
406 static bfd_boolean mmo_write_loc_chunk (bfd *, bfd_vma, const bfd_byte *,
407 unsigned int, bfd_vma *);
408 static bfd_boolean mmo_write_chunk_list (bfd *, mmo_data_list_type *);
409 static bfd_boolean mmo_write_loc_chunk_list (bfd *, mmo_data_list_type *);
410 static bfd_boolean mmo_write_symbols_and_terminator (bfd *);
411 static flagword mmo_sec_flags_from_bfd_flags (flagword);
412 static flagword bfd_sec_flags_from_mmo_flags (flagword);
413 static bfd_byte mmo_get_byte (bfd *);
414 static void mmo_write_byte (bfd *, bfd_byte);
415 static bfd_boolean mmo_new_section_hook (bfd *, asection *);
416 static int mmo_sort_mmo_symbols (const void *, const void *);
417 static bfd_boolean mmo_write_object_contents (bfd *);
418 static long mmo_canonicalize_reloc (bfd *, sec_ptr, arelent **, asymbol **);
419 static bfd_boolean mmo_write_section_description (bfd *, asection *);
420 static bfd_boolean mmo_has_leading_or_trailing_zero_tetra_p (bfd *,
423 /* Global "const" variables initialized once. Must not depend on
424 particular input or caller; put such things into the bfd or elsewhere.
425 Look ma, no static per-invocation data! */
428 char valid_mmo_symbol_character_set[/* A-Z a-z (we assume consecutive
429 codes; sorry EBCDIC:ers!). */
430 + 'Z' - 'A' + 1 + 'z' - 'a' + 1
435 /* Codes higher than 126. */
441 /* Get section SECNAME or create one if it doesn't exist. When creating
442 one, new memory for the name is allocated. */
445 mmo_make_section (bfd *abfd, const char *secname)
447 asection *sec = bfd_get_section_by_name (abfd, secname);
451 char *newsecname = strdup (secname);
453 if (newsecname == NULL)
455 (*_bfd_error_handler)
456 (_("%s: No core to allocate section name %s\n"),
457 bfd_get_filename (abfd), secname);
458 bfd_set_error (bfd_error_system_call);
461 sec = bfd_make_section (abfd, newsecname);
467 /* Nothing to do, but keep as a placeholder if we need it.
468 Note that state that might differ between bfd:s must not be initialized
469 here, nor must it be static. Add it to tdata information instead. */
474 static bfd_boolean inited = FALSE;
477 static const char letters[]
478 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:_";
484 /* Fill in the set of valid symbol characters. */
485 strcpy (valid_mmo_symbol_character_set, letters);
486 i = strlen (letters);
488 for (j = 126; j < 256; j++)
489 valid_mmo_symbol_character_set[i++] = j;
492 /* Check whether an existing file is an mmo file. */
494 static const bfd_target *
495 mmo_object_p (bfd *abfd)
502 if (bfd_stat (abfd, &statbuf) < 0
503 || bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
504 || bfd_bread (b, 4, abfd) != 4)
507 /* All mmo files are a multiple of four bytes long.
508 Only recognize version one. */
509 if ((statbuf.st_size % 4) != 0
510 || b[0] != LOP || b[1] != LOP_PRE || b[2] != 1)
513 /* Get the last 32-bit word. */
514 if (bfd_seek (abfd, (file_ptr) statbuf.st_size - 4, SEEK_SET) != 0
515 || bfd_bread (b, 4, abfd) != 4)
518 /* Check if the file ends in a lop_end lopcode. */
519 if (b[0] != LOP || b[1] != LOP_END || ! mmo_mkobject (abfd))
522 /* Compute an upper bound on the max symbol length. Not really
523 important as all of the symbol information can only be 256k. */
524 abfd->tdata.mmo_data->max_symbol_length = (b[2] * 256 + b[3]) * 4;
525 abfd->tdata.mmo_data->lop_stab_symbol
526 = bfd_malloc (abfd->tdata.mmo_data->max_symbol_length + 1);
528 if (abfd->tdata.mmo_data->lop_stab_symbol == NULL)
530 (*_bfd_error_handler)
531 (_("%s: No core to allocate a symbol %d bytes long\n"),
532 bfd_get_filename (abfd), abfd->tdata.mmo_data->max_symbol_length);
536 /* Read in everything. */
537 if (! mmo_scan (abfd))
538 goto bad_format_free;
540 if (abfd->symcount > 0)
541 abfd->flags |= HAS_SYMS;
543 /* You'll have to tweak this if you want to use this format for other
544 arches (not recommended due to its small-size limitations). Look at
545 the ELF format for how to make it target-generic. */
546 if (! bfd_default_set_arch_mach (abfd, bfd_arch_mmix, 0))
547 goto bad_format_free;
552 free (abfd->tdata.mmo_data->lop_stab_symbol);
554 bfd_set_error (bfd_error_wrong_format);
559 /* Set up the mmo tdata information. */
562 mmo_mkobject (bfd *abfd)
566 if (abfd->tdata.mmo_data == NULL)
570 /* All fields are zero-initialized, so we don't have to explicitly
572 tdata_type *tdata = (tdata_type *) bfd_zmalloc (sizeof (tdata_type));
576 created = time (NULL);
577 bfd_put_32 (abfd, created, tdata->created);
579 abfd->tdata.mmo_data = tdata;
586 mmo_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
588 if (bfd_get_flavour (ibfd) != bfd_target_mmo_flavour
589 || bfd_get_flavour (obfd) != bfd_target_mmo_flavour)
592 /* Copy the time the copied-from file was created. If people want the
593 time the file was last *modified*, they have that in the normal file
595 memcpy (obfd->tdata.mmo_data->created, ibfd->tdata.mmo_data->created,
596 sizeof (obfd->tdata.mmo_data->created));
600 /* Helper functions for mmo_decide_section, used through
601 bfd_map_over_sections. */
604 mmo_find_sec_w_addr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *p)
606 struct mmo_find_sec_info *infop = (struct mmo_find_sec_info *) p;
607 bfd_vma vma = bfd_get_section_vma (abfd, sec);
609 /* Ignore sections that aren't loaded. */
610 if ((bfd_get_section_flags (abfd, sec) & (SEC_LOAD | SEC_ALLOC))
611 != (SEC_LOAD | SEC_ALLOC))
614 if (infop->addr >= vma && infop->addr < vma + sec->size)
619 mmo_find_sec_w_addr_grow (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *p)
621 struct mmo_find_sec_info *infop = (struct mmo_find_sec_info *) p;
622 bfd_vma vma = bfd_get_section_vma (abfd, sec);
624 /* Ignore sections that aren't loaded. */
625 if ((bfd_get_section_flags (abfd, sec) & (SEC_LOAD | SEC_ALLOC))
626 != (SEC_LOAD | SEC_ALLOC))
629 if (infop->addr >= vma && infop->addr < vma + MAX_ARTIFICIAL_SECTION_SIZE)
633 /* Find a section that corresponds to a VMA. Automatically create .text
634 or .data and set current section to it, depending on what vma. If we
635 can't deduce a section, make one up as ".MMIX.sec.N", where N is an
636 increasing number. */
639 mmo_decide_section (bfd *abfd, bfd_vma vma)
641 asection *sec = NULL;
642 char sec_name[sizeof (".MMIX.sec.") + 20];
643 struct mmo_find_sec_info info;
648 /* First see if there's a section that would match exactly. */
649 bfd_map_over_sections (abfd, mmo_find_sec_w_addr, &info);
651 if (info.sec != NULL)
654 /* If there's no such section, try and expand one of the existing ones,
655 up to a limit. Make sure we have .text and .data before we try that;
656 create them corresponding to expected addresses and set flags to make
657 them match the "loaded and with contents" expectation. */
658 if ((vma >> 56) == 0)
660 sec = bfd_make_section_old_way (abfd, MMO_TEXT_SECTION_NAME);
665 if (! sec->user_set_vma)
666 bfd_set_section_vma (abfd, sec, vma);
667 if (! bfd_set_section_flags (abfd, sec,
668 bfd_get_section_flags (abfd, sec)
669 | SEC_CODE | SEC_LOAD | SEC_ALLOC))
672 else if ((vma >> 56) == 0x20)
674 sec = bfd_make_section_old_way (abfd, MMO_DATA_SECTION_NAME);
679 if (! sec->user_set_vma)
680 bfd_set_section_vma (abfd, sec, vma);
681 if (! bfd_set_section_flags (abfd, sec,
682 bfd_get_section_flags (abfd, sec)
683 | SEC_LOAD | SEC_ALLOC))
687 bfd_map_over_sections (abfd, mmo_find_sec_w_addr_grow, &info);
689 if (info.sec != NULL)
692 /* If there's still no suitable section, make a new one. */
693 sprintf (sec_name, ".MMIX.sec.%d", abfd->tdata.mmo_data->sec_no++);
694 sec = mmo_make_section (abfd, sec_name);
695 if (! sec->user_set_vma)
696 bfd_set_section_vma (abfd, sec, vma);
698 if (! bfd_set_section_flags (abfd, sec,
699 bfd_get_section_flags (abfd, sec)
700 | SEC_LOAD | SEC_ALLOC))
705 /* Xor in a 64-bit value VALUE at VMA. */
708 mmo_xore_64 (asection *sec, bfd_vma vma, bfd_vma value)
710 bfd_byte *loc = mmo_get_loc (sec, vma, 8);
711 bfd_vma prev = bfd_get_64 (sec->owner, loc);
714 bfd_put_64 (sec->owner, value, loc);
717 /* Xor in a 32-bit value VALUE at VMA. */
720 mmo_xore_32 (asection *sec, bfd_vma vma, unsigned int value)
722 bfd_byte *loc = mmo_get_loc (sec, vma, 4);
723 unsigned int prev = bfd_get_32 (sec->owner, loc);
726 bfd_put_32 (sec->owner, value, loc);
729 /* Xor in a 16-bit value VALUE at VMA. */
732 mmo_xore_16 (asection *sec, bfd_vma vma, unsigned int value)
734 bfd_byte *loc = mmo_get_loc (sec, vma, 2);
735 unsigned int prev = bfd_get_16 (sec->owner, loc);
738 bfd_put_16 (sec->owner, value, loc);
741 /* Write a 32-bit word to output file, no lop_quote generated. */
744 mmo_write_tetra_raw (bfd *abfd, unsigned int value)
748 bfd_put_32 (abfd, value, buf);
750 if (bfd_bwrite (buf, 4, abfd) != 4)
751 abfd->tdata.mmo_data->have_error = TRUE;
754 /* Write a 32-bit word to output file; lop_quote if necessary. */
757 mmo_write_tetra (bfd *abfd, unsigned int value)
759 if (((value >> 24) & 0xff) == LOP)
760 mmo_write_tetra_raw (abfd, LOP_QUOTE_NEXT);
762 mmo_write_tetra_raw (abfd, value);
765 /* Write a 64-bit word to output file, perhaps with lop_quoting. */
768 mmo_write_octa (bfd *abfd, bfd_vma value)
770 mmo_write_tetra (abfd, (unsigned int) (value >> 32));
771 mmo_write_tetra (abfd, (unsigned int) value);
774 /* Write a 64-bit word to output file, without lop_quoting. */
777 mmo_write_octa_raw (bfd *abfd, bfd_vma value)
779 mmo_write_tetra_raw (abfd, (unsigned int) (value >> 32));
780 mmo_write_tetra_raw (abfd, (unsigned int) value);
783 /* Write quoted contents. Intended to be called multiple times in
784 sequence, followed by a call to mmo_flush_chunk. */
786 static INLINE bfd_boolean
787 mmo_write_chunk (bfd *abfd, const bfd_byte *loc, unsigned int len)
789 bfd_boolean retval = TRUE;
791 /* Fill up a tetra from bytes remaining from a previous chunk. */
792 if (abfd->tdata.mmo_data->byte_no != 0)
794 while (abfd->tdata.mmo_data->byte_no < 4 && len != 0)
796 abfd->tdata.mmo_data->buf[abfd->tdata.mmo_data->byte_no++] = *loc++;
800 if (abfd->tdata.mmo_data->byte_no == 4)
802 mmo_write_tetra (abfd,
803 bfd_get_32 (abfd, abfd->tdata.mmo_data->buf));
804 abfd->tdata.mmo_data->byte_no = 0;
811 mmo_write_tetra_raw (abfd, LOP_QUOTE_NEXT);
814 && ! abfd->tdata.mmo_data->have_error
815 && 4 == bfd_bwrite (loc, 4, abfd));
823 memcpy (abfd->tdata.mmo_data->buf, loc, len);
824 abfd->tdata.mmo_data->byte_no = len;
828 abfd->tdata.mmo_data->have_error = TRUE;
832 /* Flush remaining bytes, from a previous mmo_write_chunk, zero-padded to
835 static INLINE bfd_boolean
836 mmo_flush_chunk (bfd *abfd)
838 if (abfd->tdata.mmo_data->byte_no != 0)
840 memset (abfd->tdata.mmo_data->buf + abfd->tdata.mmo_data->byte_no,
841 0, 4 - abfd->tdata.mmo_data->byte_no);
842 mmo_write_tetra (abfd,
843 bfd_get_32 (abfd, abfd->tdata.mmo_data->buf));
844 abfd->tdata.mmo_data->byte_no = 0;
847 return ! abfd->tdata.mmo_data->have_error;
850 /* Same, but from a list. */
852 static INLINE bfd_boolean
853 mmo_write_chunk_list (bfd *abfd, mmo_data_list_type *datap)
855 for (; datap != NULL; datap = datap->next)
856 if (! mmo_write_chunk (abfd, datap->data, datap->size))
859 return mmo_flush_chunk (abfd);
862 /* Write a lop_loc and some contents. A caller needs to call
863 mmo_flush_chunk after calling this function. The location is only
864 output if different than *LAST_VMAP, which is updated after this call. */
867 mmo_write_loc_chunk (bfd *abfd, bfd_vma vma, const bfd_byte *loc,
868 unsigned int len, bfd_vma *last_vmap)
870 /* Find an initial and trailing section of zero tetras; we don't need to
871 write out zeros. FIXME: When we do this, we should emit section size
872 and address specifiers, else objcopy can't always perform an identity
873 translation. Only do this if we *don't* have left-over data from a
874 previous write or the vma of this chunk is *not* the next address,
875 because then data isn't tetrabyte-aligned and we're concatenating to
876 that left-over data. */
878 if (abfd->tdata.mmo_data->byte_no == 0 || vma != *last_vmap)
880 while (len >= 4 && bfd_get_32 (abfd, loc) == 0)
887 while (len >= 4 && bfd_get_32 (abfd, loc + len - 4) == 0)
891 /* Only write out the location if it's different than the one the caller
892 (supposedly) previously handled, accounting for omitted leading zeros. */
893 if (vma != *last_vmap)
895 /* We might be in the middle of a sequence. */
896 mmo_flush_chunk (abfd);
898 /* We always write the location as 64 bits; no use saving bytes
900 mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_LOC << 16) | 2);
901 mmo_write_octa_raw (abfd, vma);
904 /* Update to reflect end of this chunk, with trailing zeros omitted. */
905 *last_vmap = vma + len;
907 return (! abfd->tdata.mmo_data->have_error
908 && mmo_write_chunk (abfd, loc, len));
911 /* Same, but from a list. */
913 static INLINE bfd_boolean
914 mmo_write_loc_chunk_list (bfd *abfd, mmo_data_list_type *datap)
916 /* Get an address different than the address of the first chunk. */
917 bfd_vma last_vma = datap ? datap->where - 1 : 0;
919 for (; datap != NULL; datap = datap->next)
920 if (! mmo_write_loc_chunk (abfd, datap->where, datap->data, datap->size,
924 return mmo_flush_chunk (abfd);
927 /* Make a .MMIX.spec_data.N section. */
930 mmo_get_generic_spec_data_section (bfd *abfd, int spec_data_number)
933 char secname[sizeof (MMIX_OTHER_SPEC_SECTION_PREFIX) + 20]
934 = MMIX_OTHER_SPEC_SECTION_PREFIX;
936 sprintf (secname + strlen (MMIX_OTHER_SPEC_SECTION_PREFIX),
937 "%d", spec_data_number);
939 sec = mmo_make_section (abfd, secname);
944 /* Make a special section for SPEC_DATA_NUMBER. If it is the one we use
945 ourselves, parse some of its data to get at the section name. */
948 mmo_get_spec_section (bfd *abfd, int spec_data_number)
953 unsigned int secname_length;
955 bfd_vma section_length;
957 mmo_data_list_type *loc;
961 /* If this isn't the "special" special data, then make a placeholder
963 if (spec_data_number != SPEC_DATA_SECTION)
964 return mmo_get_generic_spec_data_section (abfd, spec_data_number);
966 /* Seek back to this position if there was a format error. */
967 orig_pos = bfd_tell (abfd);
969 /* Read the length (in 32-bit words). */
970 if (bfd_bread (buf, 4, abfd) != 4)
975 if (buf[1] != LOP_QUOTE)
978 if (bfd_bread (buf, 4, abfd) != 4)
982 /* We don't care to keep the name length accurate. It's
984 secname_length = bfd_get_32 (abfd, buf) * 4;
986 /* Check section name length for sanity. */
987 if (secname_length > MAX_SECTION_NAME_SIZE)
990 /* This should be free'd regardless if a section is created. */
991 secname = bfd_malloc (secname_length + 1);
992 secname[secname_length] = 0;
994 for (i = 0; i < secname_length / 4; i++)
996 if (bfd_bread (secname + i * 4, 4, abfd) != 4)
997 goto format_error_free;
999 if (secname[i * 4] == (char) LOP)
1001 /* A bit of overkill, but we handle char 0x98 in a section name,
1002 and recognize misparsing. */
1003 if (secname[i * 4 + 1] != LOP_QUOTE
1004 || bfd_bread (secname + i * 4, 4, abfd) != 4)
1005 /* Whoops. We thought this was a name, and now we found a
1006 non-lop_quote lopcode before we parsed the whole length of
1007 the name. Signal end-of-file in the same manner. */
1008 goto format_error_free;
1012 /* Get the section flags. */
1013 if (bfd_bread (buf, 4, abfd) != 4
1015 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1016 goto format_error_free;
1018 flags = bfd_get_32 (abfd, buf);
1020 /* Get the section length. */
1021 if (bfd_bread (buf, 4, abfd) != 4
1023 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1024 goto format_error_free;
1026 section_length = (bfd_vma) bfd_get_32 (abfd, buf) << 32;
1028 /* That's the first, high-part. Now get the low part. */
1030 if (bfd_bread (buf, 4, abfd) != 4
1032 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1033 goto format_error_free;
1035 section_length |= (bfd_vma) bfd_get_32 (abfd, buf);
1037 /* Check the section length for sanity. */
1038 if (section_length > MAX_ARTIFICIAL_SECTION_SIZE)
1039 goto format_error_free;
1041 /* Get the section VMA. */
1042 if (bfd_bread (buf, 4, abfd) != 4
1044 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1045 goto format_error_free;
1047 section_vma = (bfd_vma) bfd_get_32 (abfd, buf) << 32;
1049 /* That's the first, high-part. Now get the low part. */
1050 if (bfd_bread (buf, 4, abfd) != 4
1052 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1053 goto format_error_free;
1055 section_vma |= (bfd_vma) bfd_get_32 (abfd, buf);
1057 sec = mmo_make_section (abfd, secname);
1062 /* We allocate a buffer here for the advertised size, with head room for
1063 tetrabyte alignment. */
1064 loc = bfd_zmalloc (section_length + 3
1065 + sizeof (struct mmo_data_list_struct));
1069 /* Use a TETRA-rounded size for the allocated buffer; we set the
1070 "visible" section size below. */
1071 loc->size = (section_length + 3) & ~3;
1073 /* Add in the section flags we found to those bfd entered during this
1074 process and set the contents. */
1075 if (! bfd_set_section_flags (abfd, sec,
1076 bfd_sec_flags_from_mmo_flags (flags)
1077 | bfd_get_section_flags (abfd, sec)
1078 | (section_length != 0 ? SEC_HAS_CONTENTS : 0))
1079 || ! bfd_set_section_size (abfd, sec, sec->size + section_length)
1080 /* Set VMA only for the first occurrence. */
1081 || (! sec->user_set_vma
1082 && ! bfd_set_section_vma (abfd, sec, section_vma)))
1084 /* If we get an error for any of the calls above, signal more than
1085 just a format error for the spec section. */
1090 if (mmo_section_data (sec)->tail != NULL)
1091 mmo_section_data (sec)->tail->next = loc;
1093 mmo_section_data (sec)->head = loc;
1094 mmo_section_data (sec)->tail = loc;
1095 loc->where = section_vma;
1102 if (bfd_seek (abfd, orig_pos, SEEK_SET) != 0)
1105 return mmo_get_generic_spec_data_section (abfd, spec_data_number);
1108 /* Read a byte, but read from file in multiples of 32-bit words. */
1111 mmo_get_byte (bfd *abfd)
1115 if (abfd->tdata.mmo_data->byte_no == 0)
1117 if (! abfd->tdata.mmo_data->have_error
1118 && bfd_bread (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
1120 abfd->tdata.mmo_data->have_error = TRUE;
1122 /* A value somewhat safe against tripping on some inconsistency
1123 when mopping up after this error. */
1128 retval = abfd->tdata.mmo_data->buf[abfd->tdata.mmo_data->byte_no];
1129 abfd->tdata.mmo_data->byte_no = (abfd->tdata.mmo_data->byte_no + 1) % 4;
1134 /* Write a byte, in multiples of 32-bit words. */
1137 mmo_write_byte (bfd *abfd, bfd_byte value)
1139 abfd->tdata.mmo_data->buf[(abfd->tdata.mmo_data->byte_no++ % 4)] = value;
1140 if ((abfd->tdata.mmo_data->byte_no % 4) == 0)
1142 if (! abfd->tdata.mmo_data->have_error
1143 && bfd_bwrite (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
1144 abfd->tdata.mmo_data->have_error = TRUE;
1148 /* Create a symbol. */
1151 mmo_create_symbol (bfd *abfd, const char *symname, bfd_vma addr, enum
1152 mmo_sym_type sym_type, unsigned int serno)
1154 struct mmo_symbol *n;
1156 n = (struct mmo_symbol *) bfd_alloc (abfd, sizeof (struct mmo_symbol));
1160 n->name = bfd_alloc (abfd, strlen (symname) + 1);
1161 if (n->name == NULL)
1164 strcpy (n->name, symname);
1167 n->sym_type = sym_type;
1170 if (abfd->tdata.mmo_data->symbols == NULL)
1171 abfd->tdata.mmo_data->symbols = n;
1173 abfd->tdata.mmo_data->symtail->next = n;
1174 abfd->tdata.mmo_data->symtail = n;
1179 /* Check that :Main equals the last octa of the .MMIX.reg_contents
1180 section, as it's the one place we're sure to pass when reading a mmo
1181 object. For written objects, we do it while setting the symbol
1183 if (strcmp (symname, MMIX_START_SYMBOL_NAME) == 0
1184 && bfd_get_start_address (abfd) != addr)
1186 (*_bfd_error_handler)
1187 (_("%s: invalid mmo file: initialization value for $255 is not `Main'\n"),
1188 bfd_get_filename (abfd));
1189 bfd_set_error (bfd_error_bad_value);
1196 /* Read in symbols. */
1199 mmo_get_symbols (bfd *abfd)
1203 Symbol-table, mmo section mapping, File layout, mmo
1207 From mmixal.w (or really, the generated mmixal.tex) in
1208 @url{http://www-cs-faculty.stanford.edu/~knuth/programs/mmix.tar.gz}):
1209 ``Symbols are stored and retrieved by means of a @samp{ternary
1210 search trie}, following ideas of Bentley and Sedgewick. (See
1211 ACM--SIAM Symp.@: on Discrete Algorithms @samp{8} (1997), 360--369;
1212 R.@:Sedgewick, @samp{Algorithms in C} (Reading, Mass.@:
1213 Addison--Wesley, 1998), @samp{15.4}.) Each trie node stores a
1214 character, and there are branches to subtries for the cases where
1215 a given character is less than, equal to, or greater than the
1216 character in the trie. There also is a pointer to a symbol table
1217 entry if a symbol ends at the current node.''
1219 So it's a tree encoded as a stream of bytes. The stream of bytes
1220 acts on a single virtual global symbol, adding and removing
1221 characters and signalling complete symbol points. Here, we read
1222 the stream and create symbols at the completion points.
1224 First, there's a control byte <<m>>. If any of the listed bits
1225 in <<m>> is nonzero, we execute what stands at the right, in
1229 | 0x40 - Traverse left trie.
1230 | (Read a new command byte and recurse.)
1233 | 0x2f - Read the next byte as a character and store it in the
1234 | current character position; increment character position.
1235 | Test the bits of <<m>>:
1238 | 0x80 - The character is 16-bit (so read another byte,
1239 | merge into current character.
1242 | 0xf - We have a complete symbol; parse the type, value
1243 | and serial number and do what should be done
1244 | with a symbol. The type and length information
1245 | is in j = (m & 0xf).
1247 | (MMO3_REGQUAL_BITS)
1248 | j == 0xf: A register variable. The following
1249 | byte tells which register.
1250 | j <= 8: An absolute symbol. Read j bytes as the
1251 | big-endian number the symbol equals.
1252 | A j = 2 with two zero bytes denotes an
1254 | j > 8: As with j <= 8, but add (0x20 << 56)
1255 | to the value in the following j - 8
1258 | Then comes the serial number, as a variant of
1259 | uleb128, but better named ubeb128:
1260 | Read bytes and shift the previous value left 7
1261 | (multiply by 128). Add in the new byte, repeat
1262 | until a byte has bit 7 set. The serial number
1263 | is the computed value minus 128.
1266 | 0x20 - Traverse middle trie. (Read a new command byte
1267 | and recurse.) Decrement character position.
1270 | 0x10 - Traverse right trie. (Read a new command byte and
1273 Let's look again at the <<lop_stab>> for the trivial file
1274 (@pxref{File layout}).
1276 | 0x980b0000 - lop_stab for ":Main" = 0, serial 1.
1283 This forms the trivial trie (note that the path between ``:'' and
1284 ``M'' is redundant):
1295 | 016e "n" is the last character in a full symbol, and
1296 | with a value represented in one byte.
1297 | 00 The value is 0.
1298 | 81 The serial number is 1. */
1300 bfd_byte m = mmo_get_byte (abfd);
1302 /* Check first if we have a bad hair day. */
1303 if (abfd->tdata.mmo_data->have_error)
1307 /* Traverse left trie. */
1308 mmo_get_symbols (abfd);
1310 if (m & MMO3_SYMBITS)
1312 bfd_byte c = mmo_get_byte (abfd);
1313 bfd_byte j = m & MMO3_TYPEBITS;
1315 enum mmo_sym_type sym_type;
1316 unsigned int serno = 0;
1321 bfd_byte c2 = mmo_get_byte (abfd);
1323 /* A two-byte character. We can't grok this, but neither can
1324 mmotype, for other cases than the second byte being zero. */
1328 abfd->tdata.mmo_data->lop_stab_symbol
1329 [abfd->tdata.mmo_data->symbol_position] = 0;
1331 (*_bfd_error_handler)
1332 (_("%s: unsupported wide character sequence"
1333 " 0x%02X 0x%02X after symbol name starting with `%s'\n"),
1334 bfd_get_filename (abfd), c, c2,
1335 abfd->tdata.mmo_data->lop_stab_symbol);
1336 bfd_set_error (bfd_error_bad_value);
1337 abfd->tdata.mmo_data->have_error = TRUE;
1344 abfd->tdata.mmo_data->lop_stab_symbol[abfd->tdata.mmo_data->symbol_position++] = c;
1345 abfd->tdata.mmo_data->lop_stab_symbol[abfd->tdata.mmo_data->symbol_position] = 0;
1347 if (j & MMO3_REGQUAL_BITS)
1349 if (j == MMO3_REGQUAL_BITS)
1351 sym_type = mmo_reg_sym;
1352 addr = mmo_get_byte (abfd);
1358 for (i = 0; i < j; i++)
1359 addr = (addr << 8) + mmo_get_byte (abfd);
1361 if (addr == 0 && j == MMO3_UNDEF)
1362 sym_type = mmo_undef_sym;
1364 sym_type = mmo_abs_sym;
1370 for (i = MMO3_DATA; i < j; i++)
1371 addr = (addr << 8) + mmo_get_byte (abfd);
1373 addr += (bfd_vma) 0x20 << 56;
1374 sym_type = mmo_data_sym;
1377 /* Get the serial number. */
1380 k = mmo_get_byte (abfd);
1381 serno = (serno << 7) + k;
1386 /* Got it. Now enter it. Skip a leading ":". */
1387 if (! abfd->tdata.mmo_data->have_error
1388 && ! mmo_create_symbol (abfd,
1389 abfd->tdata.mmo_data->lop_stab_symbol
1391 addr, sym_type, serno))
1392 abfd->tdata.mmo_data->have_error = TRUE;
1395 if (m & MMO3_MIDDLE)
1396 /* Traverse middle trie. */
1397 mmo_get_symbols (abfd);
1399 abfd->tdata.mmo_data->symbol_position--;
1403 /* Traverse right trie. */
1404 mmo_get_symbols (abfd);
1406 return ! abfd->tdata.mmo_data->have_error;
1409 /* Get the location of memory area [VMA..VMA + SIZE - 1], which we think
1410 is in section SEC. Adjust and reallocate zero-initialized contents.
1411 If there's new contents, allocate to the next multiple of
1412 MMO_SEC_CONTENTS_CHUNK_SIZE. */
1414 static INLINE bfd_byte *
1415 mmo_get_loc (asection *sec, bfd_vma vma, int size)
1417 bfd_size_type allocated_size;
1418 struct mmo_section_data_struct *sdatap = mmo_section_data (sec);
1419 struct mmo_data_list_struct *datap = sdatap->head;
1420 struct mmo_data_list_struct *entry;
1422 /* First search the list to see if we have the requested chunk in one
1423 piece, or perhaps if we have a suitable chunk with room to fit. */
1424 for (; datap != NULL; datap = datap->next)
1426 if (datap->where <= vma
1427 && datap->where + datap->size >= vma + size)
1428 return datap->data + vma - datap->where;
1429 else if (datap->where <= vma
1430 && datap->where + datap->allocated_size >= vma + size
1431 /* Only munch on the "allocated size" if it does not
1432 overlap the next chunk. */
1433 && (datap->next == NULL || datap->next->where >= vma + size))
1435 /* There was room allocated, but the size wasn't set to include
1437 datap->size += (vma + size) - (datap->where + datap->size);
1439 /* Update the section size. This happens only if we update the
1440 32-bit-aligned chunk size. Callers that have
1441 non-32-bit-aligned sections should do all allocation and
1442 size-setting by themselves or at least set the section size
1443 after the last allocating call to this function. */
1444 if (vma + size > sec->vma + sec->size)
1445 sec->size += (vma + size) - (sec->vma + sec->size);
1447 return datap->data + vma - datap->where;
1451 /* Not found; allocate a new block. First check in case we get a
1452 request for a size split up over several blocks; we'll have to return
1453 NULL for those cases, requesting the caller to split up the request.
1454 Requests with an address aligned on MMO_SEC_CONTENTS_CHUNK_SIZE bytes and
1455 for no more than MMO_SEC_CONTENTS_CHUNK_SIZE will always get resolved. */
1457 for (datap = sdatap->head; datap != NULL; datap = datap->next)
1458 if ((datap->where <= vma && datap->where + datap->size > vma)
1459 || (datap->where < vma + size
1460 && datap->where + datap->size >= vma + size))
1464 = (size + MMO_SEC_CONTENTS_CHUNK_SIZE - 1) & ~(MMO_SEC_CONTENTS_CHUNK_SIZE - 1);
1465 entry = (mmo_data_list_type *)
1466 bfd_zalloc (sec->owner, sizeof (mmo_data_list_type) + allocated_size);
1471 entry->allocated_size = allocated_size;
1473 datap = sdatap->head;
1475 /* Sort the records by address. Optimize for the common case of adding
1476 a record to the end of the list. */
1477 if (sdatap->tail != NULL && entry->where >= sdatap->tail->where)
1479 sdatap->tail->next = entry;
1481 sdatap->tail = entry;
1485 mmo_data_list_type **look;
1486 for (look = &sdatap->head;
1487 *look != NULL && (*look)->where < entry->where;
1488 look = &(*look)->next)
1490 entry->next = *look;
1492 if (entry->next == NULL)
1494 sdatap->tail = entry;
1496 /* We get here for the first time (at other times too) for this
1497 section. Say we have contents. */
1498 if (! bfd_set_section_flags (sec->owner, sec,
1499 bfd_get_section_flags (sec->owner, sec)
1500 | SEC_HAS_CONTENTS))
1505 /* Update the section size. This happens only when we add contents and
1506 re-size as we go. The section size will then be aligned to 32 bits. */
1507 if (vma + size > sec->vma + sec->size)
1508 sec->size += (vma + size) - (sec->vma + sec->size);
1512 /* Set sizes once we've read in all sections. */
1515 mmo_map_set_sizes (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
1516 void *ignored ATTRIBUTE_UNUSED)
1518 sec->lma = sec->vma;
1521 /* Read the mmo file and turn it into sections. */
1524 mmo_scan (bfd *abfd)
1527 unsigned int lineno = 1;
1528 bfd_boolean error = FALSE;
1530 asection *sec = bfd_make_section_old_way (abfd, MMO_TEXT_SECTION_NAME);
1531 asection *non_spec_sec = NULL;
1532 bfd_vma non_spec_vma = 0;
1533 char *current_filename = NULL;
1534 bfd_size_type nbytes_read = 0;
1535 /* Buffer with room to read a 64-bit value. */
1538 char *file_names[256];
1540 memset (file_names, 0, sizeof (file_names));
1542 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
1545 while ((nbytes_read = bfd_bread (buf, 4, abfd)) == 4)
1549 unsigned int y = bfd_get_8 (abfd, buf + 2);
1550 unsigned int z = bfd_get_8 (abfd, buf + 3);
1552 /* Change back to the original section for lopcodes other
1553 than LOP_QUOTE that comes after a LOP_SPEC. */
1554 if ((buf[1] != LOP_QUOTE || y != 0 || z != 1)
1555 && non_spec_sec != NULL)
1559 non_spec_sec = NULL;
1565 (*_bfd_error_handler)
1566 (_("%s: invalid mmo file: unsupported lopcode `%d'\n"),
1567 bfd_get_filename (abfd), buf[1]);
1568 bfd_set_error (bfd_error_bad_value);
1572 /* Quote the next 32-bit word. */
1573 if (y != 0 || z != 1)
1575 (*_bfd_error_handler)
1576 (_("%s: invalid mmo file: expected YZ = 1 got YZ = %d for lop_quote\n"),
1577 bfd_get_filename (abfd), y*256+z);
1578 bfd_set_error (bfd_error_bad_value);
1581 if (bfd_bread (buf, 4, abfd) != 4)
1584 mmo_xore_32 (sec, vma, bfd_get_32 (abfd, buf));
1591 /* Set vma (and section). */
1592 vma = (bfd_vma) y << 56;
1595 /* Get a 32-bit value. */
1596 if (bfd_bread (buf, 4, abfd) != 4)
1599 vma += bfd_get_32 (abfd, buf);
1603 /* Get a 64-bit value. */
1604 if (bfd_bread (buf, 8, abfd) != 8)
1607 vma += bfd_get_64 (abfd, buf);
1611 (*_bfd_error_handler)
1612 (_("%s: invalid mmo file: expected z = 1 or z = 2, got z = %d for lop_loc\n"),
1613 bfd_get_filename (abfd), z);
1614 bfd_set_error (bfd_error_bad_value);
1618 sec = mmo_decide_section (abfd, vma);
1624 /* Move forward within the same section. */
1627 sec = mmo_decide_section (abfd, vma);
1633 /* A fixup: Store the current vma somewhere. Position using
1634 same format as LOP_LOC. */
1636 bfd_vma p = (bfd_vma) y << 56;
1641 /* Get a 32-bit value. */
1642 if (bfd_bread (buf, 4, abfd) != 4)
1645 p += bfd_get_32 (abfd, buf);
1649 /* Get a 64-bit value. */
1650 if (bfd_bread (buf, 8, abfd) != 8)
1653 p += bfd_get_64 (abfd, buf);
1657 (*_bfd_error_handler)
1658 (_("%s: invalid mmo file: expected z = 1 or z = 2, got z = %d for lop_fixo\n"),
1659 bfd_get_filename (abfd), z);
1660 bfd_set_error (bfd_error_bad_value);
1664 /* The section where we store this address might be a
1665 different one than the current section. */
1666 fixosec = mmo_decide_section (abfd, p);
1667 if (fixosec == NULL)
1669 mmo_xore_64 (fixosec, p, vma);
1674 /* A fixup: Store YZ of this lopcode into YZ at vma - 4 * yz. */
1676 unsigned int yz = (y * 256 + z);
1677 bfd_vma p = vma + 2 - 4 * yz;
1678 asection *fixrsec = mmo_decide_section (abfd, p);
1679 if (fixrsec == NULL)
1681 mmo_xore_16 (fixrsec, p, yz);
1686 /* A fixup, similar to lop_fixr, but taking larger numbers
1687 and can change branches into the opposite direction
1696 (*_bfd_error_handler)
1697 (_("%s: invalid mmo file: expected y = 0, got y = %d for lop_fixrx\n"),
1698 bfd_get_filename (abfd), y);
1699 bfd_set_error (bfd_error_bad_value);
1703 if (z != 16 && z != 24)
1705 (*_bfd_error_handler)
1706 (_("%s: invalid mmo file: expected z = 16 or z = 24, got z = %d for lop_fixrx\n"),
1707 bfd_get_filename (abfd), z);
1708 bfd_set_error (bfd_error_bad_value);
1712 /* Get the next 32-bit value. */
1713 if (bfd_bread (buf, 4, abfd) != 4)
1716 delta = bfd_get_32 (abfd, buf);
1718 /* Do an, ehm, involved calculation for the location of
1719 the fixup. See mmixal documentation for a verbose
1720 explanation. We follow it verbosely here for the
1723 p = vma - 4 * delta;
1724 else if (buf[0] == 1)
1725 p = vma - 4 * ((delta & 0xffffff) - (1 << z));
1728 (*_bfd_error_handler)
1729 (_("%s: invalid mmo file: leading byte of operand word must be 0 or 1, got %d for lop_fixrx\n"),
1730 bfd_get_filename (abfd), buf[0]);
1731 bfd_set_error (bfd_error_bad_value);
1735 fixrsec = mmo_decide_section (abfd, vma);
1736 if (fixrsec == NULL)
1738 mmo_xore_32 (fixrsec, p, delta);
1743 /* Set current file and perhaps the file name. Reset line
1747 char *fname = bfd_malloc (z * 4 + 1);
1751 (*_bfd_error_handler)
1752 (_("%s: cannot allocate file name for file number %d, %d bytes\n"),
1753 bfd_get_filename (abfd), y, z * 4 + 1);
1754 bfd_set_error (bfd_error_system_call);
1760 for (i = 0; i < z; i++)
1762 if (bfd_bread (fname + i * 4, 4, abfd) != 4)
1769 if (file_names[y] != NULL)
1771 (*_bfd_error_handler)
1772 (_("%s: invalid mmo file: file number %d `%s',"
1773 " was already entered as `%s'\n"),
1774 bfd_get_filename (abfd), y, fname, file_names[y]);
1775 bfd_set_error (bfd_error_bad_value);
1779 file_names[y] = fname;
1782 if (file_names[y] == NULL)
1784 (*_bfd_error_handler)
1785 (_("%s: invalid mmo file: file name for number %d"
1786 " was not specified before use\n"),
1787 bfd_get_filename (abfd), y);
1788 bfd_set_error (bfd_error_bad_value);
1792 current_filename = file_names[y];
1797 /* Set line number. */
1798 lineno = y * 256 + z;
1799 /* FIXME: Create a sequence of mmo-specific line number
1800 entries for each section, then translate into canonical
1805 /* Special data follows until the next non-lop_quote
1809 sec = mmo_get_spec_section (abfd, y * 256 + z);
1818 /* We ignore header information, except we read in the
1819 creation time from the first 32-bit word with the time
1820 in seconds since era. */
1822 && bfd_bread (abfd->tdata.mmo_data->created, 4,
1826 for (i = 1; i < z; i++)
1827 if (bfd_bread (buf, 4, abfd) != 4)
1833 /* This tells of the contents of registers $Z..$255 at
1834 startup. We make a section out of it, with VMA = Z * 8,
1835 but only if Z != 255 or the contents is non-zero. */
1840 bfd_vma startaddr_octa;
1842 /* Read first octaword outside loop to simplify logic when
1843 excluding the Z == 255, octa == 0 case. */
1844 if (bfd_bread (buf, 8, abfd) != 8)
1847 first_octa = bfd_get_64 (abfd, buf);
1849 /* Don't emit contents for the trivial case which is
1850 always present; $255 pointing to Main. */
1854 = bfd_make_section_old_way (abfd,
1855 MMIX_REG_CONTENTS_SECTION_NAME);
1857 loc = mmo_get_loc (rsec, z * 8, (255 - z) * 8);
1858 bfd_put_64 (abfd, first_octa, loc);
1860 for (i = z + 1; i < 255; i++)
1862 if (bfd_bread (loc + (i - z) * 8, 8, abfd) != 8)
1866 /* Read out the last octabyte, and use it to set the
1868 if (bfd_bread (buf, 8, abfd) != 8)
1871 startaddr_octa = bfd_get_64 (abfd, buf);
1874 startaddr_octa = first_octa;
1876 if (! bfd_set_start_address (abfd, startaddr_octa))
1878 /* Currently this can't fail, but this should handle
1880 bfd_set_error (bfd_error_bad_value);
1887 /* We read in the symbols now, not later. */
1888 if (y != 0 || z != 0)
1890 (*_bfd_error_handler)
1891 (_("%s: invalid mmo file: fields y and z of lop_stab"
1892 " non-zero, y: %d, z: %d\n"),
1893 bfd_get_filename (abfd), y, z);
1894 bfd_set_error (bfd_error_bad_value);
1898 /* Save the location, so we can check that YZ in the LOP_END
1900 stab_loc = bfd_tell (abfd);
1902 /* It's not said that an MMO can be without symbols (though
1903 mmixal will refuse to assemble files without Main), but
1904 it seems it would still be a valid mmo-file, so allow it.
1905 We detect the absence of a symbol area in that the upper
1906 limit is computed (from the lop_end YZ field) as 0.
1907 Don't call mmo_get_symbols; it can only detect the end of
1908 a valid symbol trie, not the absence of one. */
1909 if (abfd->tdata.mmo_data->max_symbol_length != 0
1910 && ! mmo_get_symbols (abfd))
1916 /* This must be the last 32-bit word in an mmo file.
1918 struct stat statbuf;
1919 long curpos = bfd_tell (abfd);
1921 if (bfd_stat (abfd, &statbuf) < 0)
1924 if (statbuf.st_size != curpos)
1926 (*_bfd_error_handler)
1927 (_("%s: invalid mmo file: lop_end not last item in"
1929 bfd_get_filename (abfd));
1930 bfd_set_error (bfd_error_bad_value);
1934 /* Check that the YZ field is right. Subtract the size of
1935 this LOP_END in the calculation; YZ does not include
1937 if ((long) (y * 256 + z) * 4 != (curpos - stab_loc) - 4)
1939 (*_bfd_error_handler)
1940 (_("%s: invalid mmo file: YZ of lop_end (%ld)"
1941 " not equal to the number of tetras to the preceding"
1942 " lop_stab (%ld)\n"),
1943 bfd_get_filename (abfd), (long) (y * 256 + z),
1944 (curpos - stab_loc - 4)/4);
1945 bfd_set_error (bfd_error_bad_value);
1949 bfd_map_over_sections (abfd, mmo_map_set_sizes, NULL);
1956 /* This wasn't a lopcode, so store it in the current section. */
1957 mmo_xore_32 (sec, vma & ~3, bfd_get_32 (abfd, buf));
1964 /* We know this file is a multiple of four bytes (checked in
1965 mmo_object_p), so if we got something other than 0, this was a bad
1966 file (although it's more likely we'll get 0 in that case too).
1967 If we got end-of-file, then there was no lop_stab, so the file has
1970 if (nbytes_read != 0)
1971 bfd_set_error (bfd_error_system_call);
1973 bfd_set_error (bfd_error_bad_value);
1978 /* Mark the .text and .data section with their normal attribute if they
1979 contain anything. This is not redundant wrt. mmo_decide_section,
1980 since that code might never execute, and conversely the alloc+code
1981 section flags must be set then. */
1982 sec = bfd_get_section_by_name (abfd, MMO_TEXT_SECTION_NAME);
1984 && (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
1985 && ! bfd_set_section_flags (abfd, sec,
1986 bfd_get_section_flags (abfd, sec)
1987 | SEC_ALLOC | SEC_LOAD | SEC_CODE))
1990 sec = bfd_get_section_by_name (abfd, MMO_DATA_SECTION_NAME);
1992 && (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
1993 && ! bfd_set_section_flags (abfd, sec,
1994 bfd_get_section_flags (abfd, sec)
1995 | SEC_ALLOC | SEC_LOAD))
1998 /* Free whatever resources we took. */
1999 for (i = 0; i < sizeof (file_names) / sizeof (file_names[0]); i++)
2001 free (file_names[i]);
2005 /* A hook to set up object file dependent section information. For mmo,
2006 we point out the shape of allocated section contents. */
2009 mmo_new_section_hook (bfd *abfd ATTRIBUTE_UNUSED, asection *newsect)
2011 /* We zero-fill all fields and assume NULL is represented by an all
2012 zero-bit pattern. */
2013 newsect->used_by_bfd =
2014 bfd_zalloc (abfd, sizeof (struct mmo_section_data_struct));
2016 if (!newsect->used_by_bfd)
2019 /* Always align to at least 32-bit words. */
2020 newsect->alignment_power = 2;
2024 /* We already have section contents loaded for sections that have
2028 mmo_get_section_contents (bfd *abfd ATTRIBUTE_UNUSED,
2032 bfd_size_type bytes_to_do)
2034 /* Iterate over diminishing chunk sizes, copying contents, like
2035 mmo_set_section_contents. */
2038 /* A minor song-and-dance to make sure we're not bitten by the
2039 distant possibility of the cast from bfd_vma to int making the
2040 chunk zero-sized. */
2042 = (int) bytes_to_do != 0 ? bytes_to_do : MMO_SEC_CONTENTS_CHUNK_SIZE;
2046 loc = mmo_get_loc (sec, sec->vma + offset, chunk_size);
2047 while (loc == NULL && (chunk_size /= 2) != 0);
2049 if (chunk_size == 0)
2052 memcpy (location, loc, chunk_size);
2054 location += chunk_size;
2055 bytes_to_do -= chunk_size;
2056 offset += chunk_size;
2061 /* Return the amount of memory needed to read the symbol table. */
2064 mmo_get_symtab_upper_bound (bfd *abfd)
2066 return (abfd->symcount + 1) * sizeof (asymbol *);
2069 /* Sort mmo symbols by serial number. */
2072 mmo_sort_mmo_symbols (const void *arg1, const void *arg2)
2074 const struct mmo_symbol *sym1 = *(const struct mmo_symbol **) arg1;
2075 const struct mmo_symbol *sym2 = *(const struct mmo_symbol **) arg2;
2077 /* Sort by serial number first. */
2078 if (sym1->serno < sym2->serno)
2080 else if (sym1->serno > sym2->serno)
2083 /* Then sort by address of the table entries. */
2084 return ((const char *) arg1 - (const char *) arg2);
2087 /* Translate the symbol table. */
2090 mmo_canonicalize_symtab (bfd *abfd, asymbol **alocation)
2092 unsigned int symcount = bfd_get_symcount (abfd);
2096 csymbols = abfd->tdata.mmo_data->csymbols;
2097 if (csymbols == NULL)
2100 struct mmo_symbol *s;
2101 struct mmo_symbol **msp;
2103 /* First we store the symbols into the table we'll return, then we
2104 qsort it on the serial number, with secondary on the address of
2105 the symbol, to preserve order if there would be non-unique serial
2107 for (s = abfd->tdata.mmo_data->symbols,
2108 msp = (struct mmo_symbol **) alocation;
2115 qsort (alocation, symcount, sizeof (struct mmo_symbol *),
2116 mmo_sort_mmo_symbols);
2118 csymbols = (asymbol *) bfd_alloc (abfd, symcount * sizeof (asymbol));
2119 if (csymbols == NULL && symcount != 0)
2121 abfd->tdata.mmo_data->csymbols = csymbols;
2123 for (msp = (struct mmo_symbol **) alocation, c = csymbols;
2130 c->value = s->value;
2131 c->flags = BSF_GLOBAL;
2133 if (s->sym_type == mmo_data_sym)
2136 = bfd_get_section_by_name (abfd, MMO_DATA_SECTION_NAME);
2138 if (c->section == NULL)
2139 c->section = bfd_abs_section_ptr;
2141 c->value -= c->section->vma;
2143 else if (s->sym_type == mmo_undef_sym)
2144 c->section = bfd_und_section_ptr;
2145 else if (s->sym_type == mmo_reg_sym)
2148 = bfd_make_section_old_way (abfd, MMIX_REG_SECTION_NAME);
2153 = bfd_get_section_by_name (abfd, MMO_TEXT_SECTION_NAME);
2157 && c->value >= textsec->vma
2158 && c->value <= textsec->vma + textsec->size)
2160 c->section = textsec;
2161 c->value -= c->section->vma;
2163 /* In mmo, symbol types depend on the VMA. Therefore, if
2164 the data section isn't within the usual bounds, its
2165 symbols are marked as absolute. Correct that. This
2166 means we can't have absolute symbols with values matching
2167 data section addresses, but we also can't have with
2168 absolute symbols with values matching text section
2169 addresses. For such needs, use the ELF format. */
2171 = bfd_get_section_by_name (abfd,
2172 MMO_DATA_SECTION_NAME))
2174 && c->value >= datasec->vma
2175 && c->value <= datasec->vma + datasec->size)
2177 c->section = datasec;
2178 c->value -= c->section->vma;
2181 c->section = bfd_abs_section_ptr;
2188 /* Last, overwrite the incoming table with the right-type entries. */
2189 for (i = 0; i < symcount; i++)
2190 *alocation++ = csymbols++;
2196 /* Get information about a symbol. */
2199 mmo_get_symbol_info (bfd *ignore_abfd ATTRIBUTE_UNUSED,
2200 asymbol *symbol, symbol_info *ret)
2202 bfd_symbol_info (symbol, ret);
2206 mmo_print_symbol (bfd *abfd, void *afile, asymbol *symbol,
2207 bfd_print_symbol_type how)
2209 FILE *file = (FILE *) afile;
2213 case bfd_print_symbol_name:
2214 fprintf (file, "%s", symbol->name);
2217 bfd_print_symbol_vandf (abfd, file, symbol);
2219 fprintf (file, " %-5s %s",
2220 symbol->section->name,
2225 /* We can't map a file directly into executable code, so the
2226 size of header information is irrelevant. */
2229 mmo_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
2230 bfd_boolean exec ATTRIBUTE_UNUSED)
2235 /* Write the (section-neutral) file preamble. */
2238 mmo_internal_write_header (bfd *abfd)
2240 const char lop_pre_bfd[] = { LOP, LOP_PRE, 1, 1};
2242 if (bfd_bwrite (lop_pre_bfd, 4, abfd) != 4)
2245 /* Copy creation time of original file. */
2246 if (bfd_bwrite (abfd->tdata.mmo_data->created, 4, abfd) != 4)
2252 /* Write the LOP_POST record, with global register initializations.
2253 Z is the Z field of the LOP_POST, corresponding to 255 - number of
2254 registers at DATA. The Z = 255 field is filled in with the
2258 mmo_internal_write_post (bfd *abfd, int z, asection *sec)
2262 mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_POST << 16) | z);
2264 for (i = z; i < 255; i++)
2266 bfd_byte *data = mmo_get_loc (sec, i * 8, 8);
2268 if (bfd_bwrite (data, 8, abfd) != 8)
2272 /* For Z == $255, we always emit the start location; supposedly Main,
2273 but we have it handy at bfd_get_start_address. If we're called with
2274 Z == 255, don't assume DATA is valid. */
2275 bfd_put_64 (abfd, bfd_get_start_address (abfd), buf);
2277 return ! abfd->tdata.mmo_data->have_error && bfd_bwrite (buf, 8, abfd) == 8;
2280 /* Translate to and from BFD flags. This is to make sure that we don't
2281 get bitten by BFD flag number changes. */
2284 mmo_sec_flags_from_bfd_flags (flagword flags)
2286 flagword oflags = 0;
2288 if (flags & SEC_ALLOC)
2289 oflags |= MMO_SEC_ALLOC;
2290 if (flags & SEC_LOAD)
2291 oflags |= MMO_SEC_LOAD;
2292 if (flags & SEC_RELOC)
2293 oflags |= MMO_SEC_RELOC;
2294 if (flags & SEC_READONLY)
2295 oflags |= MMO_SEC_READONLY;
2296 if (flags & SEC_CODE)
2297 oflags |= MMO_SEC_CODE;
2298 if (flags & SEC_DATA)
2299 oflags |= MMO_SEC_DATA;
2300 if (flags & SEC_NEVER_LOAD)
2301 oflags |= MMO_SEC_NEVER_LOAD;
2302 if (flags & SEC_IS_COMMON)
2303 oflags |= MMO_SEC_IS_COMMON;
2304 if (flags & SEC_DEBUGGING)
2305 oflags |= MMO_SEC_DEBUGGING;
2311 bfd_sec_flags_from_mmo_flags (flagword flags)
2313 flagword oflags = 0;
2315 if (flags & MMO_SEC_ALLOC)
2316 oflags |= SEC_ALLOC;
2317 if (flags & MMO_SEC_LOAD)
2319 if (flags & MMO_SEC_RELOC)
2320 oflags |= SEC_RELOC;
2321 if (flags & MMO_SEC_READONLY)
2322 oflags |= SEC_READONLY;
2323 if (flags & MMO_SEC_CODE)
2325 if (flags & MMO_SEC_DATA)
2327 if (flags & MMO_SEC_NEVER_LOAD)
2328 oflags |= SEC_NEVER_LOAD;
2329 if (flags & MMO_SEC_IS_COMMON)
2330 oflags |= SEC_IS_COMMON;
2331 if (flags & MMO_SEC_DEBUGGING)
2332 oflags |= SEC_DEBUGGING;
2337 /* Return TRUE iff the leading or trailing tetrabyte in SEC is defined and
2341 mmo_has_leading_or_trailing_zero_tetra_p (bfd *abfd, asection *sec)
2343 bfd_vma secaddr = bfd_get_section_vma (abfd, sec);
2348 if (bfd_get_32 (abfd, mmo_get_loc (sec, secaddr, 4)) == 0
2349 && bfd_get_32 (abfd,
2350 mmo_get_loc (sec, secaddr + sec->size - 4, 4)) == 0)
2356 /* Write a section. */
2359 mmo_internal_write_section (bfd *abfd, asection *sec)
2361 /* We do it differently depending on what section this is:
2363 ".text": Output, prepended by information about the first source file
2364 (not yet implemented.)
2368 (".MMIX.reg_contents": Not handled here.)
2370 Anything else: Output inside a lop_spec 80, in the format described
2373 if (strcmp (sec->name, MMO_TEXT_SECTION_NAME) == 0)
2375 bfd_vma secaddr = bfd_get_section_vma (abfd, sec);
2377 /* Because leading and trailing zeros are omitted in output, we need to
2378 specify the section boundaries so they're correct when the file
2379 is read in again. That's also the case if this section is
2380 specified as not within its usual boundaries or alignments. */
2382 && (secaddr + sec->size >= (bfd_vma) 1 << 56
2383 || (secaddr & 3) != 0
2384 || (sec->size & 3) != 0
2385 || mmo_has_leading_or_trailing_zero_tetra_p (abfd, sec)))
2387 if (!mmo_write_section_description (abfd, sec))
2391 /* FIXME: Output source file name and line number. */
2392 return mmo_write_loc_chunk_list (abfd, mmo_section_data (sec)->head);
2394 else if (strcmp (sec->name, MMO_DATA_SECTION_NAME) == 0)
2396 bfd_vma secaddr = bfd_get_section_vma (abfd, sec);
2398 /* Same goes as for MMO_TEXT_SECTION_NAME above. */
2400 && (secaddr < (bfd_vma) 0x20 << 56
2401 || secaddr + sec->size >= (bfd_vma) 0x21 << 56
2402 || (secaddr & 3) != 0
2403 || (sec->size & 3) != 0
2404 || mmo_has_leading_or_trailing_zero_tetra_p (abfd, sec)))
2406 if (!mmo_write_section_description (abfd, sec))
2410 return mmo_write_loc_chunk_list (abfd, mmo_section_data (sec)->head);
2412 else if (strcmp (sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
2413 /* Not handled here. */
2415 /* This would normally be an abort call since this can't happen, but
2416 we don't do that. */
2417 bfd_set_error (bfd_error_bad_value);
2420 else if (strncmp (sec->name, MMIX_OTHER_SPEC_SECTION_PREFIX,
2421 strlen (MMIX_OTHER_SPEC_SECTION_PREFIX)) == 0)
2423 int n = atoi (sec->name + strlen (MMIX_OTHER_SPEC_SECTION_PREFIX));
2424 mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_SPEC << 16) | n);
2425 return (! abfd->tdata.mmo_data->have_error
2426 && mmo_write_chunk_list (abfd, mmo_section_data (sec)->head));
2428 /* Ignore sections that are just allocated or empty; we write out
2430 else if ((bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS) != 0
2433 if (!mmo_write_section_description (abfd, sec))
2436 /* Writing a LOP_LOC ends the LOP_SPEC data, and makes data actually
2438 if (bfd_get_section_flags (abfd, sec) & SEC_LOAD)
2439 return (! abfd->tdata.mmo_data->have_error
2440 && mmo_write_loc_chunk_list (abfd,
2441 mmo_section_data (sec)->head));
2442 return (! abfd->tdata.mmo_data->have_error
2443 && mmo_write_chunk_list (abfd, mmo_section_data (sec)->head));
2446 /* Some section without contents. */
2450 /* Write the description of a section, extended-mmo-style. */
2453 mmo_write_section_description (bfd *abfd, asection *sec)
2455 /* Keep the following document-comment formatted the way it is. */
2458 mmo section mapping, , Symbol-table, mmo
2462 The implementation in BFD uses special data type 80 (decimal) to
2463 encapsulate and describe named sections, containing e.g.@: debug
2464 information. If needed, any datum in the encapsulation will be
2465 quoted using lop_quote. First comes a 32-bit word holding the
2466 number of 32-bit words containing the zero-terminated zero-padded
2467 segment name. After the name there's a 32-bit word holding flags
2468 describing the section type. Then comes a 64-bit big-endian word
2469 with the section length (in bytes), then another with the section
2470 start address. Depending on the type of section, the contents
2471 might follow, zero-padded to 32-bit boundary. For a loadable
2472 section (such as data or code), the contents might follow at some
2473 later point, not necessarily immediately, as a lop_loc with the
2474 same start address as in the section description, followed by the
2475 contents. This in effect forms a descriptor that must be emitted
2476 before the actual contents. Sections described this way must not
2479 For areas that don't have such descriptors, synthetic sections are
2480 formed by BFD. Consecutive contents in the two memory areas
2481 @samp{0x0000@dots{}00} to @samp{0x01ff@dots{}ff} and
2482 @samp{0x2000@dots{}00} to @samp{0x20ff@dots{}ff} are entered in
2483 sections named <<.text>> and <<.data>> respectively. If an area
2484 is not otherwise described, but would together with a neighboring
2485 lower area be less than @samp{0x40000000} bytes long, it is joined
2486 with the lower area and the gap is zero-filled. For other cases,
2487 a new section is formed, named <<.MMIX.sec.@var{n}>>. Here,
2488 @var{n} is a number, a running count through the mmo file,
2492 A loadable section specified as:
2494 | .section secname,"ax"
2495 | TETRA 1,2,3,4,-1,-2009
2498 and linked to address @samp{0x4}, is represented by the sequence:
2500 | 0x98080050 - lop_spec 80
2501 | 0x00000002 - two 32-bit words for the section name
2502 | 0x7365636e - "secn"
2503 | 0x616d6500 - "ame\0"
2504 | 0x00000033 - flags CODE, READONLY, LOAD, ALLOC
2505 | 0x00000000 - high 32 bits of section length
2506 | 0x0000001c - section length is 28 bytes; 6 * 4 + 1 + alignment to 32 bits
2507 | 0x00000000 - high 32 bits of section address
2508 | 0x00000004 - section address is 4
2509 | 0x98010002 - 64 bits with address of following data
2510 | 0x00000000 - high 32 bits of address
2511 | 0x00000004 - low 32 bits: data starts at address 4
2517 | 0xfffff827 - -2009
2518 | 0x50000000 - 80 as a byte, padded with zeros.
2520 Note that the lop_spec wrapping does not include the section
2521 contents. Compare this to a non-loaded section specified as:
2524 | TETRA 200001,100002
2527 This, when linked to address @samp{0x200000000000001c}, is
2530 | 0x98080050 - lop_spec 80
2531 | 0x00000002 - two 32-bit words for the section name
2532 | 0x7365636e - "thir"
2533 | 0x616d6500 - "dsec"
2534 | 0x00000010 - flag READONLY
2535 | 0x00000000 - high 32 bits of section length
2536 | 0x0000000c - section length is 12 bytes; 2 * 4 + 2 + alignment to 32 bits
2537 | 0x20000000 - high 32 bits of address
2538 | 0x0000001c - low 32 bits of address 0x200000000000001c
2539 | 0x00030d41 - 200001
2540 | 0x000186a2 - 100002
2541 | 0x26280000 - 38, 40 as bytes, padded with zeros
2543 For the latter example, the section contents must not be
2544 loaded in memory, and is therefore specified as part of the
2545 special data. The address is usually unimportant but might
2546 provide information for e.g.@: the DWARF 2 debugging format. */
2548 mmo_write_tetra_raw (abfd, LOP_SPEC_SECTION);
2549 mmo_write_tetra (abfd, (strlen (sec->name) + 3) / 4);
2550 mmo_write_chunk (abfd, (bfd_byte *) sec->name, strlen (sec->name));
2551 mmo_flush_chunk (abfd);
2552 /* FIXME: We can get debug sections (.debug_line & Co.) with a section
2553 flag still having SEC_RELOC set. Investigate. This might be true
2554 for all alien sections; perhaps mmo.em should clear that flag. Might
2555 be related to weak references. */
2556 mmo_write_tetra (abfd,
2557 mmo_sec_flags_from_bfd_flags
2558 (bfd_get_section_flags (abfd, sec)));
2559 mmo_write_octa (abfd, sec->size);
2560 mmo_write_octa (abfd, bfd_get_section_vma (abfd, sec));
2564 /* We save up all data before output. */
2567 mmo_set_section_contents (bfd *abfd ATTRIBUTE_UNUSED, sec_ptr sec,
2568 const void *location, file_ptr offset,
2569 bfd_size_type bytes_to_do)
2571 /* Iterate over diminishing chunk sizes, copying contents. */
2574 /* A minor song-and-dance to make sure we're not bitten by the
2575 distant possibility of the cast from bfd_vma to int making the
2576 chunk zero-sized. */
2578 = (int) bytes_to_do != 0 ? bytes_to_do : MMO_SEC_CONTENTS_CHUNK_SIZE;
2582 loc = mmo_get_loc (sec, sec->vma + offset, chunk_size);
2583 while (loc == NULL && (chunk_size /= 2) != 0);
2585 if (chunk_size == 0)
2588 memcpy (loc, location, chunk_size);
2590 location += chunk_size;
2591 bytes_to_do -= chunk_size;
2592 offset += chunk_size;
2597 /* Add a symbol to a trie-tree. */
2600 mmo_internal_add_3_sym (bfd *abfd, struct mmo_symbol_trie *rootp,
2601 const struct mmo_symbol *symp)
2603 const char *name = symp->name;
2604 struct mmo_symbol_trie *trie = rootp;
2605 struct mmo_symbol_trie **triep = NULL;
2607 while (*name && trie != NULL)
2609 if (*name < trie->symchar)
2611 triep = &trie->left;
2614 else if (*name > trie->symchar)
2616 triep = &trie->right;
2619 else if (*name == trie->symchar)
2621 triep = &trie->middle;
2624 /* Make sure "trie" points to where we should fill in the
2625 current symbol whenever we've iterated through "name". We
2626 would lose the right position if we encounter "foobar" then
2629 trie = trie->middle;
2635 /* Create middle branches for the rest of the characters. */
2636 trie = bfd_zalloc (abfd, sizeof (struct mmo_symbol_trie));
2638 trie->symchar = *name++;
2639 triep = &trie->middle;
2642 /* We discover a duplicate symbol rather late in the process, but still;
2643 we discover it and bail out. */
2644 if (trie->sym.name != NULL)
2646 (*_bfd_error_handler)
2647 (_("%s: invalid symbol table: duplicate symbol `%s'\n"),
2648 bfd_get_filename (abfd), trie->sym.name);
2649 bfd_set_error (bfd_error_bad_value);
2653 memcpy (&trie->sym, symp, sizeof *symp);
2657 /* Find out the length of the serialized version of a trie in bytes. */
2660 mmo_internal_3_length (bfd *abfd, struct mmo_symbol_trie *trie)
2662 /* First, one for the control byte. */
2663 unsigned int length = 1;
2668 /* Add in the recursion to the left. */
2669 length += mmo_internal_3_length (abfd, trie->left);
2671 /* Add in the middle trie and the character. */
2672 length += 1 + mmo_internal_3_length (abfd, trie->middle);
2674 /* Add in the recursion to the right. */
2675 length += mmo_internal_3_length (abfd, trie->right);
2677 /* Add in bytes for the symbol (if this is an endnode). */
2678 if (trie->sym.name != NULL)
2680 unsigned int serno = trie->sym.serno;
2682 /* First what it takes to encode the value. */
2683 if (trie->sym.sym_type == mmo_reg_sym)
2685 else if (trie->sym.sym_type == mmo_undef_sym)
2689 bfd_vma value = trie->sym.value;
2691 /* Coded in one to eight following bytes. */
2692 if (trie->sym.sym_type == mmo_data_sym)
2693 value -= (bfd_vma) 0x20 << 56;
2703 /* Find out what it takes to encode the serial number. */
2715 /* Helper function for outputting the serial number of a symbol, output as
2716 a variant of leb128 (see dwarf2 documentation) which could be called
2717 beb128. Using a helper function and recursion simplifies debugging. */
2720 mmo_beb128_out (bfd *abfd, int serno, int marker)
2723 mmo_beb128_out (abfd, serno >> 7, 0);
2724 mmo_write_byte (abfd, marker | (serno & 0x7f));
2727 /* Serialize a trie. */
2730 mmo_internal_3_dump (bfd *abfd, struct mmo_symbol_trie *trie)
2732 bfd_byte control = 0;
2738 control |= MMO3_LEFT;
2741 control |= MMO3_MIDDLE;
2744 control |= MMO3_RIGHT;
2746 if (trie->sym.name != NULL)
2748 /* Encode the symbol type and length of value bytes. */
2749 if (trie->sym.sym_type == mmo_reg_sym)
2750 control |= MMO3_REGQUAL_BITS;
2751 else if (trie->sym.sym_type == mmo_undef_sym)
2752 control |= MMO3_UNDEF;
2755 bfd_vma value = trie->sym.value;
2757 /* Coded in 1..8 following bytes. */
2758 if (trie->sym.sym_type == mmo_data_sym)
2760 control |= MMO3_DATA;
2761 value -= (bfd_vma) 0x20 << 56;
2773 /* The control byte is output before recursing. */
2774 mmo_write_byte (abfd, control);
2776 mmo_internal_3_dump (abfd, trie->left);
2778 if (control & MMO3_SYMBITS)
2780 mmo_write_byte (abfd, trie->symchar);
2782 if (trie->sym.name != NULL)
2784 if (trie->sym.sym_type == mmo_reg_sym)
2785 mmo_write_byte (abfd, trie->sym.value);
2786 else if (trie->sym.sym_type == mmo_undef_sym)
2788 mmo_write_byte (abfd, 0);
2789 mmo_write_byte (abfd, 0);
2793 bfd_vma value = trie->sym.value;
2795 bfd_byte byte_n = control & 15;
2797 /* Coded in 1..8 following bytes. Note that the value is
2798 shifted out big-endian. */
2799 if (trie->sym.sym_type == mmo_data_sym)
2801 value -= (bfd_vma) 0x20 << 56;
2807 mmo_write_byte (abfd, (value >> ((byte_n - 1) * 8)) & 0xff);
2810 while (byte_n != 0);
2813 mmo_beb128_out (abfd, trie->sym.serno, 128);
2815 mmo_internal_3_dump (abfd, trie->middle);
2817 mmo_internal_3_dump (abfd, trie->right);
2820 /* Write symbols in mmo format. Also write the lop_end terminator. */
2823 mmo_write_symbols_and_terminator (bfd *abfd)
2825 int count = bfd_get_symcount (abfd);
2826 asymbol *maintable[2];
2828 asymbol **orig_table = bfd_get_outsymbols (abfd);
2830 struct mmo_symbol_trie root;
2835 /* Create a symbol for "Main". */
2836 asymbol *fakemain = bfd_make_empty_symbol (abfd);
2838 fakemain->flags = BSF_GLOBAL;
2839 fakemain->value = bfd_get_start_address (abfd);
2840 fakemain->name = MMIX_START_SYMBOL_NAME;
2841 fakemain->section = bfd_abs_section_ptr;
2842 maintable[0] = fakemain;
2843 maintable[1] = NULL;
2845 memset (&root, 0, sizeof (root));
2847 /* Make all symbols take a left turn. */
2848 root.symchar = 0xff;
2850 /* There must always be a ":Main", so we'll add one if there are no
2851 symbols. Make sure we have room for it. */
2852 table = bfd_alloc (abfd, (count + 1) * sizeof (asymbol *));
2856 memcpy (table, orig_table, count * sizeof (asymbol *));
2858 /* Move :Main (if there is one) to the first position. This is
2859 necessary to get the same layout of the trie-tree when linking as
2860 when objcopying the result as in the objcopy.exp test "simple objcopy
2861 of executable". It also automatically takes care of assigning serial
2862 number 1 to :Main (as is mandatory). */
2863 for (i = 0; i < count; i++)
2864 if (table[i] != NULL
2865 && strcmp (table[i]->name, MMIX_START_SYMBOL_NAME) == 0
2866 && (table[i]->flags & (BSF_DEBUGGING|BSF_GLOBAL)) == BSF_GLOBAL)
2868 asymbol *mainsym = table[i];
2869 memcpy (table + 1, orig_table, i * sizeof (asymbol *));
2872 /* Check that the value assigned to :Main is the same as the entry
2873 address. The default linker script asserts this. This is as
2874 good a place as any to check this consistency. */
2876 + mainsym->section->output_section->vma
2877 + mainsym->section->output_offset)
2878 != bfd_get_start_address (abfd))
2880 /* Arbitrary buffer to hold the printable representation of a
2883 char vmas_start[40];
2884 bfd_vma vma_start = bfd_get_start_address (abfd);
2886 sprintf_vma (vmas_main, mainsym->value);
2887 sprintf_vma (vmas_start, vma_start);
2889 (*_bfd_error_handler)
2890 (_("%s: Bad symbol definition: `Main' set to %s rather"
2891 " than the start address %s\n"),
2892 bfd_get_filename (abfd), vmas_main, vmas_start);
2893 bfd_set_error (bfd_error_bad_value);
2898 if (i == count && count != 0)
2900 /* When there are symbols, there must be a :Main. There was no
2901 :Main, so we need to add it manually. */
2902 memcpy (table + 1, orig_table, count * sizeof (asymbol *));
2903 table[0] = fakemain;
2907 for (i = 0, serno = 1; i < count && table[i] != NULL; i++)
2909 asymbol *s = table[i];
2911 /* It's not enough to consult bfd_is_local_label, since it does not
2912 mean "local" in the sense of linkable-and-observable-after-link.
2913 Let's just check the BSF_GLOBAL flag.
2915 Also, don't export symbols with characters not in the allowed set. */
2916 if ((s->flags & (BSF_DEBUGGING|BSF_GLOBAL)) == BSF_GLOBAL
2918 valid_mmo_symbol_character_set) == strlen (s->name))
2920 struct mmo_symbol sym;
2921 memset (&sym, 0, sizeof (sym));
2923 /* Need to strip const here; strdup:ing would leak and the
2924 existing string must be safe to reuse. */
2925 sym.name = (char *) s->name;
2928 + s->section->output_section->vma
2929 + s->section->output_offset;
2931 if (bfd_is_und_section (s->section))
2932 sym.sym_type = mmo_undef_sym;
2933 else if (strcmp (s->section->name, MMO_DATA_SECTION_NAME) == 0
2934 /* The encoding of data symbols require that the "rest"
2935 of the value fits in 6 bytes, so the upper two bytes
2936 must be 0x2000. All other symbols get to be the
2938 && (sym.value >> 48) == 0x2000)
2939 sym.sym_type = mmo_data_sym;
2940 else if (strcmp (s->section->name, MMIX_REG_SECTION_NAME) == 0)
2941 sym.sym_type = mmo_reg_sym;
2942 else if (strcmp (s->section->name,
2943 MMIX_REG_CONTENTS_SECTION_NAME) == 0)
2945 sym.sym_type = mmo_reg_sym;
2949 sym.sym_type = mmo_abs_sym;
2951 /* FIXME: We assume the order of the received symbols is an
2952 ordered mapping of the serial numbers. This is not
2953 necessarily true if we e.g. objcopy a mmo file to another and
2954 there are gaps in the numbering. Not sure if this can
2955 happen. Not sure what to do. */
2956 sym.serno = serno++;
2958 if (! mmo_internal_add_3_sym (abfd, &root, &sym))
2963 /* Change the root node to be a ":"-prefix. */
2965 root.middle = root.left;
2969 /* We have to find out if we can fit the whole symbol table in the mmo
2970 symtab. It would be bad to assume we can always fit it in 262144
2971 bytes. If we can't, just leave the Main symbol. */
2972 trie_len = (mmo_internal_3_length (abfd, &root) + 3)/4;
2974 if (trie_len > 0xffff)
2976 /* Test this code by using a lower limit in the test above and check
2977 that the single "Main" symbol is emitted and handled properly.
2978 There's no specific test-case. */
2979 struct mmo_symbol sym;
2981 (*_bfd_error_handler)
2982 (_("%s: warning: symbol table too large for mmo, larger than 65535"
2983 " 32-bit words: %d. Only `Main' will be emitted.\n"),
2984 bfd_get_filename (abfd), trie_len);
2986 memset (&sym, 0, sizeof (sym));
2987 sym.sym_type = mmo_abs_sym;
2988 sym.name = MMIX_START_SYMBOL_NAME;
2990 sym.value = bfd_get_start_address (abfd);
2992 /* Then patch up a symbol table to be just the ":Main" symbol. */
2993 memset (&root, 0, sizeof (root));
2994 root.left = root.middle;
2995 root.symchar = 0xff;
2999 if (! mmo_internal_add_3_sym (abfd, &root, &sym))
3003 root.middle = root.left;
3007 trie_len = (mmo_internal_3_length (abfd, &root) + 3)/4;
3010 /* Reset the written-bytes counter. */
3011 abfd->tdata.mmo_data->byte_no = 0;
3013 /* Put out the lop_stab mark. */
3014 bfd_put_32 (abfd, (LOP << 24) | (LOP_STAB << 16), buf);
3015 if (bfd_bwrite (buf, 4, abfd) != 4)
3018 /* Dump out symbols. */
3019 mmo_internal_3_dump (abfd, &root);
3021 if (trie_len != (abfd->tdata.mmo_data->byte_no + 3)/4)
3023 /* I haven't seen this trig. It seems no use claiming this case
3024 isn't debugged and abort if we get here. Instead emit a
3025 diagnostic and fail "normally". */
3026 (*_bfd_error_handler)
3027 (_("%s: internal error, symbol table changed size from %d to %d"
3029 bfd_get_filename (abfd), trie_len,
3030 (abfd->tdata.mmo_data->byte_no + 3)/4);
3031 bfd_set_error (bfd_error_bad_value);
3035 /* Dump out remaining bytes in the buffer and handle I/O errors by
3036 propagating errors. */
3037 if ((abfd->tdata.mmo_data->byte_no % 4) != 0
3038 || abfd->tdata.mmo_data->have_error)
3040 memset (abfd->tdata.mmo_data->buf + (abfd->tdata.mmo_data->byte_no % 4),
3041 0, 4 - (abfd->tdata.mmo_data->byte_no % 4));
3043 if (abfd->tdata.mmo_data->have_error
3044 || bfd_bwrite (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
3048 bfd_put_32 (abfd, (LOP << 24) | (LOP_END << 16) | trie_len, buf);
3049 return bfd_bwrite (buf, 4, abfd) == 4;
3052 /* Write section unless it is the register contents section. For that, we
3053 instead store the section in the supplied pointer. This function is
3054 used through bfd_map_over_sections. */
3057 mmo_write_section_unless_reg_contents (bfd *abfd, asection *sec, void *p)
3059 struct mmo_write_sec_info *infop = (struct mmo_write_sec_info *) p;
3061 if (! infop->retval)
3064 if (strcmp (sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
3066 infop->reg_section = sec;
3070 /* Exclude the convenience register section. */
3071 if (strcmp (sec->name, MMIX_REG_SECTION_NAME) == 0)
3073 if (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
3075 /* Make sure it hasn't got contents. It seems impossible to
3076 make it carry contents, so we don't have a test-case for
3078 (*_bfd_error_handler)
3079 (_("%s: internal error, internal register section %s had"
3081 bfd_get_filename (abfd), sec->name);
3082 bfd_set_error (bfd_error_bad_value);
3083 infop->retval = FALSE;
3090 infop->retval = mmo_internal_write_section (abfd, sec);
3093 /* Do the actual output of a file. Assumes mmo_set_section_contents is
3097 mmo_write_object_contents (bfd *abfd)
3099 struct mmo_write_sec_info wsecinfo;
3101 /* First, there are a few words of preamble. */
3102 if (! mmo_internal_write_header (abfd))
3105 wsecinfo.reg_section = NULL;
3106 wsecinfo.retval = TRUE;
3108 bfd_map_over_sections (abfd, mmo_write_section_unless_reg_contents,
3111 if (! wsecinfo.retval)
3114 if (wsecinfo.reg_section != NULL)
3116 asection *sec = wsecinfo.reg_section;
3117 unsigned int z = (unsigned int) (sec->vma / 8);
3119 /* Registers 0..31 must not be global. Do sanity check on the "vma"
3120 of the register contents section and check that it corresponds to
3121 the length of the section. */
3122 if (z < 32 || z >= 255 || (sec->vma & 7) != 0
3123 || sec->vma != 256 * 8 - sec->size - 8)
3125 bfd_set_error (bfd_error_bad_value);
3128 /* There must always be at least one such register. */
3129 (*_bfd_error_handler)
3130 (_("%s: no initialized registers; section length 0\n"),
3131 bfd_get_filename (abfd));
3132 else if (sec->vma > (256 - 32) * 8)
3133 /* Provide better error message for the case of too many
3134 global registers. */
3135 (*_bfd_error_handler)
3136 (_("%s: too many initialized registers; section length %ld\n"),
3137 bfd_get_filename (abfd),
3140 (*_bfd_error_handler)
3141 (_("%s: invalid start address for initialized registers of"
3142 " length %ld: 0x%lx%08lx\n"),
3143 bfd_get_filename (abfd),
3145 (unsigned long) (sec->vma >> 32), (unsigned long) (sec->vma));
3150 if (! mmo_internal_write_post (abfd, z, sec))
3154 if (! mmo_internal_write_post (abfd, 255, NULL))
3157 return mmo_write_symbols_and_terminator (abfd);
3160 /* Return the size of a NULL pointer, so we support linking in an mmo
3164 mmo_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
3165 asection *sec ATTRIBUTE_UNUSED)
3167 return sizeof (void *);
3170 /* Similarly canonicalize relocs to empty, filling in the terminating NULL
3174 mmo_canonicalize_reloc (bfd *abfd ATTRIBUTE_UNUSED,
3175 sec_ptr section ATTRIBUTE_UNUSED, arelent **relptr,
3176 asymbol **symbols ATTRIBUTE_UNUSED)
3182 /* If there's anything in particular in a mmo bfd that we want to free,
3183 make this a real function. Only do this if you see major memory
3184 thrashing; zealous free:ing will cause unwanted behavior, especially if
3185 you "free" memory allocated with "bfd_alloc", or even "bfd_release" a
3186 block allocated with "bfd_alloc"; they're really allocated from an
3187 obstack, and we don't know what was allocated there since this
3188 particular allocation. */
3190 #define mmo_close_and_cleanup _bfd_generic_close_and_cleanup
3191 #define mmo_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
3193 /* Perhaps we need to adjust this one; mmo labels (originally) without a
3194 leading ':' might more appropriately be called local. */
3195 #define mmo_bfd_is_local_label_name bfd_generic_is_local_label_name
3196 #define mmo_bfd_is_target_special_symbol \
3197 ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
3199 /* Is this one really used or defined by anyone? */
3200 #define mmo_get_lineno _bfd_nosymbols_get_lineno
3202 /* FIXME: We can do better on this one, if we have a dwarf2 .debug_line
3203 section or if MMO line numbers are implemented. */
3204 #define mmo_find_nearest_line _bfd_nosymbols_find_nearest_line
3205 #define mmo_make_empty_symbol _bfd_generic_make_empty_symbol
3206 #define mmo_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
3207 #define mmo_read_minisymbols _bfd_generic_read_minisymbols
3208 #define mmo_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
3210 #define mmo_get_section_contents_in_window \
3211 _bfd_generic_get_section_contents_in_window
3212 #define mmo_bfd_get_relocated_section_contents \
3213 bfd_generic_get_relocated_section_contents
3214 #define mmo_bfd_gc_sections bfd_generic_gc_sections
3215 #define mmo_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
3216 #define mmo_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
3217 #define mmo_bfd_link_add_symbols _bfd_generic_link_add_symbols
3218 #define mmo_bfd_link_just_syms _bfd_generic_link_just_syms
3219 #define mmo_bfd_final_link _bfd_generic_final_link
3220 #define mmo_bfd_link_split_section _bfd_generic_link_split_section
3222 /* Strictly speaking, only MMIX uses this restricted format, but let's not
3223 stop anybody from shooting themselves in the foot. */
3224 #define mmo_set_arch_mach bfd_default_set_arch_mach
3225 #define mmo_bfd_relax_section bfd_generic_relax_section
3226 #define mmo_bfd_merge_sections bfd_generic_merge_sections
3227 #define mmo_bfd_is_group_section bfd_generic_is_group_section
3228 #define mmo_bfd_discard_group bfd_generic_discard_group
3229 #define mmo_section_already_linked \
3230 _bfd_generic_section_already_linked
3232 /* objcopy will be upset if we return -1 from bfd_get_reloc_upper_bound by
3233 using BFD_JUMP_TABLE_RELOCS (_bfd_norelocs) rather than 0. FIXME: Most
3234 likely a bug in the _bfd_norelocs definition.
3236 On the other hand, we smuggle in an mmo object (because setting up ELF
3237 is too cumbersome) when linking (from other formats, presumably ELF) to
3238 represent the g255 entry. We need to link that object, so need to say
3239 it has no relocs. Upper bound for the size of the relocation table is
3240 the size of a NULL pointer, and we support "canonicalization" for that
3242 #define mmo_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
3244 /* We want to copy time of creation, otherwise we'd use
3245 BFD_JUMP_TABLE_COPY (_bfd_generic). */
3246 #define mmo_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
3247 #define mmo_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
3248 #define mmo_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
3249 #define mmo_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
3250 #define mmo_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
3251 #define mmo_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
3253 const bfd_target bfd_mmo_vec =
3256 bfd_target_mmo_flavour,
3257 BFD_ENDIAN_BIG, /* target byte order */
3258 BFD_ENDIAN_BIG, /* target headers byte order */
3260 /* FIXME: Might need adjustments. */
3261 (HAS_RELOC | EXEC_P | /* object flags */
3262 HAS_LINENO | HAS_DEBUG |
3263 HAS_SYMS | HAS_LOCALS | WP_TEXT),
3265 /* FIXME: Might need adjustments. */
3266 (SEC_CODE | SEC_DATA | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
3267 | SEC_READONLY | SEC_EXCLUDE | SEC_DEBUGGING | SEC_IN_MEMORY),
3269 0, /* leading underscore */
3270 ' ', /* ar_pad_char */
3271 16, /* ar_max_namelen */
3272 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3273 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3274 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
3275 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3276 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3277 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
3281 mmo_object_p, /* bfd_check_format */
3291 { /* bfd_write_contents */
3293 mmo_write_object_contents,
3298 BFD_JUMP_TABLE_GENERIC (mmo),
3299 BFD_JUMP_TABLE_COPY (mmo),
3300 BFD_JUMP_TABLE_CORE (_bfd_nocore),
3301 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
3302 BFD_JUMP_TABLE_SYMBOLS (mmo),
3303 /* We have to provide a valid method for getting relocs, returning zero,
3304 so we can't say BFD_JUMP_TABLE_RELOCS (_bfd_norelocs). */
3305 BFD_JUMP_TABLE_RELOCS (mmo),
3306 BFD_JUMP_TABLE_WRITE (mmo),
3307 BFD_JUMP_TABLE_LINK (mmo),
3308 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),