1 /* BFD back-end for mmo objects (MMIX-specific object-format).
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 Contents is entered as 32-bit words, xor:ed over previous
65 contents, always zero-initialized. A word that starts with the
66 byte @samp{0x98} forms a command called a @samp{lopcode}, where
67 the next byte distinguished between the thirteen lopcodes. The
68 two remaining bytes, called the @samp{Y} and @samp{Z} fields, or
69 the @samp{YZ} field (a 16-bit big-endian number), are used for
70 various purposes different for each lopcode. As documented in
71 @url{http://www-cs-faculty.stanford.edu/~knuth/mmixal-intro.ps.gz},
74 There is provision for specifying ``special data'' of 65536
75 different types. We use type 80 (decimal), arbitrarily chosen the
76 same as the ELF <<e_machine>> number for MMIX, filling it with
77 section information normally found in ELF objects. @xref{mmo
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. */
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 /* These structures are used in bfd_map_over_sections constructs. */
340 /* Used when writing out sections; all but the register contents section
341 which is stored in reg_section. */
342 struct mmo_write_sec_info
344 asection *reg_section;
348 /* Used when trying to find a section corresponding to addr. */
349 struct mmo_find_sec_info
355 static boolean mmo_bfd_copy_private_bfd_data PARAMS ((bfd *, bfd *));
356 static void mmo_write_section_unless_reg_contents
357 PARAMS ((bfd *, asection *, PTR));
358 static void mmo_find_sec_w_addr PARAMS ((bfd *, asection *, PTR));
359 static void mmo_find_sec_w_addr_grow PARAMS ((bfd *, asection *, PTR));
360 static asection *mmo_make_section PARAMS ((bfd *, CONST char *));
361 static void mmo_get_symbol_info PARAMS ((bfd *, asymbol *, symbol_info *));
362 static void mmo_print_symbol
363 PARAMS ((bfd *, PTR, asymbol *, bfd_print_symbol_type));
364 static void mmo_init PARAMS ((void));
365 static boolean mmo_mkobject PARAMS ((bfd *));
366 static boolean mmo_scan PARAMS ((bfd *));
367 static asection *mmo_decide_section PARAMS ((bfd *, bfd_vma));
368 static asection *mmo_get_generic_spec_data_section PARAMS ((bfd *, int));
369 static asection *mmo_get_spec_section PARAMS ((bfd *, int));
370 static INLINE bfd_byte *mmo_get_loc PARAMS ((asection *, bfd_vma, int));
371 static void mmo_xore_64 PARAMS ((asection *, bfd_vma vma, bfd_vma value));
372 static void mmo_xore_32 PARAMS ((asection *, bfd_vma vma, unsigned int));
373 static void mmo_xore_16 PARAMS ((asection *, bfd_vma vma, unsigned int));
374 static CONST bfd_target *mmo_object_p PARAMS ((bfd *));
375 static void mmo_map_set_sizes PARAMS ((bfd *, asection *, PTR));
376 static boolean mmo_get_symbols PARAMS ((bfd *));
377 static boolean mmo_create_symbol PARAMS ((bfd *, CONST char *, bfd_vma,
378 enum mmo_sym_type, unsigned int));
379 static boolean mmo_get_section_contents
380 PARAMS ((bfd *, asection *, PTR, file_ptr, bfd_size_type));
381 static long mmo_get_symtab_upper_bound PARAMS ((bfd *));
382 static long mmo_get_symtab PARAMS ((bfd *, asymbol **));
383 static void mmo_get_symbol_info PARAMS ((bfd *, asymbol *, symbol_info *));
384 static void mmo_print_symbol PARAMS ((bfd *, PTR, asymbol *,
385 bfd_print_symbol_type));
386 static boolean mmo_set_section_contents
387 PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type));
388 static int mmo_sizeof_headers PARAMS ((bfd *, boolean));
389 static long mmo_get_reloc_upper_bound PARAMS ((bfd *, asection *));
391 static boolean mmo_internal_write_header PARAMS ((bfd *));
392 static boolean mmo_internal_write_post PARAMS ((bfd *, int, asection *));
393 static boolean mmo_internal_add_3_sym
394 PARAMS ((bfd *, struct mmo_symbol_trie *, CONST struct mmo_symbol *));
395 static unsigned int mmo_internal_3_length
396 PARAMS ((bfd *, struct mmo_symbol_trie *));
397 static void mmo_internal_3_dump
398 PARAMS ((bfd *, struct mmo_symbol_trie *));
399 static void mmo_beb128_out PARAMS ((bfd *, int, int));
400 static boolean mmo_internal_write_section
401 PARAMS ((bfd *, asection *));
402 static void mmo_write_tetra PARAMS ((bfd *, unsigned int));
403 static void mmo_write_tetra_raw PARAMS ((bfd *, unsigned int));
404 static void mmo_write_octa PARAMS ((bfd *, bfd_vma));
405 static void mmo_write_octa_raw PARAMS ((bfd *, bfd_vma));
406 static boolean mmo_write_chunk
407 PARAMS ((bfd *, CONST bfd_byte *, unsigned int));
408 static boolean mmo_write_loc_chunk
409 PARAMS ((bfd *, bfd_vma, CONST bfd_byte *, unsigned int));
410 static boolean mmo_write_chunk_list PARAMS ((bfd *, mmo_data_list_type *));
411 static boolean mmo_write_loc_chunk_list
412 PARAMS ((bfd *, mmo_data_list_type *));
413 static boolean mmo_write_symbols_and_terminator PARAMS ((bfd *));
414 static flagword mmo_sec_flags_from_bfd_flags PARAMS ((flagword));
415 static flagword bfd_sec_flags_from_mmo_flags PARAMS ((flagword));
416 static bfd_byte mmo_get_byte PARAMS ((bfd *));
417 static void mmo_write_byte PARAMS ((bfd *, bfd_byte));
418 static boolean mmo_new_section_hook PARAMS ((bfd *, asection *));
419 static int mmo_sort_mmo_symbols PARAMS ((CONST PTR, CONST PTR));
420 static boolean mmo_write_object_contents PARAMS ((bfd *));
421 static long mmo_canonicalize_reloc
422 PARAMS ((bfd *, sec_ptr, arelent **, asymbol **));
424 /* Global "const" variables initialized once. Must not depend on
425 particular input or caller; put such things into the bfd or elsewhere.
426 Look ma, no static per-invocation data! */
429 char valid_mmo_symbol_character_set[/* A-Z a-z (we assume consecutive
430 codes; sorry EBCDIC:ers!). */
431 + 'Z' - 'A' + 1 + 'z' - 'a' + 1
436 /* Codes higher than 126. */
442 /* Get section SECNAME or create one if it doesn't exist. When creating
443 one, new memory for the name is allocated. */
446 mmo_make_section (abfd, secname)
450 asection *sec = bfd_get_section_by_name (abfd, secname);
454 char *newsecname = strdup (secname);
456 if (newsecname == NULL)
458 (*_bfd_error_handler)
459 (_("%s: No core to allocate section name %s\n"),
460 bfd_get_filename (abfd), secname);
461 bfd_set_error (bfd_error_system_call);
464 sec = bfd_make_section (abfd, newsecname);
470 /* Nothing to do, but keep as a placeholder if we need it.
471 Note that state that might differ between bfd:s must not be initialized
472 here, nor must it be static. Add it to tdata information instead. */
477 static boolean inited = false;
480 static const char letters[]
481 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:_";
487 /* Fill in the set of valid symbol characters. */
488 strcpy (valid_mmo_symbol_character_set, letters);
489 i = strlen (letters);
491 for (j = 126; j < 256; j++)
492 valid_mmo_symbol_character_set[i++] = j;
495 /* Check whether an existing file is an mmo file. */
497 static CONST bfd_target *
506 if (bfd_stat (abfd, &statbuf) < 0
507 || bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
508 || bfd_bread (b, 4, abfd) != 4)
511 /* All mmo files are a multiple of four bytes long.
512 Only recognize version one. */
513 if ((statbuf.st_size % 4) != 0
514 || b[0] != LOP || b[1] != LOP_PRE || b[2] != 1)
517 /* Get the last 32-bit word. */
518 if (bfd_seek (abfd, (file_ptr) statbuf.st_size - 4, SEEK_SET) != 0
519 || bfd_bread (b, 4, abfd) != 4)
522 /* Check if the file ends in a lop_end lopcode. */
523 if (b[0] != LOP || b[1] != LOP_END || ! mmo_mkobject (abfd))
526 /* Compute an upper bound on the max symbol length. Not really
527 important as all of the symbol information can only be 256k. */
528 abfd->tdata.mmo_data->max_symbol_length = (b[2] * 256 + b[3]) * 4;
529 abfd->tdata.mmo_data->lop_stab_symbol
530 = bfd_malloc (abfd->tdata.mmo_data->max_symbol_length + 1);
532 if (abfd->tdata.mmo_data->lop_stab_symbol == NULL)
534 (*_bfd_error_handler)
535 (_("%s: No core to allocate a symbol %d bytes long\n"),
536 bfd_get_filename (abfd), abfd->tdata.mmo_data->max_symbol_length);
540 /* Read in everything. */
541 if (! mmo_scan (abfd))
542 goto bad_format_free;
544 if (abfd->symcount > 0)
545 abfd->flags |= HAS_SYMS;
547 /* You'll have to tweak this if you want to use this format for other
548 arches (not recommended due to its small-size limitations). Look at
549 the ELF format for how to make it target-generic. */
550 if (! bfd_default_set_arch_mach (abfd, bfd_arch_mmix, 0))
551 goto bad_format_free;
556 free (abfd->tdata.mmo_data->lop_stab_symbol);
558 bfd_set_error (bfd_error_wrong_format);
563 /* Set up the mmo tdata information. */
571 if (abfd->tdata.mmo_data == NULL)
575 /* All fields are zero-initialized, so we don't have to explicitly
577 tdata_type *tdata = (tdata_type *) bfd_zmalloc (sizeof (tdata_type));
581 created = time (NULL);
582 bfd_put_32 (abfd, created, tdata->created);
584 abfd->tdata.mmo_data = tdata;
591 mmo_bfd_copy_private_bfd_data (ibfd, obfd)
595 if (bfd_get_flavour (ibfd) != bfd_target_mmo_flavour
596 || bfd_get_flavour (obfd) != bfd_target_mmo_flavour)
599 /* Copy the time the copied-from file was created. If people want the
600 time the file was last *modified*, they have that in the normal file
602 memcpy (obfd->tdata.mmo_data->created, ibfd->tdata.mmo_data->created,
603 sizeof (obfd->tdata.mmo_data->created));
607 /* Helper functions for mmo_decide_section, used through
608 bfd_map_over_sections. */
611 mmo_find_sec_w_addr (abfd, sec, p)
612 bfd *abfd ATTRIBUTE_UNUSED;
616 struct mmo_find_sec_info *infop = (struct mmo_find_sec_info *) p;
617 bfd_vma vma = bfd_get_section_vma (abfd, sec);
619 /* Ignore sections that aren't loaded. */
620 if ((bfd_get_section_flags (abfd, sec) & (SEC_LOAD | SEC_ALLOC))
621 != (SEC_LOAD | SEC_ALLOC))
624 if (infop->addr >= vma && infop->addr < vma + sec->_raw_size)
629 mmo_find_sec_w_addr_grow (abfd, sec, p)
630 bfd *abfd ATTRIBUTE_UNUSED;
634 struct mmo_find_sec_info *infop = (struct mmo_find_sec_info *) p;
635 bfd_vma vma = bfd_get_section_vma (abfd, sec);
637 /* Ignore sections that aren't loaded. */
638 if ((bfd_get_section_flags (abfd, sec) & (SEC_LOAD | SEC_ALLOC))
639 != (SEC_LOAD | SEC_ALLOC))
642 if (infop->addr >= vma && infop->addr < vma + MAX_ARTIFICIAL_SECTION_SIZE)
646 /* Find a section that corresponds to a VMA. Automatically create .text
647 or .data and set current section to it, depending on what vma. If we
648 can't deduce a section, make one up as ".MMIX.sec.N", where N is an
649 increasing number. */
652 mmo_decide_section (abfd, vma)
656 asection *sec = NULL;
657 char sec_name[sizeof (".MMIX.sec.") + 20];
658 struct mmo_find_sec_info info;
663 /* First see if there's a section that would match exactly. */
664 bfd_map_over_sections (abfd, mmo_find_sec_w_addr, &info);
666 if (info.sec != NULL)
669 /* If there's no such section, try and expand one of the existing ones,
670 up to a limit. Make sure we have .text and .data before we try that;
671 create them corresponding to expected addresses and set flags to make
672 them match the "loaded and with contents" expectation. */
673 if ((vma >> 56) == 0)
675 sec = bfd_make_section_old_way (abfd, MMO_TEXT_SECTION_NAME);
680 if (! sec->user_set_vma)
681 bfd_set_section_vma (abfd, sec, vma);
682 if (! bfd_set_section_flags (abfd, sec,
683 bfd_get_section_flags (abfd, sec)
684 | SEC_CODE | SEC_LOAD | SEC_ALLOC))
687 else if ((vma >> 56) == 0x20)
689 sec = bfd_make_section_old_way (abfd, MMO_DATA_SECTION_NAME);
694 if (! sec->user_set_vma)
695 bfd_set_section_vma (abfd, sec, vma);
696 if (! bfd_set_section_flags (abfd, sec,
697 bfd_get_section_flags (abfd, sec)
698 | SEC_LOAD | SEC_ALLOC))
702 bfd_map_over_sections (abfd, mmo_find_sec_w_addr_grow, &info);
704 if (info.sec != NULL)
707 /* If there's still no suitable section, make a new one. */
708 sprintf (sec_name, ".MMIX.sec.%d", abfd->tdata.mmo_data->sec_no++);
709 sec = mmo_make_section (abfd, sec_name);
710 if (! sec->user_set_vma)
711 bfd_set_section_vma (abfd, sec, vma);
713 if (! bfd_set_section_flags (abfd, sec,
714 bfd_get_section_flags (abfd, sec)
715 | SEC_LOAD | SEC_ALLOC))
720 /* Xor in a 64-bit value VALUE at VMA. */
723 mmo_xore_64 (sec, vma, value)
728 bfd_byte *loc = mmo_get_loc (sec, vma, 8);
729 bfd_vma prev = bfd_get_64 (sec->owner, loc);
732 bfd_put_64 (sec->owner, value, loc);
735 /* Xor in a 32-bit value VALUE at VMA. */
738 mmo_xore_32 (sec, vma, value)
743 bfd_byte *loc = mmo_get_loc (sec, vma, 4);
744 unsigned int prev = bfd_get_32 (sec->owner, loc);
747 bfd_put_32 (sec->owner, value, loc);
750 /* Xor in a 16-bit value VALUE at VMA. */
753 mmo_xore_16 (sec, vma, value)
758 bfd_byte *loc = mmo_get_loc (sec, vma, 2);
759 unsigned int prev = bfd_get_16 (sec->owner, loc);
762 bfd_put_16 (sec->owner, value, loc);
765 /* Write a 32-bit word to output file, no lop_quote generated. */
768 mmo_write_tetra_raw (abfd, value)
774 bfd_put_32 (abfd, value, buf);
776 if (bfd_bwrite ((PTR) buf, 4, abfd) != 4)
777 abfd->tdata.mmo_data->have_error = true;
780 /* Write a 32-bit word to output file; lop_quote if necessary. */
783 mmo_write_tetra (abfd, value)
787 if (((value >> 24) & 0xff) == LOP)
788 mmo_write_tetra_raw (abfd, LOP_QUOTE_NEXT);
790 mmo_write_tetra_raw (abfd, value);
793 /* Write a 64-bit word to output file, perhaps with lop_quoting. */
796 mmo_write_octa (abfd, value)
800 mmo_write_tetra (abfd, (unsigned int) (value >> 32));
801 mmo_write_tetra (abfd, (unsigned int) value);
804 /* Write a 64-bit word to output file, without lop_quoting. */
807 mmo_write_octa_raw (abfd, value)
811 mmo_write_tetra_raw (abfd, (unsigned int) (value >> 32));
812 mmo_write_tetra_raw (abfd, (unsigned int) value);
815 /* Write quoted contents, zero filled. */
817 static INLINE boolean
818 mmo_write_chunk (abfd, loc, len)
823 boolean retval = true;
824 bfd_byte buf[4] = {0, 0, 0, 0};
829 mmo_write_tetra_raw (abfd, LOP_QUOTE_NEXT);
833 && abfd->tdata.mmo_data->have_error == false
834 && 4 == bfd_bwrite ((PTR) loc, 4, abfd));
842 memcpy (buf, loc, len);
844 mmo_write_tetra_raw (abfd, LOP_QUOTE_NEXT);
848 && abfd->tdata.mmo_data->have_error == false
849 && 4 == bfd_bwrite ((PTR) buf, 4, abfd));
855 /* Same, but from a list. */
857 static INLINE boolean
858 mmo_write_chunk_list (abfd, datap)
860 mmo_data_list_type *datap;
862 for (; datap != NULL; datap = datap->next)
863 if (! mmo_write_chunk (abfd, datap->data, datap->size))
869 /* Write a lop_loc and some contents. */
872 mmo_write_loc_chunk (abfd, vma, loc, len)
878 /* We always write the location as 64 bits; no use saving bytes here. */
879 mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_LOC << 16) | 2);
881 /* Find an initial and trailing section of zero tetras; we don't need to
882 write out zeros. FIXME: When we do this, we should emit section size
883 and address specifiers, else objcopy can't perform a unity
885 while (len >= 4 && bfd_get_32 (abfd, loc) == 0)
892 while (len >= 4 && bfd_get_32 (abfd, loc + len - 4) == 0)
895 mmo_write_octa_raw (abfd, vma);
897 abfd->tdata.mmo_data->have_error == false
898 && mmo_write_chunk (abfd, loc, len);
901 /* Same, but from a list. */
903 static INLINE boolean
904 mmo_write_loc_chunk_list (abfd, datap)
906 mmo_data_list_type *datap;
908 for (; datap != NULL; datap = datap->next)
909 if (! mmo_write_loc_chunk (abfd, datap->where, datap->data, datap->size))
915 /* Make a .MMIX.spec_data.N section. */
918 mmo_get_generic_spec_data_section (abfd, spec_data_number)
920 int spec_data_number;
923 char secname[sizeof (MMIX_OTHER_SPEC_SECTION_PREFIX) + 20]
924 = MMIX_OTHER_SPEC_SECTION_PREFIX;
926 sprintf (secname + strlen (MMIX_OTHER_SPEC_SECTION_PREFIX),
927 "%d", spec_data_number);
929 sec = mmo_make_section (abfd, secname);
934 /* Make a special section for SPEC_DATA_NUMBER. If it is the one we use
935 ourselves, parse some of its data to get at the section name. */
938 mmo_get_spec_section (abfd, spec_data_number)
940 int spec_data_number;
945 unsigned int secname_length;
947 bfd_vma section_length;
949 mmo_data_list_type *loc;
953 /* If this isn't the "special" special data, then make a placeholder
955 if (spec_data_number != SPEC_DATA_SECTION)
956 return mmo_get_generic_spec_data_section (abfd, spec_data_number);
958 /* Seek back to this position if there was a format error. */
959 orig_pos = bfd_tell (abfd);
961 /* Read the length (in 32-bit words). */
962 if (bfd_bread (buf, 4, abfd) != 4)
967 if (buf[1] != LOP_QUOTE)
970 if (bfd_bread (buf, 4, abfd) != 4)
974 /* We don't care to keep the name length accurate. It's
976 secname_length = bfd_get_32 (abfd, buf) * 4;
978 /* Check section name length for sanity. */
979 if (secname_length > MAX_SECTION_NAME_SIZE)
982 /* This should be free'd regardless if a section is created. */
983 secname = bfd_malloc (secname_length + 1);
984 secname[secname_length] = 0;
986 for (i = 0; i < secname_length / 4; i++)
988 if (bfd_bread (secname + i * 4, 4, abfd) != 4)
989 goto format_error_free;
991 if (secname[i * 4] == LOP)
993 /* A bit of overkill, but we handle char 0x98 in a section name,
994 and recognize misparsing. */
995 if (secname[i * 4 + 1] != LOP_QUOTE
996 || bfd_bread (secname + i * 4, 4, abfd) != 4)
997 /* Whoops. We thought this was a name, and now we found a
998 non-lop_quote lopcode before we parsed the whole length of
999 the name. Signal end-of-file in the same manner. */
1000 goto format_error_free;
1004 /* Get the section flags. */
1005 if (bfd_bread (buf, 4, abfd) != 4
1007 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1008 goto format_error_free;
1010 flags = bfd_get_32 (abfd, buf);
1012 /* Get the section length. */
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 section_length = (bfd_vma) bfd_get_32 (abfd, buf) << 32;
1020 /* That's the first, high-part. Now get the low part. */
1022 if (bfd_bread (buf, 4, abfd) != 4
1024 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1025 goto format_error_free;
1027 section_length |= (bfd_vma) bfd_get_32 (abfd, buf);
1029 /* Check the section length for sanity. */
1030 if (section_length > MAX_ARTIFICIAL_SECTION_SIZE)
1031 goto format_error_free;
1033 /* Get the section VMA. */
1034 if (bfd_bread (buf, 4, abfd) != 4
1036 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1037 goto format_error_free;
1039 section_vma = (bfd_vma) bfd_get_32 (abfd, buf) << 32;
1041 /* That's the first, high-part. Now get the low part. */
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);
1049 sec = mmo_make_section (abfd, secname);
1054 /* We allocate a buffer here for the advertised size, with head room for
1055 tetrabyte alignment. */
1056 loc = bfd_zmalloc (section_length + 3
1057 + sizeof (struct mmo_data_list_struct));
1061 /* Use a TETRA-rounded size for the allocated buffer; we set the
1062 "visible" section size below. */
1063 loc->size = (section_length + 3) & ~3;
1065 /* Add in the section flags we found to those bfd entered during this
1066 process and set the contents. */
1067 if (! bfd_set_section_flags (abfd, sec,
1068 bfd_sec_flags_from_mmo_flags (flags)
1069 | bfd_get_section_flags (abfd, sec)
1070 | (section_length != 0 ? SEC_HAS_CONTENTS : 0))
1071 || ! bfd_set_section_size (abfd, sec,
1072 sec->_cooked_size + section_length)
1073 /* Set VMA only for the first occurrence. */
1074 || (! sec->user_set_vma
1075 && ! bfd_set_section_vma (abfd, sec, section_vma)))
1077 /* If we get an error for any of the calls above, signal more than
1078 just a format error for the spec section. */
1083 if (((struct mmo_section_data_struct *) (sec->used_by_bfd))->tail != NULL)
1084 ((struct mmo_section_data_struct *) (sec->used_by_bfd))->tail->next
1087 ((struct mmo_section_data_struct *) (sec->used_by_bfd))->head = loc;
1088 ((struct mmo_section_data_struct *) (sec->used_by_bfd))->tail = loc;
1089 loc->where = section_vma;
1096 if (bfd_seek (abfd, orig_pos, SEEK_SET) != 0)
1099 return mmo_get_generic_spec_data_section (abfd, spec_data_number);
1102 /* Read a byte, but read from file in multiples of 32-bit words. */
1110 if (abfd->tdata.mmo_data->byte_no == 0)
1112 if (abfd->tdata.mmo_data->have_error == false
1113 && bfd_bread (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
1115 abfd->tdata.mmo_data->have_error = true;
1117 /* A value somewhat safe against tripping on some inconsistency
1118 when mopping up after this error. */
1123 retval = abfd->tdata.mmo_data->buf[abfd->tdata.mmo_data->byte_no];
1124 abfd->tdata.mmo_data->byte_no = (abfd->tdata.mmo_data->byte_no + 1) % 4;
1129 /* Write a byte, in multiples of 32-bit words. */
1132 mmo_write_byte (abfd, value)
1136 abfd->tdata.mmo_data->buf[(abfd->tdata.mmo_data->byte_no++ % 4)] = value;
1137 if ((abfd->tdata.mmo_data->byte_no % 4) == 0)
1139 if (abfd->tdata.mmo_data->have_error == false
1140 && bfd_bwrite (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
1141 abfd->tdata.mmo_data->have_error = true;
1145 /* Create a symbol. */
1148 mmo_create_symbol (abfd, symname, addr, sym_type, serno)
1150 CONST char *symname;
1152 enum mmo_sym_type sym_type;
1155 struct mmo_symbol *n;
1157 n = (struct mmo_symbol *) bfd_alloc (abfd, sizeof (struct mmo_symbol));
1161 n->name = bfd_alloc (abfd, strlen (symname) + 1);
1162 if (n->name == NULL)
1165 strcpy ((PTR) n->name, symname);
1168 n->sym_type = sym_type;
1171 if (abfd->tdata.mmo_data->symbols == NULL)
1172 abfd->tdata.mmo_data->symbols = n;
1174 abfd->tdata.mmo_data->symtail->next = n;
1175 abfd->tdata.mmo_data->symtail = n;
1180 /* Check that :Main equals the last octa of the .MMIX.reg_contents
1181 section, as it's the one place we're sure to pass when reading a mmo
1182 object. For written objects, we do it while setting the symbol
1184 if (strcmp (symname, MMIX_START_SYMBOL_NAME) == 0
1185 && bfd_get_start_address (abfd) != addr)
1187 (*_bfd_error_handler)
1188 (_("%s: invalid mmo file: initialization value for $255 is not `Main'\n"),
1189 bfd_get_filename (abfd));
1190 bfd_set_error (bfd_error_bad_value);
1197 /* Read in symbols. */
1200 mmo_get_symbols (abfd)
1205 Symbol-table, mmo section mapping, File layout, mmo
1209 From mmixal.w (or really, the generated mmixal.tex) in
1210 @url{http://www-cs-faculty.stanford.edu/~knuth/programs/mmix.tar.gz}):
1211 ``Symbols are stored and retrieved by means of a @samp{ternary
1212 search trie}, following ideas of Bentley and Sedgewick. (See
1213 ACM--SIAM Symp.@: on Discrete Algorithms @samp{8} (1997), 360--369;
1214 R.@:Sedgewick, @samp{Algorithms in C} (Reading, Mass.@:
1215 Addison--Wesley, 1998), @samp{15.4}.) Each trie node stores a
1216 character, and there are branches to subtries for the cases where
1217 a given character is less than, equal to, or greater than the
1218 character in the trie. There also is a pointer to a symbol table
1219 entry if a symbol ends at the current node.''
1221 So it's a tree encoded as a stream of bytes. The stream of bytes
1222 acts on a single virtual global symbol, adding and removing
1223 characters and signalling complete symbol points. Here, we read
1224 the stream and create symbols at the completion points.
1226 First, there's a control byte <<m>>. If any of the listed bits
1227 in <<m>> is nonzero, we execute what stands at the right, in
1231 | 0x40 - Traverse left trie.
1232 | (Read a new command byte and recurse.)
1235 | 0x2f - Read the next byte as a character and store it in the
1236 | current character position; increment character position.
1237 | Test the bits of <<m>>:
1240 | 0x80 - The character is 16-bit (so read another byte,
1241 | merge into current character.
1244 | 0xf - We have a complete symbol; parse the type, value
1245 | and serial number and do what should be done
1246 | with a symbol. The type and length information
1247 | is in j = (m & 0xf).
1249 | (MMO3_REGQUAL_BITS)
1250 | j == 0xf: A register variable. The following
1251 | byte tells which register.
1252 | j <= 8: An absolute symbol. Read j bytes as the
1253 | big-endian number the symbol equals.
1254 | A j = 2 with two zero bytes denotes an
1256 | j > 8: As with j <= 8, but add (0x20 << 56)
1257 | to the value in the following j - 8
1260 | Then comes the serial number, as a variant of
1261 | uleb128, but better named ubeb128:
1262 | Read bytes and shift the previous value left 7
1263 | (multiply by 128). Add in the new byte, repeat
1264 | until a byte has bit 7 set. The serial number
1265 | is the computed value minus 128.
1268 | 0x20 - Traverse middle trie. (Read a new command byte
1269 | and recurse.) Decrement character position.
1272 | 0x10 - Traverse right trie. (Read a new command byte and
1275 Let's look again at the <<lop_stab>> for the trivial file
1276 (@pxref{File layout}).
1278 | 0x980b0000 - lop_stab for ":Main" = 0, serial 1.
1285 This forms the trivial trie (note that the path between ``:'' and
1286 ``M'' is redundant):
1297 | 016e "n" is the last character in a full symbol, and
1298 | with a value represented in one byte.
1299 | 00 The value is 0.
1300 | 81 The serial number is 1. */
1302 bfd_byte m = mmo_get_byte (abfd);
1304 /* Check first if we have a bad hair day. */
1305 if (abfd->tdata.mmo_data->have_error == true)
1309 /* Traverse left trie. */
1310 mmo_get_symbols (abfd);
1312 if (m & MMO3_SYMBITS)
1314 bfd_byte c = mmo_get_byte (abfd);
1315 bfd_byte j = m & MMO3_TYPEBITS;
1317 enum mmo_sym_type sym_type;
1318 unsigned int serno = 0;
1323 bfd_byte c2 = mmo_get_byte (abfd);
1325 /* A two-byte character. We can't grok this, but neither can
1326 mmotype, for other cases than the second byte being zero. */
1330 abfd->tdata.mmo_data->lop_stab_symbol
1331 [abfd->tdata.mmo_data->symbol_position] = 0;
1333 (*_bfd_error_handler)
1334 (_("%s: unsupported wide character sequence\
1335 0x%02X 0x%02X after symbol name starting with `%s'\n"),
1336 bfd_get_filename (abfd), c, c2,
1337 abfd->tdata.mmo_data->lop_stab_symbol);
1338 bfd_set_error (bfd_error_bad_value);
1339 abfd->tdata.mmo_data->have_error = true;
1346 abfd->tdata.mmo_data->lop_stab_symbol[abfd->tdata.mmo_data->symbol_position++] = c;
1347 abfd->tdata.mmo_data->lop_stab_symbol[abfd->tdata.mmo_data->symbol_position] = 0;
1349 if (j & MMO3_REGQUAL_BITS)
1351 if (j == MMO3_REGQUAL_BITS)
1353 sym_type = mmo_reg_sym;
1354 addr = mmo_get_byte (abfd);
1360 for (i = 0; i < j; i++)
1361 addr = (addr << 8) + mmo_get_byte (abfd);
1363 if (addr == 0 && j == MMO3_UNDEF)
1364 sym_type = mmo_undef_sym;
1366 sym_type = mmo_abs_sym;
1372 for (i = MMO3_DATA; i < j; i++)
1373 addr = (addr << 8) + mmo_get_byte (abfd);
1375 addr += (bfd_vma) 0x20 << 56;
1376 sym_type = mmo_data_sym;
1379 /* Get the serial number. */
1382 k = mmo_get_byte (abfd);
1383 serno = (serno << 7) + k;
1388 /* Got it. Now enter it. Skip a leading ":". */
1389 if (abfd->tdata.mmo_data->have_error == false
1390 && ! mmo_create_symbol (abfd,
1391 abfd->tdata.mmo_data->lop_stab_symbol
1393 addr, sym_type, serno))
1394 abfd->tdata.mmo_data->have_error = true;
1397 if (m & MMO3_MIDDLE)
1398 /* Traverse middle trie. */
1399 mmo_get_symbols (abfd);
1401 abfd->tdata.mmo_data->symbol_position--;
1405 /* Traverse right trie. */
1406 mmo_get_symbols (abfd);
1408 return abfd->tdata.mmo_data->have_error == false;
1411 /* Get the location of memory area [VMA..VMA + SIZE - 1], which we think
1412 is in section SEC. Adjust and reallocate zero-initialized contents.
1413 If there's new contents, allocate to the next multiple of
1414 MMO_SEC_CONTENTS_CHUNK_SIZE. */
1416 static INLINE bfd_byte *
1417 mmo_get_loc (sec, vma, size)
1422 bfd_size_type allocated_size;
1423 struct mmo_section_data_struct *sdatap
1424 = (struct mmo_section_data_struct *) sec->used_by_bfd;
1425 struct mmo_data_list_struct *datap = sdatap->head;
1426 struct mmo_data_list_struct *entry;
1428 for (; datap != NULL; datap = datap->next)
1430 if (datap->where <= vma
1431 && datap->where + datap->size >= vma + size)
1432 return datap->data + vma - datap->where;
1433 else if (datap->where <= vma
1434 && datap->where + datap->allocated_size >= vma + size
1435 /* Only munch on the "allocated size" if it does not
1436 overlap the next chunk. */
1437 && (datap->next == NULL || datap->next->where >= vma + size))
1439 /* There was room allocated, but the size wasn't set to include
1441 datap->size += (vma + size) - (datap->where + datap->size);
1443 /* Update the section size. */
1444 if (vma + size > sec->vma + sec->_raw_size)
1445 sec->_raw_size += (vma + size) - (sec->vma + sec->_raw_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. */
1506 if (vma + size > sec->vma + sec->_raw_size)
1507 sec->_raw_size += (vma + size) - (sec->vma + sec->_raw_size);
1511 /* Set sizes once we've read in all sections. */
1514 mmo_map_set_sizes (abfd, sec, ignored)
1515 bfd *abfd ATTRIBUTE_UNUSED;
1517 PTR ignored ATTRIBUTE_UNUSED;
1519 sec->_cooked_size = sec->_raw_size;
1520 sec->lma = sec->vma;
1523 /* Read the mmo file and turn it into sections. */
1530 unsigned int lineno = 1;
1531 boolean error = false;
1533 asection *sec = bfd_make_section_old_way (abfd, MMO_TEXT_SECTION_NAME);
1534 asection *non_spec_sec = NULL;
1535 bfd_vma non_spec_vma = 0;
1536 char *current_filename = NULL;
1537 bfd_size_type nbytes_read = 0;
1538 /* Buffer with room to read a 64-bit value. */
1541 char *file_names[256];
1543 memset (file_names, 0, sizeof (file_names));
1545 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
1548 while ((nbytes_read = bfd_bread (buf, 4, abfd)) == 4)
1552 unsigned int y = bfd_get_8 (abfd, buf + 2);
1553 unsigned int z = bfd_get_8 (abfd, buf + 3);
1555 /* Change back to the original section for lopcodes other
1556 than LOP_QUOTE that comes after a LOP_SPEC. */
1557 if ((buf[1] != LOP_QUOTE || y != 0 || z != 1)
1558 && non_spec_sec != NULL)
1562 non_spec_sec = NULL;
1568 (*_bfd_error_handler)
1569 (_("%s: invalid mmo file: unsupported lopcode `%d'\n"),
1570 bfd_get_filename (abfd), buf[1]);
1571 bfd_set_error (bfd_error_bad_value);
1575 /* Quote the next 32-bit word. */
1576 if (y != 0 || z != 1)
1578 (*_bfd_error_handler)
1579 (_("%s: invalid mmo file: expected YZ = 1 got YZ = %d for lop_quote\n"),
1580 bfd_get_filename (abfd), y*256+z);
1581 bfd_set_error (bfd_error_bad_value);
1584 if (bfd_bread (buf, 4, abfd) != 4)
1587 mmo_xore_32 (sec, vma, bfd_get_32 (abfd, buf));
1594 /* Set vma (and section). */
1595 vma = (bfd_vma) y << 56;
1598 /* Get a 32-bit value. */
1599 if (bfd_bread (buf, 4, abfd) != 4)
1602 vma += bfd_get_32 (abfd, buf);
1606 /* Get a 64-bit value. */
1607 if (bfd_bread (buf, 8, abfd) != 8)
1610 vma += bfd_get_64 (abfd, buf);
1614 (*_bfd_error_handler)
1615 (_("%s: invalid mmo file: expected z = 1 or z = 2, got z = %d for lop_loc\n"),
1616 bfd_get_filename (abfd), z);
1617 bfd_set_error (bfd_error_bad_value);
1621 sec = mmo_decide_section (abfd, vma);
1627 /* Move forward within the same section. */
1630 sec = mmo_decide_section (abfd, vma);
1636 /* A fixup: Store the current vma somewhere. Position using
1637 same format as LOP_LOC. */
1639 bfd_vma p = (bfd_vma) y << 56;
1644 /* Get a 32-bit value. */
1645 if (bfd_bread (buf, 4, abfd) != 4)
1648 p += bfd_get_32 (abfd, buf);
1652 /* Get a 64-bit value. */
1653 if (bfd_bread (buf, 8, abfd) != 8)
1656 p += bfd_get_64 (abfd, buf);
1660 (*_bfd_error_handler)
1661 (_("%s: invalid mmo file: expected z = 1 or z = 2, got z = %d for lop_fixo\n"),
1662 bfd_get_filename (abfd), z);
1663 bfd_set_error (bfd_error_bad_value);
1667 /* The section where we store this address might be a
1668 different one than the current section. */
1669 fixosec = mmo_decide_section (abfd, p);
1670 if (fixosec == NULL)
1672 mmo_xore_64 (fixosec, p, vma);
1677 /* A fixup: Store YZ of this lopcode into YZ at vma - 4 * yz. */
1679 unsigned int yz = (y * 256 + z);
1680 bfd_vma p = vma + 2 - 4 * yz;
1681 asection *fixrsec = mmo_decide_section (abfd, p);
1682 if (fixrsec == NULL)
1684 mmo_xore_16 (fixrsec, p, yz);
1689 /* A fixup, similar to lop_fixr, but taking larger numbers
1690 and can change branches into the opposite direction
1699 (*_bfd_error_handler)
1700 (_("%s: invalid mmo file: expected y = 0, got y = %d for lop_fixrx\n"),
1701 bfd_get_filename (abfd), y);
1702 bfd_set_error (bfd_error_bad_value);
1706 if (z != 16 && z != 24)
1708 (*_bfd_error_handler)
1709 (_("%s: invalid mmo file: expected z = 16 or z = 24, got z = %d for lop_fixrx\n"),
1710 bfd_get_filename (abfd), z);
1711 bfd_set_error (bfd_error_bad_value);
1715 /* Get the next 32-bit value. */
1716 if (bfd_bread (buf, 4, abfd) != 4)
1719 delta = bfd_get_32 (abfd, buf);
1721 /* Do an, ehm, involved calculation for the location of
1722 the fixup. See mmixal documentation for a verbose
1723 explanation. We follow it verbosely here for the
1726 p = vma - 4 * delta;
1727 else if (buf[0] == 1)
1728 p = vma - 4 * ((delta & 0xffffff) - (1 << z));
1731 (*_bfd_error_handler)
1732 (_("%s: invalid mmo file: leading byte of operand word must be 0 or 1, got %d for lop_fixrx\n"),
1733 bfd_get_filename (abfd), buf[0]);
1734 bfd_set_error (bfd_error_bad_value);
1738 fixrsec = mmo_decide_section (abfd, vma);
1739 if (fixrsec == NULL)
1741 mmo_xore_32 (fixrsec, p, delta);
1746 /* Set current file and perhaps the file name. Reset line
1750 char *fname = bfd_malloc (z * 4 + 1);
1754 (*_bfd_error_handler)
1755 (_("%s: cannot allocate file name for file number %d, %d bytes\n"),
1756 bfd_get_filename (abfd), y, z * 4 + 1);
1757 bfd_set_error (bfd_error_system_call);
1763 for (i = 0; i < z; i++)
1765 if (bfd_bread (fname + i * 4, 4, abfd) != 4)
1772 if (file_names[y] != NULL)
1774 (*_bfd_error_handler)
1775 (_("%s: invalid mmo file: file number %d `%s',\
1776 was already entered as `%s'\n"),
1777 bfd_get_filename (abfd), y, fname, file_names[y]);
1778 bfd_set_error (bfd_error_bad_value);
1782 file_names[y] = fname;
1785 if (file_names[y] == NULL)
1787 (*_bfd_error_handler)
1788 (_("%s: invalid mmo file: file name for number %d\
1789 was not specified before use\n"),
1790 bfd_get_filename (abfd), y);
1791 bfd_set_error (bfd_error_bad_value);
1795 current_filename = file_names[y];
1800 /* Set line number. */
1801 lineno = y * 256 + z;
1802 /* FIXME: Create a sequence of mmo-specific line number
1803 entries for each section, then translate into canonical
1808 /* Special data follows until the next non-lop_quote
1812 sec = mmo_get_spec_section (abfd, y * 256 + z);
1821 /* We ignore header information, except we read in the
1822 creation time from the first 32-bit word with the time
1823 in seconds since era. */
1825 && bfd_bread (abfd->tdata.mmo_data->created, 4,
1829 for (i = 1; i < z; i++)
1830 if (bfd_bread (buf, 4, abfd) != 4)
1836 /* This tells of the contents of registers $Z..$255 at
1837 startup. We make a section out of it, with VMA = Z * 8,
1838 but only if Z != 255 or the contents is non-zero. */
1843 bfd_vma startaddr_octa;
1845 /* Read first octaword outside loop to simplify logic when
1846 excluding the Z == 255, octa == 0 case. */
1847 if (bfd_bread (buf, 8, abfd) != 8)
1850 first_octa = bfd_get_64 (abfd, buf);
1852 /* Don't emit contents for the trivial case which is
1853 always present; $255 pointing to Main. */
1857 = bfd_make_section_old_way (abfd,
1858 MMIX_REG_CONTENTS_SECTION_NAME);
1860 loc = mmo_get_loc (rsec, z * 8, (255 - z) * 8);
1861 bfd_put_64 (abfd, first_octa, loc);
1863 for (i = z + 1; i < 255; i++)
1865 if (bfd_bread (loc + (i - z) * 8, 8, abfd) != 8)
1869 /* Read out the last octabyte, and use it to set the
1871 if (bfd_bread (buf, 8, abfd) != 8)
1874 startaddr_octa = bfd_get_64 (abfd, buf);
1877 startaddr_octa = first_octa;
1879 if (! bfd_set_start_address (abfd, startaddr_octa))
1881 /* Currently this can't fail, but this should handle
1883 bfd_set_error (bfd_error_bad_value);
1890 /* We read in the symbols now, not later. */
1891 if (y != 0 || z != 0)
1893 (*_bfd_error_handler)
1894 (_("%s: invalid mmo file: fields y and z of lop_stab\
1895 non-zero, y: %d, z: %d\n"),
1896 bfd_get_filename (abfd), y, z);
1897 bfd_set_error (bfd_error_bad_value);
1901 /* Save the location, so we can check that YZ in the LOP_END
1903 stab_loc = bfd_tell (abfd);
1905 /* It's not said that an MMO can be without symbols (though
1906 mmixal will refuse to assemble files without Main), but
1907 it seems it would still be a valid mmo-file, so allow it.
1908 We detect the absence of a symbol area in that the upper
1909 limit is computed (from the lop_end YZ field) as 0.
1910 Don't call mmo_get_symbols; it can only detect the end of
1911 a valid symbol trie, not the absence of one. */
1912 if (abfd->tdata.mmo_data->max_symbol_length != 0
1913 && ! mmo_get_symbols (abfd))
1919 /* This must be the last 32-bit word in an mmo file.
1921 struct stat statbuf;
1922 long curpos = bfd_tell (abfd);
1924 if (bfd_stat (abfd, &statbuf) < 0)
1927 if (statbuf.st_size != curpos)
1929 (*_bfd_error_handler)
1930 (_("%s: invalid mmo file: lop_end not last item in\
1932 bfd_get_filename (abfd));
1933 bfd_set_error (bfd_error_bad_value);
1937 /* Check that the YZ field is right. Subtract the size of
1938 this LOP_END in the calculation; YZ does not include
1940 if ((long) (y * 256 + z) * 4 != (curpos - stab_loc) - 4)
1942 (*_bfd_error_handler)
1943 (_("%s: invalid mmo file: YZ of lop_end (%ld)\
1944 not equal to the number of tetras to the preceding lop_stab (%ld)\n"),
1945 bfd_get_filename (abfd), (long) (y * 256 + z),
1946 (curpos - stab_loc - 4)/4);
1947 bfd_set_error (bfd_error_bad_value);
1951 bfd_map_over_sections (abfd, mmo_map_set_sizes, NULL);
1958 /* This wasn't a lopcode, so store it in the current section. */
1959 mmo_xore_32 (sec, vma & ~3, bfd_get_32 (abfd, buf));
1966 /* We know this file is a multiple of four bytes (checked in
1967 mmo_object_p), so if we got something other than 0, this was a bad
1968 file (although it's more likely we'll get 0 in that case too).
1969 If we got end-of-file, then there was no lop_stab, so the file has
1972 if (nbytes_read != 0)
1973 bfd_set_error (bfd_error_system_call);
1975 bfd_set_error (bfd_error_bad_value);
1980 /* Mark the .text and .data section with their normal attribute if they
1981 contain anything. This is not redundant wrt. mmo_decide_section,
1982 since that code might never execute, and conversely the alloc+code
1983 section flags must be set then. */
1984 sec = bfd_get_section_by_name (abfd, MMO_TEXT_SECTION_NAME);
1986 && (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
1987 && ! bfd_set_section_flags (abfd, sec,
1988 bfd_get_section_flags (abfd, sec)
1989 | SEC_ALLOC | SEC_LOAD | SEC_CODE))
1992 sec = bfd_get_section_by_name (abfd, MMO_DATA_SECTION_NAME);
1994 && (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
1995 && ! bfd_set_section_flags (abfd, sec,
1996 bfd_get_section_flags (abfd, sec)
1997 | SEC_ALLOC | SEC_LOAD))
2000 /* Free whatever resources we took. */
2001 for (i = 0; i < sizeof (file_names) / sizeof (file_names[0]); i++)
2003 free (file_names[i]);
2004 return error ? false : true;
2007 /* A hook to set up object file dependent section information. For mmo,
2008 we point out the shape of allocated section contents. */
2011 mmo_new_section_hook (abfd, newsect)
2012 bfd *abfd ATTRIBUTE_UNUSED;
2015 /* We zero-fill all fields and assume NULL is represented by an all
2016 zero-bit pattern. */
2017 newsect->used_by_bfd =
2018 (PTR) bfd_zalloc (abfd, sizeof (struct mmo_section_data_struct));
2020 if (!newsect->used_by_bfd)
2023 /* Always align to at least 32-bit words. */
2024 newsect->alignment_power = 2;
2028 /* We already have section contents loaded for sections that have
2032 mmo_get_section_contents (abfd, sec, location, offset, bytes_to_do)
2033 bfd *abfd ATTRIBUTE_UNUSED;
2034 asection *sec ATTRIBUTE_UNUSED;
2035 PTR location ATTRIBUTE_UNUSED;
2036 file_ptr offset ATTRIBUTE_UNUSED;
2037 bfd_size_type bytes_to_do ATTRIBUTE_UNUSED;
2039 /* Iterate over diminishing chunk sizes, copying contents, like
2040 mmo_set_section_contents. */
2043 /* A minor song-and-dance to make sure we're not bitten by the
2044 distant possibility of the cast from bfd_vma to int making the
2045 chunk zero-sized. */
2047 = (int) bytes_to_do != 0 ? bytes_to_do : MMO_SEC_CONTENTS_CHUNK_SIZE;
2051 loc = mmo_get_loc (sec, sec->vma + offset, chunk_size);
2052 while (loc == NULL && (chunk_size /= 2) != 0);
2054 if (chunk_size == 0)
2057 memcpy (location, loc, chunk_size);
2059 location += chunk_size;
2060 bytes_to_do -= chunk_size;
2061 offset += chunk_size;
2066 /* Return the amount of memory needed to read the symbol table. */
2069 mmo_get_symtab_upper_bound (abfd)
2070 bfd *abfd ATTRIBUTE_UNUSED;
2072 return (abfd->symcount + 1) * sizeof (asymbol *);
2075 /* Sort mmo symbols by serial number. */
2078 mmo_sort_mmo_symbols (arg1, arg2)
2082 CONST struct mmo_symbol *sym1 = *(CONST struct mmo_symbol **) arg1;
2083 CONST struct mmo_symbol *sym2 = *(CONST struct mmo_symbol **) arg2;
2085 /* Sort by serial number first. */
2086 if (sym1->serno < sym2->serno)
2088 else if (sym1->serno > sym2->serno)
2091 /* Then sort by address of the table entries. */
2092 return ((CONST char *) arg1 - (CONST char *) arg2);
2095 /* Translate the symbol table. */
2098 mmo_get_symtab (abfd, alocation)
2100 asymbol **alocation;
2102 unsigned int symcount = bfd_get_symcount (abfd);
2106 csymbols = abfd->tdata.mmo_data->csymbols;
2107 if (csymbols == NULL)
2110 struct mmo_symbol *s;
2111 struct mmo_symbol **msp;
2113 /* First we store the symbols into the table we'll return, then we
2114 qsort it on the serial number, with secondary on the address of
2115 the symbol, to preserve order if there would be non-unique serial
2117 for (s = abfd->tdata.mmo_data->symbols,
2118 msp = (struct mmo_symbol **) alocation;
2125 qsort (alocation, symcount, sizeof (struct mmo_symbol *),
2126 mmo_sort_mmo_symbols);
2128 csymbols = (asymbol *) bfd_alloc (abfd, symcount * sizeof (asymbol));
2129 if (csymbols == NULL && symcount != 0)
2131 abfd->tdata.mmo_data->csymbols = csymbols;
2133 for (msp = (struct mmo_symbol **) alocation, c = csymbols;
2140 c->value = s->value;
2141 c->flags = BSF_GLOBAL;
2143 if (s->sym_type == mmo_data_sym)
2146 = bfd_get_section_by_name (abfd, MMO_DATA_SECTION_NAME);
2148 if (c->section == NULL)
2149 c->section = bfd_abs_section_ptr;
2151 c->value -= c->section->vma;
2153 else if (s->sym_type == mmo_undef_sym)
2154 c->section = bfd_und_section_ptr;
2155 else if (s->sym_type == mmo_reg_sym)
2158 = bfd_make_section_old_way (abfd, MMIX_REG_SECTION_NAME);
2163 = bfd_get_section_by_name (abfd, MMO_TEXT_SECTION_NAME);
2166 && c->value >= textsec->vma
2167 && c->value <= textsec->vma + textsec->_cooked_size)
2169 c->section = textsec;
2170 c->value -= c->section->vma;
2173 c->section = bfd_abs_section_ptr;
2180 /* Last, overwrite the incoming table with the right-type entries. */
2181 for (i = 0; i < symcount; i++)
2182 *alocation++ = csymbols++;
2188 /* Get information about a symbol. */
2191 mmo_get_symbol_info (ignore_abfd, symbol, ret)
2192 bfd *ignore_abfd ATTRIBUTE_UNUSED;
2196 bfd_symbol_info (symbol, ret);
2200 mmo_print_symbol (abfd, afile, symbol, how)
2204 bfd_print_symbol_type how;
2206 FILE *file = (FILE *) afile;
2210 case bfd_print_symbol_name:
2211 fprintf (file, "%s", symbol->name);
2214 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
2216 fprintf (file, " %-5s %s",
2217 symbol->section->name,
2222 /* We can't map a file directly into executable code, so the
2223 size of header information is irrelevant. */
2226 mmo_sizeof_headers (abfd, exec)
2227 bfd *abfd ATTRIBUTE_UNUSED;
2228 boolean exec ATTRIBUTE_UNUSED;
2233 /* Write the (section-neutral) file preamble. */
2236 mmo_internal_write_header (abfd)
2239 CONST char lop_pre_bfd[] = { LOP, LOP_PRE, 1, 1};
2241 if (bfd_bwrite (lop_pre_bfd, 4, abfd) != 4)
2244 /* Copy creation time of original file. */
2245 if (bfd_bwrite (abfd->tdata.mmo_data->created, 4, abfd) != 4)
2251 /* Write the LOP_POST record, with global register initializations.
2252 Z is the Z field of the LOP_POST, corresponding to 255 - number of
2253 registers at DATA. The Z = 255 field is filled in with the
2257 mmo_internal_write_post (abfd, z, sec)
2264 mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_POST << 16) | z);
2266 for (i = z; i < 255; i++)
2268 bfd_byte *data = mmo_get_loc (sec, i * 8, 8);
2270 if (bfd_bwrite (data, 8, abfd) != 8)
2274 /* For Z == $255, we always emit the start location; supposedly Main,
2275 but we have it handy at bfd_get_start_address. If we're called with
2276 Z == 255, don't assume DATA is valid. */
2277 bfd_put_64 (abfd, bfd_get_start_address (abfd), buf);
2280 abfd->tdata.mmo_data->have_error == false
2281 && bfd_bwrite (buf, 8, abfd) == 8;
2284 /* Translate to and from BFD flags. This is to make sure that we don't
2285 get bitten by BFD flag number changes. */
2288 mmo_sec_flags_from_bfd_flags (flags)
2291 flagword oflags = 0;
2293 if (flags & SEC_ALLOC)
2294 oflags |= MMO_SEC_ALLOC;
2295 if (flags & SEC_LOAD)
2296 oflags |= MMO_SEC_LOAD;
2297 if (flags & SEC_RELOC)
2298 oflags |= MMO_SEC_RELOC;
2299 if (flags & SEC_READONLY)
2300 oflags |= MMO_SEC_READONLY;
2301 if (flags & SEC_CODE)
2302 oflags |= MMO_SEC_CODE;
2303 if (flags & SEC_DATA)
2304 oflags |= MMO_SEC_DATA;
2305 if (flags & SEC_NEVER_LOAD)
2306 oflags |= MMO_SEC_NEVER_LOAD;
2307 if (flags & SEC_IS_COMMON)
2308 oflags |= MMO_SEC_IS_COMMON;
2309 if (flags & SEC_DEBUGGING)
2310 oflags |= MMO_SEC_DEBUGGING;
2316 bfd_sec_flags_from_mmo_flags (flags)
2319 flagword oflags = 0;
2321 if (flags & MMO_SEC_ALLOC)
2322 oflags |= SEC_ALLOC;
2323 if (flags & MMO_SEC_LOAD)
2325 if (flags & MMO_SEC_RELOC)
2326 oflags |= SEC_RELOC;
2327 if (flags & MMO_SEC_READONLY)
2328 oflags |= SEC_READONLY;
2329 if (flags & MMO_SEC_CODE)
2331 if (flags & MMO_SEC_DATA)
2333 if (flags & MMO_SEC_NEVER_LOAD)
2334 oflags |= SEC_NEVER_LOAD;
2335 if (flags & MMO_SEC_IS_COMMON)
2336 oflags |= SEC_IS_COMMON;
2337 if (flags & MMO_SEC_DEBUGGING)
2338 oflags |= SEC_DEBUGGING;
2343 /* Write a section. */
2346 mmo_internal_write_section (abfd, sec)
2350 /* We do it differently depending on what section this is:
2352 ".text": Output, prepended by information about the first source file
2353 (not yet implemented.)
2357 (".MMIX.reg_contents": Not handled here.)
2359 Anything else: Output inside a lop_spec 80, in the format described
2362 if (strcmp (sec->name, MMO_TEXT_SECTION_NAME) == 0)
2363 /* FIXME: Output source file name and line number. */
2365 mmo_write_loc_chunk_list (abfd,
2366 ((struct mmo_section_data_struct *)
2367 (sec->used_by_bfd))->head);
2368 else if (strcmp (sec->name, MMO_DATA_SECTION_NAME) == 0)
2370 mmo_write_loc_chunk_list (abfd,
2371 ((struct mmo_section_data_struct *)
2372 (sec->used_by_bfd))->head);
2373 else if (strcmp (sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
2374 /* Not handled here. */
2376 /* This would normally be an abort call since this can't happen, but
2377 we don't do that. */
2378 bfd_set_error (bfd_error_bad_value);
2381 else if (strncmp (sec->name, MMIX_OTHER_SPEC_SECTION_PREFIX,
2382 strlen (MMIX_OTHER_SPEC_SECTION_PREFIX)) == 0)
2384 int n = atoi (sec->name + strlen (MMIX_OTHER_SPEC_SECTION_PREFIX));
2385 mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_SPEC << 16) | n);
2387 abfd->tdata.mmo_data->have_error == false
2388 && mmo_write_chunk_list (abfd,
2389 ((struct mmo_section_data_struct *)
2390 (sec->used_by_bfd))->head);
2392 /* Ignore sections that are just allocated or empty; we write out
2394 else if ((bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS) != 0
2395 && sec->_raw_size != 0)
2397 /* Keep the document-comment formatted the way it is. */
2400 mmo section mapping, , Symbol-table, mmo
2404 The implementation in BFD uses special data type 80 (decimal) to
2405 encapsulate and describe named sections, containing e.g.@: debug
2406 information. If needed, any datum in the encapsulation will be
2407 quoted using lop_quote. First comes a 32-bit word holding the
2408 number of 32-bit words containing the zero-terminated zero-padded
2409 segment name. After the name there's a 32-bit word holding flags
2410 describing the section type. Then comes a 64-bit big-endian word
2411 with the section length (in bytes), then another with the section
2412 start address. Depending on the type of section, the contents
2413 might follow, zero-padded to 32-bit boundary. For a loadable
2414 section (such as data or code), the contents might follow at some
2415 later point, not necessarily immediately, as a lop_loc with the
2416 same start address as in the section description, followed by the
2417 contents. This in effect forms a descriptor that must be emitted
2418 before the actual contents. Sections described this way must not
2421 For areas that don't have such descriptors, synthetic sections are
2422 formed by BFD. Consecutive contents in the two memory areas
2423 @samp{0x0000@dots{}00} to @samp{0x01ff@dots{}ff} and
2424 @samp{0x2000@dots{}00} to @samp{0x20ff@dots{}ff} are entered in
2425 sections named <<.text>> and <<.data>> respectively. If an area
2426 is not otherwise described, but would together with a neighboring
2427 lower area be less than @samp{0x40000000} bytes long, it is joined
2428 with the lower area and the gap is zero-filled. For other cases,
2429 a new section is formed, named <<.MMIX.sec.@var{n}>>. Here,
2430 @var{n} is a number, a running count through the mmo file,
2434 A loadable section specified as:
2436 | .section secname,"ax"
2437 | TETRA 1,2,3,4,-1,-2009
2440 and linked to address @samp{0x4}, is represented by the sequence:
2442 | 0x98080050 - lop_spec 80
2443 | 0x00000002 - two 32-bit words for the section name
2444 | 0x7365636e - "secn"
2445 | 0x616d6500 - "ame\0"
2446 | 0x00000033 - flags CODE, READONLY, LOAD, ALLOC
2447 | 0x00000000 - high 32 bits of section length
2448 | 0x0000001c - section length is 28 bytes; 6 * 4 + 1 + alignment to 32 bits
2449 | 0x00000000 - high 32 bits of section address
2450 | 0x00000004 - section address is 4
2451 | 0x98010002 - 64 bits with address of following data
2452 | 0x00000000 - high 64 bits of address
2453 | 0x00000004 - data starts at address 4
2459 | 0xfffff827 - -2009
2460 | 0x50000000 - 80 as a byte, padded with zeros.
2462 Note that the lop_spec wrapping does not include the section
2463 contents. Compare this to a non-loaded section specified as:
2466 | TETRA 200001,100002
2469 This, when linked to address @samp{0x200000000000001c}, is
2472 | 0x98080050 - lop_spec 80
2473 | 0x00000002 - two 32-bit words for the section name
2474 | 0x7365636e - "thir"
2475 | 0x616d6500 - "dsec"
2476 | 0x00000010 - flag READONLY
2477 | 0x00000000 - high 32 bits of section length
2478 | 0x0000000c - section length is 12 bytes; 2 * 4 + 2 + alignment to 32 bits
2479 | 0x20000000 - high 64 bits of address
2480 | 0x0000001c - low 64 bits of address 0x200000000000001c
2481 | 0x00030d41 - 200001
2482 | 0x000186a2 - 100002
2483 | 0x26280000 - 38, 40 as bytes, padded with zeros
2485 For the latter example, the section contents must not to appear
2486 loaded in memory, and is therefore specified as part of the
2487 special data. The address is usually unimportant but might
2488 provide information for e.g.@: the DWARF 2 debugging format. */
2490 mmo_write_tetra_raw (abfd, LOP_SPEC_SECTION);
2491 mmo_write_tetra (abfd, (strlen (sec->name) + 3) / 4);
2492 mmo_write_chunk (abfd, sec->name, strlen (sec->name));
2493 /* FIXME: We can get debug sections (.debug_line & Co.) with a
2494 section flag still having SEC_RELOC set. Investigate. This
2495 might be true for all alien sections; perhaps mmo.em should clear
2496 that flag. Might be related to weak references. */
2497 mmo_write_tetra (abfd,
2498 mmo_sec_flags_from_bfd_flags
2499 (bfd_get_section_flags (abfd, sec)));
2500 mmo_write_octa (abfd, sec->_raw_size);
2501 mmo_write_octa (abfd, bfd_get_section_vma (abfd, sec));
2503 /* Writing a LOP_LOC ends the LOP_SPEC data, and makes data actually
2505 if (bfd_get_section_flags (abfd, sec) & SEC_LOAD)
2507 abfd->tdata.mmo_data->have_error == false
2508 && mmo_write_loc_chunk_list (abfd,
2509 ((struct mmo_section_data_struct *)
2510 (sec->used_by_bfd))->head);
2512 abfd->tdata.mmo_data->have_error == false
2513 && mmo_write_chunk_list (abfd,
2514 ((struct mmo_section_data_struct *)
2515 (sec->used_by_bfd))->head);
2520 /* We save up all data before output. */
2523 mmo_set_section_contents (abfd, sec, location, offset, bytes_to_do)
2524 bfd *abfd ATTRIBUTE_UNUSED;
2528 bfd_size_type bytes_to_do;
2530 /* Iterate over diminishing chunk sizes, copying contents. */
2533 /* A minor song-and-dance to make sure we're not bitten by the
2534 distant possibility of the cast from bfd_vma to int making the
2535 chunk zero-sized. */
2537 = (int) bytes_to_do != 0 ? bytes_to_do : MMO_SEC_CONTENTS_CHUNK_SIZE;
2541 loc = mmo_get_loc (sec, sec->vma + offset, chunk_size);
2542 while (loc == NULL && (chunk_size /= 2) != 0);
2544 if (chunk_size == 0)
2547 memcpy (loc, location, chunk_size);
2549 location += chunk_size;
2550 bytes_to_do -= chunk_size;
2551 offset += chunk_size;
2556 /* Add a symbol to a trie-tree. */
2559 mmo_internal_add_3_sym (abfd, rootp, symp)
2561 struct mmo_symbol_trie *rootp;
2562 CONST struct mmo_symbol *symp;
2564 CONST char *name = symp->name;
2565 struct mmo_symbol_trie *trie = rootp;
2566 struct mmo_symbol_trie **triep = NULL;
2568 while (*name && trie != NULL)
2570 if (*name < trie->symchar)
2572 triep = &trie->left;
2575 else if (*name > trie->symchar)
2577 triep = &trie->right;
2580 else if (*name == trie->symchar)
2582 triep = &trie->middle;
2585 /* Make sure "trie" points to where we should fill in the
2586 current symbol whenever we've iterated through "name". We
2587 would lose the right position if we encounter "foobar" then
2590 trie = trie->middle;
2596 /* Create middle branches for the rest of the characters. */
2597 trie = bfd_zalloc (abfd, sizeof (struct mmo_symbol_trie));
2599 trie->symchar = *name++;
2600 triep = &trie->middle;
2603 /* We discover a duplicate symbol rather late in the process, but still;
2604 we discover it and bail out. */
2605 if (trie->sym.name != NULL)
2607 (*_bfd_error_handler)
2608 (_("%s: invalid symbol table: duplicate symbol `%s'\n"),
2609 bfd_get_filename (abfd), trie->sym.name);
2610 bfd_set_error (bfd_error_bad_value);
2614 memcpy (&trie->sym, symp, sizeof *symp);
2618 /* Find out the length of the serialized version of a trie in bytes. */
2621 mmo_internal_3_length (abfd, trie)
2623 struct mmo_symbol_trie *trie;
2625 /* First, one for the control byte. */
2626 unsigned int length = 1;
2631 /* Add in the recursion to the left. */
2632 length += mmo_internal_3_length (abfd, trie->left);
2634 /* Add in the middle trie and the character. */
2635 length += 1 + mmo_internal_3_length (abfd, trie->middle);
2637 /* Add in the recursion to the right. */
2638 length += mmo_internal_3_length (abfd, trie->right);
2640 /* Add in bytes for the symbol (if this is an endnode). */
2641 if (trie->sym.name != NULL)
2643 unsigned int serno = trie->sym.serno;
2645 /* First what it takes to encode the value. */
2646 if (trie->sym.sym_type == mmo_reg_sym)
2648 else if (trie->sym.sym_type == mmo_undef_sym)
2652 bfd_vma value = trie->sym.value;
2654 /* Coded in one to eight following bytes. */
2655 if (trie->sym.sym_type == mmo_data_sym)
2656 value -= (bfd_vma) 0x20 << 56;
2666 /* Find out what it takes to encode the serial number. */
2678 /* Helper function for outputting the serial number of a symbol, output as
2679 a variant of leb128 (see dwarf2 documentation) which could be called
2680 beb128. Using a helper function and recursion simplifies debugging. */
2683 mmo_beb128_out (abfd, serno, marker)
2689 mmo_beb128_out (abfd, serno >> 7, 0);
2690 mmo_write_byte (abfd, marker | (serno & 0x7f));
2693 /* Serialize a trie. */
2696 mmo_internal_3_dump (abfd, trie)
2698 struct mmo_symbol_trie *trie;
2700 bfd_byte control = 0;
2706 control |= MMO3_LEFT;
2709 control |= MMO3_MIDDLE;
2712 control |= MMO3_RIGHT;
2714 if (trie->sym.name != NULL)
2716 /* Encode the symbol type and length of value bytes. */
2717 if (trie->sym.sym_type == mmo_reg_sym)
2718 control |= MMO3_REGQUAL_BITS;
2719 else if (trie->sym.sym_type == mmo_undef_sym)
2720 control |= MMO3_UNDEF;
2723 bfd_vma value = trie->sym.value;
2725 /* Coded in 1..8 following bytes. */
2726 if (trie->sym.sym_type == mmo_data_sym)
2728 control |= MMO3_DATA;
2729 value -= (bfd_vma) 0x20 << 56;
2741 /* The control byte is output before recursing. */
2742 mmo_write_byte (abfd, control);
2744 mmo_internal_3_dump (abfd, trie->left);
2746 if (control & MMO3_SYMBITS)
2748 mmo_write_byte (abfd, trie->symchar);
2750 if (trie->sym.name != NULL)
2752 if (trie->sym.sym_type == mmo_reg_sym)
2753 mmo_write_byte (abfd, trie->sym.value);
2754 else if (trie->sym.sym_type == mmo_undef_sym)
2756 mmo_write_byte (abfd, 0);
2757 mmo_write_byte (abfd, 0);
2761 bfd_vma value = trie->sym.value;
2763 bfd_byte byte_n = control & 15;
2765 /* Coded in 1..8 following bytes. Note that the value is
2766 shifted out big-endian. */
2767 if (trie->sym.sym_type == mmo_data_sym)
2769 value -= (bfd_vma) 0x20 << 56;
2775 mmo_write_byte (abfd, (value >> ((byte_n - 1) * 8)) & 0xff);
2778 while (byte_n != 0);
2781 mmo_beb128_out (abfd, trie->sym.serno, 128);
2783 mmo_internal_3_dump (abfd, trie->middle);
2785 mmo_internal_3_dump (abfd, trie->right);
2788 /* Write symbols, either in mmo format or hidden in a lop_spec 80 section.
2789 Write the lop_end terminator also. */
2792 mmo_write_symbols_and_terminator (abfd)
2795 int count = bfd_get_symcount (abfd);
2796 asymbol *fakemain[2];
2799 struct mmo_symbol_trie root;
2804 /* Create a symbol for "Main". */
2805 asymbol *mainsym = bfd_make_empty_symbol (abfd);
2807 mainsym->flags = BSF_GLOBAL;
2808 mainsym->value = bfd_get_start_address (abfd);
2809 mainsym->name = MMIX_START_SYMBOL_NAME;
2810 mainsym->section = bfd_abs_section_ptr;
2811 fakemain[0] = mainsym;
2814 memset (&root, 0, sizeof (root));
2816 /* Make all symbols take a left turn. */
2817 root.symchar = 0xff;
2819 /* There must always be a ":Main", so we'll add one
2820 if there are no symbols. */
2827 table = bfd_get_outsymbols (abfd);
2829 for (i = 0; i < count && table[i] != NULL; i++)
2831 asymbol *s = table[i];
2833 /* It's not enough to consult bfd_is_local_label, since it does not
2834 mean "local" in the sense of linkable-and-observable-after-link.
2835 Let's just check the BSF_GLOBAL flag.
2837 Also, don't export symbols with characters not in the allowed set. */
2838 if ((s->flags & (BSF_DEBUGGING|BSF_GLOBAL)) == BSF_GLOBAL
2840 valid_mmo_symbol_character_set) == strlen (s->name))
2842 struct mmo_symbol sym;
2843 memset (&sym, 0, sizeof (sym));
2848 + s->section->output_section->vma
2849 + s->section->output_offset;
2851 if (bfd_is_und_section (s->section))
2852 sym.sym_type = mmo_undef_sym;
2853 else if (strcmp (s->section->name, MMO_DATA_SECTION_NAME) == 0
2854 /* The encoding of data symbols require that the "rest"
2855 of the value fits in 6 bytes, so the upper two bytes
2856 must be 0x2000. All other symbols get to be the
2858 && (sym.value >> 48) == 0x2000)
2859 sym.sym_type = mmo_data_sym;
2860 else if (strcmp (s->section->name, MMIX_REG_SECTION_NAME) == 0)
2861 sym.sym_type = mmo_reg_sym;
2862 else if (strcmp (s->section->name,
2863 MMIX_REG_CONTENTS_SECTION_NAME) == 0)
2865 sym.sym_type = mmo_reg_sym;
2869 sym.sym_type = mmo_abs_sym;
2871 /* FIXME: We assume the order of the received symbols is an
2872 ordered mapping of the serial numbers. This is not
2873 necessarily true if we e.g. objcopy a mmo file to another and
2874 there are gaps in the numbering. Note sure if this can
2875 happen. Not sure what to do. */
2876 /* Make sure Main has serial number 1; others start at 2. */
2877 if (strcmp (s->name, MMIX_START_SYMBOL_NAME) == 0)
2881 /* Check that the value assigned to :Main is the same as the
2882 entry address. The default linker script asserts this.
2883 This is as good a place as any to check this consistency. */
2884 if (sym.value != bfd_get_start_address (abfd))
2886 /* Arbitrary buffer to hold the printable representation
2889 char vmas_start[40];
2890 bfd_vma vma_start = bfd_get_start_address (abfd);
2892 sprintf_vma (vmas_main, s->value);
2893 sprintf_vma (vmas_start, vma_start);
2895 (*_bfd_error_handler)
2896 (_("%s: Bad symbol definition: `Main' set to %s rather\
2897 than the start address %s\n"),
2898 bfd_get_filename (abfd), vmas_main, vmas_start);
2899 bfd_set_error (bfd_error_bad_value);
2904 sym.serno = serno++;
2906 if (! mmo_internal_add_3_sym (abfd, &root, &sym))
2911 /* Change the root node to be a ":"-prefix. */
2913 root.middle = root.left;
2917 /* We have to find out if we can fit the whole symbol table in the mmo
2918 symtab. It would be bad to assume we can always fit it in 262144
2919 bytes. If we can't, just leave the Main symbol. */
2920 trie_len = (mmo_internal_3_length (abfd, &root) + 3)/4;
2922 if (trie_len > 0xffff)
2924 /* Test this code by using a lower limit in the test above and check
2925 that the single "Main" symbol is emitted and handled properly.
2926 There's no specific test-case. */
2927 struct mmo_symbol sym;
2929 (*_bfd_error_handler)
2930 (_("%s: warning: symbol table too large for mmo, larger than 65535\
2931 32-bit words: %d. Only `Main' will be emitted.\n"),
2932 bfd_get_filename (abfd), trie_len);
2934 memset (&sym, 0, sizeof (sym));
2935 sym.sym_type = mmo_abs_sym;
2936 sym.name = MMIX_START_SYMBOL_NAME;
2938 sym.value = bfd_get_start_address (abfd);
2940 /* Then patch up a symbol table to be just the ":Main" symbol. */
2941 memset (&root, 0, sizeof (root));
2942 root.left = root.middle;
2943 root.symchar = 0xff;
2947 if (! mmo_internal_add_3_sym (abfd, &root, &sym))
2951 root.middle = root.left;
2955 trie_len = (mmo_internal_3_length (abfd, &root) + 3)/4;
2958 /* Reset the written-bytes counter. */
2959 abfd->tdata.mmo_data->byte_no = 0;
2961 /* Put out the lop_stab mark. */
2962 bfd_put_32 (abfd, (LOP << 24) | (LOP_STAB << 16), buf);
2963 if (bfd_bwrite (buf, 4, abfd) != 4)
2966 /* Dump out symbols. */
2967 mmo_internal_3_dump (abfd, &root);
2969 if (trie_len != (abfd->tdata.mmo_data->byte_no + 3)/4)
2971 /* I haven't seen this trig. It seems no use claiming this case
2972 isn't debugged and abort if we get here. Instead emit a
2973 diagnostic and fail "normally". */
2974 (*_bfd_error_handler)
2975 (_("%s: internal error, symbol table changed size from %d to %d\
2977 bfd_get_filename (abfd), trie_len,
2978 (abfd->tdata.mmo_data->byte_no + 3)/4);
2979 bfd_set_error (bfd_error_bad_value);
2983 /* Dump out remaining bytes in the buffer and handle I/O errors by
2984 propagating errors. */
2985 if ((abfd->tdata.mmo_data->byte_no % 4) != 0
2986 || abfd->tdata.mmo_data->have_error)
2988 memset (abfd->tdata.mmo_data->buf + (abfd->tdata.mmo_data->byte_no % 4),
2989 0, 4 - (abfd->tdata.mmo_data->byte_no % 4));
2991 if (abfd->tdata.mmo_data->have_error
2992 || bfd_bwrite (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
2996 bfd_put_32 (abfd, (LOP << 24) | (LOP_END << 16) | trie_len, buf);
2997 return bfd_bwrite (buf, 4, abfd) == 4;
3000 /* Write section unless it is the register contents section. For that, we
3001 instead store the section in the supplied pointer. This function is
3002 used through bfd_map_over_sections. */
3005 mmo_write_section_unless_reg_contents (abfd, sec, p)
3010 struct mmo_write_sec_info *infop = (struct mmo_write_sec_info *) p;
3012 if (infop->retval == false)
3015 if (strcmp (sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
3017 infop->reg_section = sec;
3021 /* Exclude the convenience register section. */
3022 if (strcmp (sec->name, MMIX_REG_SECTION_NAME) == 0)
3024 if (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
3026 /* Make sure it hasn't got contents. It seems impossible to
3027 make it carry contents, so we don't have a test-case for
3029 (*_bfd_error_handler)
3030 (_("%s: internal error, internal register section %s had\
3032 bfd_get_filename (abfd), sec->name);
3033 bfd_set_error (bfd_error_bad_value);
3034 infop->retval = false;
3041 infop->retval = mmo_internal_write_section (abfd, sec);
3044 /* Do the actual output of a file. Assumes mmo_set_section_contents is
3048 mmo_write_object_contents (abfd)
3051 struct mmo_write_sec_info wsecinfo;
3053 /* First, there are a few words of preamble. */
3054 if (! mmo_internal_write_header (abfd))
3057 wsecinfo.reg_section = NULL;
3058 wsecinfo.retval = true;
3060 bfd_map_over_sections (abfd, mmo_write_section_unless_reg_contents,
3063 if (wsecinfo.retval == false)
3066 if (wsecinfo.reg_section != NULL)
3068 asection *sec = wsecinfo.reg_section;
3069 unsigned int z = (unsigned int) (sec->vma / 8);
3071 /* Registers 0..31 must not be global. Do sanity check on the "vma"
3072 of the register contents section and check that it corresponds to
3073 the length of the section. */
3074 if (z < 32 || z >= 255 || (sec->vma & 7) != 0
3075 || sec->vma != 256 * 8 - sec->_raw_size - 8)
3077 bfd_set_error (bfd_error_bad_value);
3079 if (sec->_raw_size == 0)
3080 /* There must always be at least one such register. */
3081 (*_bfd_error_handler)
3082 (_("%s: no initialized registers; section length 0\n"),
3083 bfd_get_filename (abfd));
3084 else if (sec->vma > (256 - 32) * 8)
3085 /* Provide better error message for the case of too many
3086 global registers. */
3087 (*_bfd_error_handler)
3088 (_("%s: too many initialized registers; section length %ld\n"),
3089 bfd_get_filename (abfd),
3090 (long) sec->_raw_size);
3092 (*_bfd_error_handler)
3093 (_("%s: invalid start address for initialized registers of\
3094 length %ld: 0x%lx%08lx\n"),
3095 bfd_get_filename (abfd),
3096 (long) sec->_raw_size,
3097 (unsigned long) (sec->vma >> 32), (unsigned long) (sec->vma));
3102 if (! mmo_internal_write_post (abfd, z, sec))
3106 if (! mmo_internal_write_post (abfd, 255, NULL))
3109 return mmo_write_symbols_and_terminator (abfd);
3112 /* Return the size of a NULL pointer, so we support linking in an mmo
3116 mmo_get_reloc_upper_bound (abfd, sec)
3117 bfd *abfd ATTRIBUTE_UNUSED;
3118 asection *sec ATTRIBUTE_UNUSED;
3120 return sizeof (PTR);
3123 /* Similarly canonicalize relocs to empty, filling in the terminating NULL
3127 mmo_canonicalize_reloc (abfd, section, relptr, symbols)
3128 bfd *abfd ATTRIBUTE_UNUSED;
3129 sec_ptr section ATTRIBUTE_UNUSED;
3131 asymbol **symbols ATTRIBUTE_UNUSED;
3137 /* If there's anything in particular in a mmo bfd that we want to free,
3138 make this a real function. Only do this if you see major memory
3139 thrashing; zealous free:ing will cause unwanted behavior, especially if
3140 you "free" memory allocated with "bfd_alloc", or even "bfd_release" a
3141 block allocated with "bfd_alloc"; they're really allocated from an
3142 obstack, and we don't know what was allocated there since this
3143 particular allocation. */
3145 #define mmo_close_and_cleanup _bfd_generic_close_and_cleanup
3146 #define mmo_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
3148 /* Perhaps we need to adjust this one; mmo labels (originally) without a
3149 leading ':' might more appropriately be called local. */
3150 #define mmo_bfd_is_local_label_name bfd_generic_is_local_label_name
3152 /* Is this one really used or defined by anyone? */
3153 #define mmo_get_lineno _bfd_nosymbols_get_lineno
3155 /* FIXME: We can do better on this one, if we have a dwarf2 .debug_line
3156 section or if MMO line numbers are implemented. */
3157 #define mmo_find_nearest_line _bfd_nosymbols_find_nearest_line
3158 #define mmo_make_empty_symbol _bfd_generic_make_empty_symbol
3159 #define mmo_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
3160 #define mmo_read_minisymbols _bfd_generic_read_minisymbols
3161 #define mmo_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
3163 #define mmo_get_section_contents_in_window \
3164 _bfd_generic_get_section_contents_in_window
3165 #define mmo_bfd_get_relocated_section_contents \
3166 bfd_generic_get_relocated_section_contents
3167 #define mmo_bfd_gc_sections bfd_generic_gc_sections
3168 #define mmo_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
3169 #define mmo_bfd_link_add_symbols _bfd_generic_link_add_symbols
3170 #define mmo_bfd_final_link _bfd_generic_final_link
3171 #define mmo_bfd_link_split_section _bfd_generic_link_split_section
3173 /* Strictly speaking, only MMIX uses this restricted format, but let's not
3174 stop anybody from shooting themselves in the foot. */
3175 #define mmo_set_arch_mach bfd_default_set_arch_mach
3176 #define mmo_bfd_relax_section bfd_generic_relax_section
3177 #define mmo_bfd_merge_sections bfd_generic_merge_sections
3179 /* objcopy will be upset if we return -1 from bfd_get_reloc_upper_bound by
3180 using BFD_JUMP_TABLE_RELOCS (_bfd_norelocs) rather than 0. FIXME: Most
3181 likely a bug in the _bfd_norelocs definition.
3183 On the other hand, we smuggle in an mmo object (because setting up ELF
3184 is too cumbersome) when linking (from other formats, presumably ELF) to
3185 represent the g255 entry. We need to link that object, so need to say
3186 it has no relocs. Upper bound for the size of the relocation table is
3187 the size of a NULL pointer, and we support "canonicalization" for that
3189 #define mmo_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
3191 /* We want to copy time of creation, otherwise we'd use
3192 BFD_JUMP_TABLE_COPY (_bfd_generic). */
3193 #define mmo_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
3194 #define mmo_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
3195 #define mmo_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
3196 #define mmo_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
3197 #define mmo_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
3199 CONST bfd_target bfd_mmo_vec =
3202 bfd_target_mmo_flavour,
3203 BFD_ENDIAN_BIG, /* target byte order */
3204 BFD_ENDIAN_BIG, /* target headers byte order */
3206 /* FIXME: Might need adjustments. */
3207 (HAS_RELOC | EXEC_P | /* object flags */
3208 HAS_LINENO | HAS_DEBUG |
3209 HAS_SYMS | HAS_LOCALS | WP_TEXT),
3211 /* FIXME: Might need adjustments. */
3212 (SEC_CODE | SEC_DATA | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
3213 | SEC_READONLY | SEC_EXCLUDE | SEC_DEBUGGING | SEC_IN_MEMORY),
3215 0, /* leading underscore */
3216 ' ', /* ar_pad_char */
3217 16, /* ar_max_namelen */
3218 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3219 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3220 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
3221 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3222 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3223 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
3227 mmo_object_p, /* bfd_check_format */
3237 { /* bfd_write_contents */
3239 mmo_write_object_contents,
3244 BFD_JUMP_TABLE_GENERIC (mmo),
3245 BFD_JUMP_TABLE_COPY (mmo),
3246 BFD_JUMP_TABLE_CORE (_bfd_nocore),
3247 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
3248 BFD_JUMP_TABLE_SYMBOLS (mmo),
3249 /* We have to provide a valid method for getting relocs, returning zero,
3250 so we can't say BFD_JUMP_TABLE_RELOCS (_bfd_norelocs). */
3251 BFD_JUMP_TABLE_RELOCS (mmo),
3252 BFD_JUMP_TABLE_WRITE (mmo),
3253 BFD_JUMP_TABLE_LINK (mmo),
3254 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),