* aout-adobe.c: Don't compare against "true" or "false.
[external/binutils.git] / bfd / mmo.c
1 /* BFD back-end for mmo objects (MMIX-specific object-format).
2    Copyright 2001, 2002
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
6    binary.c.
7
8 This file is part of BFD, the Binary File Descriptor library.
9
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.
14
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.
19
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.  */
23
24 /*
25 SECTION
26         mmo backend
27
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.
41
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".
45 @menu
46 @* File layout::
47 @* Symbol-table::
48 @* mmo section mapping::
49 @end menu
50
51 INODE
52 File layout, Symbol-table, mmo, mmo
53 SUBSECTION
54         File layout
55
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}.
63
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},
72         the lopcodes are:
73
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
78         section mapping}.
79
80         @table @code
81         @item lop_quote
82         0x98000001.  The next word is contents, regardless of whether it
83         starts with 0x98 or not.
84
85         @item lop_loc
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.
91
92         @item lop_skip
93         0x9802YYZZ.  Increase the current location by @samp{YZ} bytes.
94
95         @item lop_fixo
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 *
99         2^56}.
100
101         @item lop_fixr
102         0x9804YYZZ.  @samp{YZ} is stored into the current location plus
103         @math{2 - 4 * YZ}.
104
105         @item lop_fixrx
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})}.
112
113         @item lop_file
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.
120
121         @item lop_line
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.
126
127         @item lop_spec
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}.
131
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.
139
140         @item lop_pre
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}.
144
145         @item lop_post
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}.
151
152         @item lop_stab
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}).
157
158         @item lop_end
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.
163         @end table
164
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>>.
168
169 EXAMPLE
170         This trivial one-label, one-instruction file:
171
172 | :Main TRAP 1,2,3
173
174         can be represented this way in mmo:
175
176 | 0x98090101 - lop_pre, one 32-bit word with timestamp.
177 | <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.
188 | 0x00000000
189 | 0x00000000
190 | 0x980b0000 - lop_stab for ":Main" = 0, serial 1.
191 | 0x203a4040   @xref{Symbol-table}.
192 | 0x10404020
193 | 0x4d206120
194 | 0x69016e00
195 | 0x81000000
196 | 0x980c0005 - lop_end; symbol table contained five 32-bit words.  */
197
198 #include "bfd.h"
199 #include "sysdep.h"
200 #include "libbfd.h"
201 #include "libiberty.h"
202 #include "elf/mmix.h"
203 #include "opcode/mmix.h"
204
205 #define LOP 0x98
206 #define LOP_QUOTE 0
207 #define LOP_LOC 1
208 #define LOP_SKIP 2
209 #define LOP_FIXO 3
210 #define LOP_FIXR 4
211 #define LOP_FIXRX 5
212 #define LOP_FILE 6
213 #define LOP_LINE 7
214 #define LOP_SPEC 8
215 #define LOP_PRE 9
216 #define LOP_POST 10
217 #define LOP_STAB 11
218 #define LOP_END 12
219
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)
224
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)
228
229 /* An arbitrary number for the maximum length section name size.  */
230 #define MAX_SECTION_NAME_SIZE (1024 * 1024)
231
232 /* A quite arbitrary number for the maximum length section size.  */
233 #define MAX_ARTIFICIAL_SECTION_SIZE (1024 * 1024 * 1024)
234
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
241 #define MMO3_UNDEF 2
242 #define MMO3_DATA 8
243 #define MMO3_SYMBITS 0x2f
244
245 /* Put these everywhere in new code.  */
246 #define FATAL_DEBUG                                             \
247  _bfd_abort (__FILE__, __LINE__,                                \
248              "Internal: Non-debugged code (test-case missing)")
249
250 #define BAD_CASE(x)                             \
251  _bfd_abort (__FILE__, __LINE__,                \
252              "bad case for " #x)
253
254 enum mmo_sym_type { mmo_reg_sym, mmo_undef_sym, mmo_data_sym, mmo_abs_sym};
255
256 /* When scanning the mmo file, a linked list of mmo_symbol
257    structures is built to represent the symbol table (if there is
258    one).  */
259
260 struct mmo_symbol
261   {
262     struct mmo_symbol *next;
263     const char *name;
264     bfd_vma value;
265     enum mmo_sym_type sym_type;
266     unsigned int serno;
267   };
268
269 struct mmo_data_list_struct
270   {
271     struct mmo_data_list_struct *next;
272     bfd_vma where;
273     bfd_size_type size;
274     bfd_size_type allocated_size;
275     bfd_byte data[1];
276   };
277
278 typedef struct mmo_data_list_struct mmo_data_list_type;
279
280 struct mmo_symbol_trie
281   {
282     struct mmo_symbol_trie *left;
283     struct mmo_symbol_trie *right;
284     struct mmo_symbol_trie *middle;
285
286     bfd_byte symchar;
287
288     /* A zero name means there's nothing here.  */
289     struct mmo_symbol sym;
290   };
291
292 /* The mmo tdata information.  */
293
294 struct mmo_data_struct
295   {
296     struct mmo_symbol *symbols;
297     struct mmo_symbol *symtail;
298     asymbol *csymbols;
299
300     /* File representation of time (NULL) when this file was created.  */
301     bfd_byte created[4];
302
303     /* When we're reading bytes recursively, check this occasionally.
304        Also holds write errors.  */
305     boolean have_error;
306
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;
311
312     /* Here's the symbol we build in lop_stab.  */
313     char *lop_stab_symbol;
314
315     /* Index into lop_stab_symbol for the next character when parsing the
316        symbol information.  */
317     int symbol_position;
318
319     /* When creating arbitrary sections, we need to count section numbers.  */
320     int sec_no;
321
322     /* When writing or reading byte-wise, we need to count the bytes
323        within a 32-bit word.  */
324     int byte_no;
325
326     /* We also need a buffer to hold the bytes we count reading or writing.  */
327     bfd_byte buf[4];
328   };
329
330 typedef struct mmo_data_struct tdata_type;
331
332 struct mmo_section_data_struct
333   {
334     mmo_data_list_type *head;
335     mmo_data_list_type *tail;
336   };
337
338 /* These structures are used in bfd_map_over_sections constructs.  */
339
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
343   {
344     asection *reg_section;
345     boolean retval;
346   };
347
348 /* Used when trying to find a section corresponding to addr.  */
349 struct mmo_find_sec_info
350   {
351     asection *sec;
352     bfd_vma addr;
353   };
354
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 *));
390
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_flush_chunk PARAMS ((bfd *));
409 static boolean mmo_write_loc_chunk
410   PARAMS ((bfd *, bfd_vma, const bfd_byte *, unsigned int, bfd_vma *));
411 static boolean mmo_write_chunk_list PARAMS ((bfd *, mmo_data_list_type *));
412 static boolean mmo_write_loc_chunk_list
413   PARAMS ((bfd *, mmo_data_list_type *));
414 static boolean mmo_write_symbols_and_terminator PARAMS ((bfd *));
415 static flagword mmo_sec_flags_from_bfd_flags PARAMS ((flagword));
416 static flagword bfd_sec_flags_from_mmo_flags PARAMS ((flagword));
417 static bfd_byte mmo_get_byte PARAMS ((bfd *));
418 static void mmo_write_byte PARAMS ((bfd *, bfd_byte));
419 static boolean mmo_new_section_hook PARAMS ((bfd *, asection *));
420 static int mmo_sort_mmo_symbols PARAMS ((const PTR, const PTR));
421 static boolean mmo_write_object_contents PARAMS ((bfd *));
422 static long mmo_canonicalize_reloc
423   PARAMS ((bfd *, sec_ptr, arelent **, asymbol **));
424
425 /* Global "const" variables initialized once.  Must not depend on
426    particular input or caller; put such things into the bfd or elsewhere.
427    Look ma, no static per-invocation data!  */
428
429 static unsigned
430 char valid_mmo_symbol_character_set[/* A-Z a-z (we assume consecutive
431                                        codes; sorry EBCDIC:ers!).  */
432                                     + 'Z' - 'A' + 1 + 'z' - 'a' + 1
433                                     /* Digits.  */
434                                     + 10
435                                     /* ':' and '_'.  */
436                                     + 1 + 1
437                                     /* Codes higher than 126.  */
438                                     + 256 - 126
439                                     /* Ending zero.  */
440                                     + 1];
441
442
443 /* Get section SECNAME or create one if it doesn't exist.  When creating
444    one, new memory for the name is allocated.  */
445
446 static asection *
447 mmo_make_section (abfd, secname)
448      bfd *abfd;
449      const char *secname;
450 {
451   asection *sec = bfd_get_section_by_name (abfd, secname);
452
453   if (sec == NULL)
454     {
455       char *newsecname = strdup (secname);
456
457       if (newsecname == NULL)
458         {
459           (*_bfd_error_handler)
460             (_("%s: No core to allocate section name %s\n"),
461              bfd_get_filename (abfd), secname);
462           bfd_set_error (bfd_error_system_call);
463           return NULL;
464         }
465       sec = bfd_make_section (abfd, newsecname);
466     }
467
468   return sec;
469 }
470
471 /* Nothing to do, but keep as a placeholder if we need it.
472    Note that state that might differ between bfd:s must not be initialized
473    here, nor must it be static.  Add it to tdata information instead.  */
474
475 static void
476 mmo_init ()
477 {
478   static boolean inited = false;
479   int i = 0;
480   int j = 0;
481   static const char letters[]
482     = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:_";
483
484   if (inited)
485     return;
486   inited = true;
487
488   /* Fill in the set of valid symbol characters.  */
489   strcpy (valid_mmo_symbol_character_set, letters);
490   i = strlen (letters);
491
492   for (j = 126; j < 256; j++)
493     valid_mmo_symbol_character_set[i++] = j;
494 }
495
496 /* Check whether an existing file is an mmo file.  */
497
498 static const bfd_target *
499 mmo_object_p (abfd)
500      bfd *abfd;
501 {
502   struct stat statbuf;
503   bfd_byte b[4];
504
505   mmo_init ();
506
507   if (bfd_stat (abfd, &statbuf) < 0
508       || bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
509       || bfd_bread (b, 4, abfd) != 4)
510     goto bad_final;
511
512   /* All mmo files are a multiple of four bytes long.
513      Only recognize version one.  */
514   if ((statbuf.st_size % 4) != 0
515       || b[0] != LOP || b[1] != LOP_PRE || b[2] != 1)
516     goto bad_format;
517
518   /* Get the last 32-bit word.  */
519   if (bfd_seek (abfd, (file_ptr) statbuf.st_size - 4, SEEK_SET) != 0
520       || bfd_bread (b, 4, abfd) != 4)
521     goto bad_final;
522
523   /* Check if the file ends in a lop_end lopcode. */
524   if (b[0] != LOP || b[1] != LOP_END || ! mmo_mkobject (abfd))
525     goto bad_format;
526
527   /* Compute an upper bound on the max symbol length.  Not really
528      important as all of the symbol information can only be 256k.  */
529   abfd->tdata.mmo_data->max_symbol_length = (b[2] * 256 + b[3]) * 4;
530   abfd->tdata.mmo_data->lop_stab_symbol
531     = bfd_malloc (abfd->tdata.mmo_data->max_symbol_length + 1);
532
533   if (abfd->tdata.mmo_data->lop_stab_symbol == NULL)
534     {
535       (*_bfd_error_handler)
536         (_("%s: No core to allocate a symbol %d bytes long\n"),
537          bfd_get_filename (abfd), abfd->tdata.mmo_data->max_symbol_length);
538       goto bad_final;
539     }
540
541   /* Read in everything.  */
542   if (! mmo_scan (abfd))
543     goto bad_format_free;
544
545   if (abfd->symcount > 0)
546     abfd->flags |= HAS_SYMS;
547
548   /* You'll have to tweak this if you want to use this format for other
549      arches (not recommended due to its small-size limitations).  Look at
550      the ELF format for how to make it target-generic.  */
551   if (! bfd_default_set_arch_mach (abfd, bfd_arch_mmix, 0))
552     goto bad_format_free;
553
554   return abfd->xvec;
555
556  bad_format_free:
557   free (abfd->tdata.mmo_data->lop_stab_symbol);
558  bad_format:
559   bfd_set_error (bfd_error_wrong_format);
560  bad_final:
561   return NULL;
562 }
563
564 /* Set up the mmo tdata information.  */
565
566 static boolean
567 mmo_mkobject (abfd)
568      bfd *abfd;
569 {
570   mmo_init ();
571
572   if (abfd->tdata.mmo_data == NULL)
573     {
574       time_t created;
575
576       /* All fields are zero-initialized, so we don't have to explicitly
577          initialize most.  */
578       tdata_type *tdata = (tdata_type *) bfd_zmalloc (sizeof (tdata_type));
579       if (tdata == NULL)
580         return false;
581
582       created = time (NULL);
583       bfd_put_32 (abfd, created, tdata->created);
584
585       abfd->tdata.mmo_data = tdata;
586     }
587
588   return true;
589 }
590
591 static boolean
592 mmo_bfd_copy_private_bfd_data (ibfd, obfd)
593      bfd *ibfd;
594      bfd *obfd;
595 {
596   if (bfd_get_flavour (ibfd) != bfd_target_mmo_flavour
597       || bfd_get_flavour (obfd) != bfd_target_mmo_flavour)
598     return true;
599
600   /* Copy the time the copied-from file was created.  If people want the
601      time the file was last *modified*, they have that in the normal file
602      information.  */
603   memcpy (obfd->tdata.mmo_data->created, ibfd->tdata.mmo_data->created,
604           sizeof (obfd->tdata.mmo_data->created));
605   return true;
606 }
607
608 /* Helper functions for mmo_decide_section, used through
609    bfd_map_over_sections.  */
610
611 static void
612 mmo_find_sec_w_addr (abfd, sec, p)
613      bfd *abfd ATTRIBUTE_UNUSED;
614      asection *sec;
615      PTR p;
616 {
617   struct mmo_find_sec_info *infop = (struct mmo_find_sec_info *) p;
618   bfd_vma vma = bfd_get_section_vma (abfd, sec);
619
620   /* Ignore sections that aren't loaded.  */
621   if ((bfd_get_section_flags (abfd, sec) & (SEC_LOAD | SEC_ALLOC))
622       !=  (SEC_LOAD | SEC_ALLOC))
623     return;
624
625   if (infop->addr >= vma && infop->addr < vma + sec->_raw_size)
626     infop->sec = sec;
627 }
628
629 static void
630 mmo_find_sec_w_addr_grow (abfd, sec, p)
631      bfd *abfd ATTRIBUTE_UNUSED;
632      asection *sec;
633      PTR p;
634 {
635   struct mmo_find_sec_info *infop = (struct mmo_find_sec_info *) p;
636   bfd_vma vma = bfd_get_section_vma (abfd, sec);
637
638   /* Ignore sections that aren't loaded.  */
639   if ((bfd_get_section_flags (abfd, sec) & (SEC_LOAD | SEC_ALLOC))
640       !=  (SEC_LOAD | SEC_ALLOC))
641     return;
642
643   if (infop->addr >= vma && infop->addr < vma + MAX_ARTIFICIAL_SECTION_SIZE)
644     infop->sec = sec;
645 }
646
647 /* Find a section that corresponds to a VMA.  Automatically create .text
648    or .data and set current section to it, depending on what vma.  If we
649    can't deduce a section, make one up as ".MMIX.sec.N", where N is an
650    increasing number.  */
651
652 static asection *
653 mmo_decide_section (abfd, vma)
654      bfd *abfd;
655      bfd_vma vma;
656 {
657   asection *sec = NULL;
658   char sec_name[sizeof (".MMIX.sec.") + 20];
659   struct mmo_find_sec_info info;
660
661   info.addr = vma;
662   info.sec = NULL;
663
664   /* First see if there's a section that would match exactly.  */
665   bfd_map_over_sections (abfd, mmo_find_sec_w_addr, &info);
666
667   if (info.sec != NULL)
668     return info.sec;
669
670   /* If there's no such section, try and expand one of the existing ones,
671      up to a limit.  Make sure we have .text and .data before we try that;
672      create them corresponding to expected addresses and set flags to make
673      them match the "loaded and with contents" expectation.  */
674   if ((vma >> 56) == 0)
675     {
676       sec = bfd_make_section_old_way (abfd, MMO_TEXT_SECTION_NAME);
677
678       if (sec == NULL)
679         return NULL;
680
681       if (! sec->user_set_vma)
682         bfd_set_section_vma (abfd, sec, vma);
683       if (! bfd_set_section_flags (abfd, sec,
684                                    bfd_get_section_flags (abfd, sec)
685                                    | SEC_CODE | SEC_LOAD | SEC_ALLOC))
686         return NULL;
687     }
688   else if ((vma >> 56) == 0x20)
689     {
690       sec = bfd_make_section_old_way (abfd, MMO_DATA_SECTION_NAME);
691
692       if (sec == NULL)
693         return NULL;
694
695       if (! sec->user_set_vma)
696         bfd_set_section_vma (abfd, sec, vma);
697       if (! bfd_set_section_flags (abfd, sec,
698                                    bfd_get_section_flags (abfd, sec)
699                                    | SEC_LOAD | SEC_ALLOC))
700         return NULL;
701     }
702
703   bfd_map_over_sections (abfd, mmo_find_sec_w_addr_grow, &info);
704
705   if (info.sec != NULL)
706     return info.sec;
707
708   /* If there's still no suitable section, make a new one.  */
709   sprintf (sec_name, ".MMIX.sec.%d", abfd->tdata.mmo_data->sec_no++);
710   sec = mmo_make_section (abfd, sec_name);
711   if (! sec->user_set_vma)
712     bfd_set_section_vma (abfd, sec, vma);
713
714   if (! bfd_set_section_flags (abfd, sec,
715                                bfd_get_section_flags (abfd, sec)
716                                | SEC_LOAD | SEC_ALLOC))
717     return NULL;
718   return sec;
719 }
720
721 /* Xor in a 64-bit value VALUE at VMA.  */
722
723 static INLINE void
724 mmo_xore_64 (sec, vma, value)
725      asection *sec;
726      bfd_vma vma;
727      bfd_vma value;
728 {
729   bfd_byte *loc = mmo_get_loc (sec, vma, 8);
730   bfd_vma prev = bfd_get_64 (sec->owner, loc);
731
732   value ^= prev;
733   bfd_put_64 (sec->owner, value, loc);
734 }
735
736 /* Xor in a 32-bit value VALUE at VMA.  */
737
738 static INLINE void
739 mmo_xore_32 (sec, vma, value)
740      asection *sec;
741      bfd_vma vma;
742      unsigned int value;
743 {
744   bfd_byte *loc = mmo_get_loc (sec, vma, 4);
745   unsigned int prev = bfd_get_32 (sec->owner, loc);
746
747   value ^= prev;
748   bfd_put_32 (sec->owner, value, loc);
749 }
750
751 /* Xor in a 16-bit value VALUE at VMA.  */
752
753 static INLINE void
754 mmo_xore_16 (sec, vma, value)
755      asection *sec;
756      bfd_vma vma;
757      unsigned int value;
758 {
759   bfd_byte *loc = mmo_get_loc (sec, vma, 2);
760   unsigned int prev = bfd_get_16 (sec->owner, loc);
761
762   value ^= prev;
763   bfd_put_16 (sec->owner, value, loc);
764 }
765
766 /* Write a 32-bit word to output file, no lop_quote generated.  */
767
768 static INLINE void
769 mmo_write_tetra_raw (abfd, value)
770      bfd *abfd;
771      unsigned int value;
772 {
773   bfd_byte buf[4];
774
775   bfd_put_32 (abfd, value, buf);
776
777   if (bfd_bwrite ((PTR) buf, 4, abfd) != 4)
778     abfd->tdata.mmo_data->have_error = true;
779 }
780
781 /* Write a 32-bit word to output file; lop_quote if necessary.  */
782
783 static INLINE void
784 mmo_write_tetra (abfd, value)
785      bfd *abfd;
786      unsigned int value;
787 {
788   if (((value >> 24) & 0xff) == LOP)
789     mmo_write_tetra_raw (abfd, LOP_QUOTE_NEXT);
790
791   mmo_write_tetra_raw (abfd, value);
792 }
793
794 /* Write a 64-bit word to output file, perhaps with lop_quoting.  */
795
796 static INLINE void
797 mmo_write_octa (abfd, value)
798      bfd *abfd;
799      bfd_vma value;
800 {
801   mmo_write_tetra (abfd, (unsigned int) (value >> 32));
802   mmo_write_tetra (abfd, (unsigned int) value);
803 }
804
805 /* Write a 64-bit word to output file, without lop_quoting.  */
806
807 static INLINE void
808 mmo_write_octa_raw (abfd, value)
809      bfd *abfd;
810      bfd_vma value;
811 {
812   mmo_write_tetra_raw (abfd, (unsigned int) (value >> 32));
813   mmo_write_tetra_raw (abfd, (unsigned int) value);
814 }
815
816 /* Write quoted contents.  Intended to be called multiple times in
817    sequence, followed by a call to mmo_flush_chunk.  */
818
819 static INLINE boolean
820 mmo_write_chunk (abfd, loc, len)
821      bfd *abfd;
822      const bfd_byte *loc;
823      unsigned int len;
824 {
825   boolean retval = true;
826
827   /* Fill up a tetra from bytes remaining from a previous chunk.  */
828   if (abfd->tdata.mmo_data->byte_no != 0)
829     {
830       while (abfd->tdata.mmo_data->byte_no < 4 && len != 0)
831         {
832           abfd->tdata.mmo_data->buf[abfd->tdata.mmo_data->byte_no++] = *loc++;
833           len--;
834         }
835
836       if (abfd->tdata.mmo_data->byte_no == 4)
837         {
838           mmo_write_tetra (abfd,
839                            bfd_get_32 (abfd, abfd->tdata.mmo_data->buf));
840           abfd->tdata.mmo_data->byte_no = 0;
841         }
842     }
843
844   while (len >= 4)
845     {
846       if (loc[0] == LOP)
847         mmo_write_tetra_raw (abfd, LOP_QUOTE_NEXT);
848
849       retval = (retval
850                 && ! abfd->tdata.mmo_data->have_error
851                 && 4 == bfd_bwrite ((PTR) loc, 4, abfd));
852
853       loc += 4;
854       len -= 4;
855     }
856
857   if (len)
858     {
859       memcpy (abfd->tdata.mmo_data->buf, loc, len);
860       abfd->tdata.mmo_data->byte_no = len;
861     }
862
863   if (! retval)
864     abfd->tdata.mmo_data->have_error = true;
865   return retval;
866 }
867
868 /* Flush remaining bytes, from a previous mmo_write_chunk, zero-padded to
869    4 bytes.  */
870
871 static INLINE boolean
872 mmo_flush_chunk (abfd)
873      bfd *abfd;
874 {
875   if (abfd->tdata.mmo_data->byte_no != 0)
876     {
877       memset (abfd->tdata.mmo_data->buf + abfd->tdata.mmo_data->byte_no,
878               0, 4 - abfd->tdata.mmo_data->byte_no);
879       mmo_write_tetra (abfd,
880                        bfd_get_32 (abfd, abfd->tdata.mmo_data->buf));
881       abfd->tdata.mmo_data->byte_no = 0;
882     }
883
884   return ! abfd->tdata.mmo_data->have_error;
885 }
886
887 /* Same, but from a list.  */
888
889 static INLINE boolean
890 mmo_write_chunk_list (abfd, datap)
891      bfd *abfd;
892      mmo_data_list_type *datap;
893 {
894   for (; datap != NULL; datap = datap->next)
895     if (! mmo_write_chunk (abfd, datap->data, datap->size))
896       return false;
897
898   return mmo_flush_chunk (abfd);
899 }
900
901 /* Write a lop_loc and some contents.  A caller needs to call
902    mmo_flush_chunk after calling this function.  The location is only
903    output if different than *LAST_VMAP, which is updated after this call.  */
904
905 static boolean
906 mmo_write_loc_chunk (abfd, vma, loc, len, last_vmap)
907      bfd *abfd;
908      bfd_vma vma;
909      const bfd_byte *loc;
910      unsigned int len;
911      bfd_vma *last_vmap;
912 {
913   /* Find an initial and trailing section of zero tetras; we don't need to
914      write out zeros.  FIXME: When we do this, we should emit section size
915      and address specifiers, else objcopy can't always perform an identity
916      translation.  */
917   while (len >= 4 && bfd_get_32 (abfd, loc) == 0)
918     {
919       vma += 4;
920       len -= 4;
921       loc += 4;
922     }
923
924   while (len >= 4 && bfd_get_32 (abfd, loc + len - 4) == 0)
925     len -= 4;
926
927   /* Only write out the location if it's different than the one the caller
928      (supposedly) previously handled, accounting for omitted leading zeros.  */
929   if (vma != *last_vmap)
930     {
931       /* We might be in the middle of a sequence.  */
932       mmo_flush_chunk (abfd);
933
934       /* We always write the location as 64 bits; no use saving bytes
935          here.  */
936       mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_LOC << 16) | 2);
937       mmo_write_octa_raw (abfd, vma);
938     }
939
940   /* Update to reflect end of this chunk, with trailing zeros omitted.  */
941   *last_vmap = vma + len;
942
943   return (! abfd->tdata.mmo_data->have_error
944           && mmo_write_chunk (abfd, loc, len));
945 }
946
947 /* Same, but from a list.  */
948
949 static INLINE boolean
950 mmo_write_loc_chunk_list (abfd, datap)
951      bfd *abfd;
952      mmo_data_list_type *datap;
953 {
954   /* Get an address different than the address of the first chunk.  */
955   bfd_vma last_vma = datap ? datap->where - 1 : 0;
956
957   for (; datap != NULL; datap = datap->next)
958     if (! mmo_write_loc_chunk (abfd, datap->where, datap->data, datap->size,
959                                &last_vma))
960       return false;
961
962   return mmo_flush_chunk (abfd);
963 }
964
965 /* Make a .MMIX.spec_data.N section.  */
966
967 static asection *
968 mmo_get_generic_spec_data_section (abfd, spec_data_number)
969      bfd *abfd;
970      int spec_data_number;
971 {
972   asection *sec;
973   char secname[sizeof (MMIX_OTHER_SPEC_SECTION_PREFIX) + 20]
974     = MMIX_OTHER_SPEC_SECTION_PREFIX;
975
976   sprintf (secname + strlen (MMIX_OTHER_SPEC_SECTION_PREFIX),
977            "%d", spec_data_number);
978
979   sec = mmo_make_section (abfd, secname);
980
981   return sec;
982 }
983
984 /* Make a special section for SPEC_DATA_NUMBER.  If it is the one we use
985    ourselves, parse some of its data to get at the section name.  */
986
987 static asection *
988 mmo_get_spec_section (abfd, spec_data_number)
989      bfd *abfd;
990      int spec_data_number;
991 {
992   bfd_byte *secname;
993   asection *sec;
994   bfd_byte buf[4];
995   unsigned int secname_length;
996   unsigned int i;
997   bfd_vma section_length;
998   bfd_vma section_vma;
999   mmo_data_list_type *loc;
1000   flagword flags;
1001   long orig_pos;
1002
1003   /* If this isn't the "special" special data, then make a placeholder
1004      section.  */
1005   if (spec_data_number != SPEC_DATA_SECTION)
1006     return mmo_get_generic_spec_data_section (abfd, spec_data_number);
1007
1008   /* Seek back to this position if there was a format error.  */
1009   orig_pos = bfd_tell (abfd);
1010
1011   /* Read the length (in 32-bit words).  */
1012   if (bfd_bread (buf, 4, abfd) != 4)
1013     goto format_error;
1014
1015   if (buf[0] == LOP)
1016     {
1017       if (buf[1] != LOP_QUOTE)
1018         goto format_error;
1019
1020       if (bfd_bread (buf, 4, abfd) != 4)
1021         goto format_error;
1022     }
1023
1024   /* We don't care to keep the name length accurate.  It's
1025      zero-terminated.  */
1026   secname_length = bfd_get_32 (abfd, buf) * 4;
1027
1028   /* Check section name length for sanity.  */
1029   if (secname_length > MAX_SECTION_NAME_SIZE)
1030     goto format_error;
1031
1032   /* This should be free'd regardless if a section is created.  */
1033   secname = bfd_malloc (secname_length + 1);
1034   secname[secname_length] = 0;
1035
1036   for (i = 0; i < secname_length / 4; i++)
1037     {
1038       if (bfd_bread (secname + i * 4, 4, abfd) != 4)
1039         goto format_error_free;
1040
1041       if (secname[i * 4] == LOP)
1042         {
1043           /* A bit of overkill, but we handle char 0x98 in a section name,
1044              and recognize misparsing.  */
1045           if (secname[i * 4 + 1] != LOP_QUOTE
1046               || bfd_bread (secname + i * 4, 4, abfd) != 4)
1047             /* Whoops.  We thought this was a name, and now we found a
1048                non-lop_quote lopcode before we parsed the whole length of
1049                the name.  Signal end-of-file in the same manner.  */
1050               goto format_error_free;
1051         }
1052     }
1053
1054   /* Get the section flags.  */
1055   if (bfd_bread (buf, 4, abfd) != 4
1056       || (buf[0] == LOP
1057           && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1058     goto format_error_free;
1059
1060   flags = bfd_get_32 (abfd, buf);
1061
1062   /* Get the section length.  */
1063   if (bfd_bread (buf, 4, abfd) != 4
1064       || (buf[0] == LOP
1065           && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1066     goto format_error_free;
1067
1068   section_length = (bfd_vma) bfd_get_32 (abfd, buf) << 32;
1069
1070   /* That's the first, high-part.  Now get the low part.  */
1071
1072   if (bfd_bread (buf, 4, abfd) != 4
1073       || (buf[0] == LOP
1074           && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1075     goto format_error_free;
1076
1077   section_length |= (bfd_vma) bfd_get_32 (abfd, buf);
1078
1079   /* Check the section length for sanity.  */
1080   if (section_length > MAX_ARTIFICIAL_SECTION_SIZE)
1081     goto format_error_free;
1082
1083   /* Get the section VMA.  */
1084   if (bfd_bread (buf, 4, abfd) != 4
1085       || (buf[0] == LOP
1086           && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1087     goto format_error_free;
1088
1089   section_vma = (bfd_vma) bfd_get_32 (abfd, buf) << 32;
1090
1091   /* That's the first, high-part.  Now get the low part.  */
1092   if (bfd_bread (buf, 4, abfd) != 4
1093       || (buf[0] == LOP
1094           && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1095     goto format_error_free;
1096
1097   section_vma |= (bfd_vma) bfd_get_32 (abfd, buf);
1098
1099   sec = mmo_make_section (abfd, secname);
1100   free (secname);
1101   if (sec == NULL)
1102     goto format_error;
1103
1104   /* We allocate a buffer here for the advertised size, with head room for
1105      tetrabyte alignment.  */
1106   loc = bfd_zmalloc (section_length + 3
1107                      + sizeof (struct mmo_data_list_struct));
1108   if (loc == NULL)
1109     goto format_error;
1110
1111   /* Use a TETRA-rounded size for the allocated buffer; we set the
1112      "visible" section size below.  */
1113   loc->size = (section_length + 3) & ~3;
1114
1115   /* Add in the section flags we found to those bfd entered during this
1116      process and set the contents.  */
1117   if (! bfd_set_section_flags (abfd, sec,
1118                                bfd_sec_flags_from_mmo_flags (flags)
1119                                | bfd_get_section_flags (abfd, sec)
1120                                | (section_length != 0 ? SEC_HAS_CONTENTS : 0))
1121       || ! bfd_set_section_size (abfd, sec,
1122                                  sec->_cooked_size + section_length)
1123       /* Set VMA only for the first occurrence.  */
1124       || (! sec->user_set_vma
1125           && ! bfd_set_section_vma  (abfd, sec, section_vma)))
1126     {
1127       /* If we get an error for any of the calls above, signal more than
1128          just a format error for the spec section.  */
1129       return NULL;
1130     }
1131
1132   loc->next = NULL;
1133   if (((struct mmo_section_data_struct *) (sec->used_by_bfd))->tail != NULL)
1134     ((struct mmo_section_data_struct *) (sec->used_by_bfd))->tail->next
1135       = loc;
1136   else
1137     ((struct mmo_section_data_struct *) (sec->used_by_bfd))->head = loc;
1138   ((struct mmo_section_data_struct *) (sec->used_by_bfd))->tail = loc;
1139   loc->where = section_vma;
1140
1141   return sec;
1142
1143  format_error_free:
1144   free (secname);
1145  format_error:
1146   if (bfd_seek (abfd, orig_pos, SEEK_SET) != 0)
1147     return NULL;
1148
1149   return mmo_get_generic_spec_data_section (abfd, spec_data_number);
1150 }
1151
1152 /* Read a byte, but read from file in multiples of 32-bit words.  */
1153
1154 static bfd_byte
1155 mmo_get_byte (abfd)
1156      bfd *abfd;
1157 {
1158   bfd_byte retval;
1159
1160   if (abfd->tdata.mmo_data->byte_no == 0)
1161     {
1162       if (! abfd->tdata.mmo_data->have_error
1163           && bfd_bread (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
1164         {
1165           abfd->tdata.mmo_data->have_error = true;
1166
1167           /* A value somewhat safe against tripping on some inconsistency
1168              when mopping up after this error.  */
1169           return 128;
1170         }
1171     }
1172
1173   retval = abfd->tdata.mmo_data->buf[abfd->tdata.mmo_data->byte_no];
1174   abfd->tdata.mmo_data->byte_no = (abfd->tdata.mmo_data->byte_no + 1) % 4;
1175
1176   return retval;
1177 }
1178
1179 /* Write a byte, in multiples of 32-bit words.  */
1180
1181 static void
1182 mmo_write_byte (abfd, value)
1183      bfd *abfd;
1184      bfd_byte value;
1185 {
1186   abfd->tdata.mmo_data->buf[(abfd->tdata.mmo_data->byte_no++ % 4)] = value;
1187   if ((abfd->tdata.mmo_data->byte_no % 4) == 0)
1188     {
1189       if (! abfd->tdata.mmo_data->have_error
1190           && bfd_bwrite (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
1191         abfd->tdata.mmo_data->have_error = true;
1192     }
1193 }
1194
1195 /* Create a symbol.  */
1196
1197 static boolean
1198 mmo_create_symbol (abfd, symname, addr, sym_type, serno)
1199      bfd *abfd;
1200      const char *symname;
1201      bfd_vma addr;
1202      enum mmo_sym_type sym_type;
1203      unsigned int serno;
1204 {
1205   struct mmo_symbol *n;
1206
1207   n = (struct mmo_symbol *) bfd_alloc (abfd, sizeof (struct mmo_symbol));
1208   if (n == NULL)
1209     return false;
1210
1211   n->name = bfd_alloc (abfd, strlen (symname) + 1);
1212   if (n->name == NULL)
1213     return false;
1214
1215   strcpy ((PTR) n->name, symname);
1216
1217   n->value = addr;
1218   n->sym_type = sym_type;
1219   n->serno = serno;
1220
1221   if (abfd->tdata.mmo_data->symbols == NULL)
1222     abfd->tdata.mmo_data->symbols = n;
1223   else
1224     abfd->tdata.mmo_data->symtail->next = n;
1225   abfd->tdata.mmo_data->symtail = n;
1226   n->next = NULL;
1227
1228   ++abfd->symcount;
1229
1230   /* Check that :Main equals the last octa of the .MMIX.reg_contents
1231      section, as it's the one place we're sure to pass when reading a mmo
1232      object.  For written objects, we do it while setting the symbol
1233      table.  */
1234   if (strcmp (symname, MMIX_START_SYMBOL_NAME) == 0
1235       && bfd_get_start_address (abfd) != addr)
1236     {
1237       (*_bfd_error_handler)
1238         (_("%s: invalid mmo file: initialization value for $255 is not `Main'\n"),
1239          bfd_get_filename (abfd));
1240       bfd_set_error (bfd_error_bad_value);
1241       return false;
1242     }
1243
1244   return true;
1245 }
1246
1247 /* Read in symbols.  */
1248
1249 static boolean
1250 mmo_get_symbols (abfd)
1251      bfd *abfd;
1252 {
1253 /*
1254 INODE
1255 Symbol-table, mmo section mapping, File layout, mmo
1256 SUBSECTION
1257         Symbol table format
1258
1259         From mmixal.w (or really, the generated mmixal.tex) in
1260         @url{http://www-cs-faculty.stanford.edu/~knuth/programs/mmix.tar.gz}):
1261         ``Symbols are stored and retrieved by means of a @samp{ternary
1262         search trie}, following ideas of Bentley and Sedgewick. (See
1263         ACM--SIAM Symp.@: on Discrete Algorithms @samp{8} (1997), 360--369;
1264         R.@:Sedgewick, @samp{Algorithms in C} (Reading, Mass.@:
1265         Addison--Wesley, 1998), @samp{15.4}.)  Each trie node stores a
1266         character, and there are branches to subtries for the cases where
1267         a given character is less than, equal to, or greater than the
1268         character in the trie.  There also is a pointer to a symbol table
1269         entry if a symbol ends at the current node.''
1270
1271         So it's a tree encoded as a stream of bytes.  The stream of bytes
1272         acts on a single virtual global symbol, adding and removing
1273         characters and signalling complete symbol points.  Here, we read
1274         the stream and create symbols at the completion points.
1275
1276         First, there's a control byte <<m>>.  If any of the listed bits
1277         in <<m>> is nonzero, we execute what stands at the right, in
1278         the listed order:
1279
1280 | (MMO3_LEFT)
1281 | 0x40 - Traverse left trie.
1282 |        (Read a new command byte and recurse.)
1283 |
1284 | (MMO3_SYMBITS)
1285 | 0x2f - Read the next byte as a character and store it in the
1286 |        current character position; increment character position.
1287 |        Test the bits of <<m>>:
1288 |
1289 |        (MMO3_WCHAR)
1290 |        0x80 - The character is 16-bit (so read another byte,
1291 |               merge into current character.
1292 |
1293 |        (MMO3_TYPEBITS)
1294 |        0xf  - We have a complete symbol; parse the type, value
1295 |               and serial number and do what should be done
1296 |               with a symbol.  The type and length information
1297 |               is in j = (m & 0xf).
1298 |
1299 |               (MMO3_REGQUAL_BITS)
1300 |               j == 0xf: A register variable.  The following
1301 |                         byte tells which register.
1302 |               j <= 8:   An absolute symbol.  Read j bytes as the
1303 |                         big-endian number the symbol equals.
1304 |                         A j = 2 with two zero bytes denotes an
1305 |                         unknown symbol.
1306 |               j > 8:    As with j <= 8, but add (0x20 << 56)
1307 |                         to the value in the following j - 8
1308 |                         bytes.
1309 |
1310 |               Then comes the serial number, as a variant of
1311 |               uleb128, but better named ubeb128:
1312 |               Read bytes and shift the previous value left 7
1313 |               (multiply by 128).  Add in the new byte, repeat
1314 |               until a byte has bit 7 set.  The serial number
1315 |               is the computed value minus 128.
1316 |
1317 |        (MMO3_MIDDLE)
1318 |        0x20 - Traverse middle trie.  (Read a new command byte
1319 |               and recurse.)  Decrement character position.
1320 |
1321 | (MMO3_RIGHT)
1322 | 0x10 - Traverse right trie.  (Read a new command byte and
1323 |        recurse.)
1324
1325         Let's look again at the <<lop_stab>> for the trivial file
1326         (@pxref{File layout}).
1327
1328 | 0x980b0000 - lop_stab for ":Main" = 0, serial 1.
1329 | 0x203a4040
1330 | 0x10404020
1331 | 0x4d206120
1332 | 0x69016e00
1333 | 0x81000000
1334
1335         This forms the trivial trie (note that the path between ``:'' and
1336         ``M'' is redundant):
1337
1338 | 203a     ":"
1339 | 40       /
1340 | 40      /
1341 | 10      \
1342 | 40      /
1343 | 40     /
1344 | 204d  "M"
1345 | 2061  "a"
1346 | 2069  "i"
1347 | 016e  "n" is the last character in a full symbol, and
1348 |       with a value represented in one byte.
1349 | 00    The value is 0.
1350 | 81    The serial number is 1.  */
1351
1352   bfd_byte m = mmo_get_byte (abfd);
1353
1354   /* Check first if we have a bad hair day.  */
1355   if (abfd->tdata.mmo_data->have_error)
1356     return false;
1357
1358   if (m & MMO3_LEFT)
1359     /* Traverse left trie. */
1360     mmo_get_symbols (abfd);
1361
1362   if (m & MMO3_SYMBITS)
1363     {
1364       bfd_byte c = mmo_get_byte (abfd);
1365       bfd_byte j = m & MMO3_TYPEBITS;
1366       bfd_vma addr = 0;
1367       enum mmo_sym_type sym_type;
1368       unsigned int serno = 0;
1369       bfd_byte k;
1370
1371       if (m & MMO3_WCHAR)
1372         {
1373           bfd_byte c2 = mmo_get_byte (abfd);
1374
1375           /* A two-byte character.  We can't grok this, but neither can
1376              mmotype, for other cases than the second byte being zero.  */
1377
1378           if (c != 0)
1379             {
1380               abfd->tdata.mmo_data->lop_stab_symbol
1381                 [abfd->tdata.mmo_data->symbol_position] = 0;
1382
1383               (*_bfd_error_handler)
1384                 (_("%s: unsupported wide character sequence\
1385  0x%02X 0x%02X after symbol name starting with `%s'\n"),
1386                  bfd_get_filename (abfd), c, c2,
1387                  abfd->tdata.mmo_data->lop_stab_symbol);
1388               bfd_set_error (bfd_error_bad_value);
1389               abfd->tdata.mmo_data->have_error = true;
1390               return false;
1391             }
1392           else
1393             c = c2;
1394         }
1395
1396       abfd->tdata.mmo_data->lop_stab_symbol[abfd->tdata.mmo_data->symbol_position++] = c;
1397       abfd->tdata.mmo_data->lop_stab_symbol[abfd->tdata.mmo_data->symbol_position] = 0;
1398
1399       if (j & MMO3_REGQUAL_BITS)
1400         {
1401           if (j == MMO3_REGQUAL_BITS)
1402             {
1403               sym_type = mmo_reg_sym;
1404               addr = mmo_get_byte (abfd);
1405             }
1406           else if (j <= 8)
1407             {
1408               unsigned int i;
1409
1410               for (i = 0; i < j; i++)
1411                 addr = (addr << 8) + mmo_get_byte (abfd);
1412
1413               if (addr == 0 && j == MMO3_UNDEF)
1414                 sym_type = mmo_undef_sym;
1415               else
1416                 sym_type = mmo_abs_sym;
1417             }
1418           else
1419             {
1420               unsigned int i;
1421
1422               for (i = MMO3_DATA; i < j; i++)
1423                 addr = (addr << 8) + mmo_get_byte (abfd);
1424
1425               addr += (bfd_vma) 0x20 << 56;
1426               sym_type = mmo_data_sym;
1427             }
1428
1429           /* Get the serial number.  */
1430           do
1431             {
1432               k = mmo_get_byte (abfd);
1433               serno = (serno << 7) + k;
1434             }
1435           while (k < 128);
1436           serno -= 128;
1437
1438           /* Got it.  Now enter it.  Skip a leading ":".  */
1439           if (! abfd->tdata.mmo_data->have_error
1440               && ! mmo_create_symbol (abfd,
1441                                       abfd->tdata.mmo_data->lop_stab_symbol
1442                                       + 1,
1443                                       addr, sym_type, serno))
1444             abfd->tdata.mmo_data->have_error = true;
1445         }
1446
1447       if (m & MMO3_MIDDLE)
1448         /* Traverse middle trie. */
1449         mmo_get_symbols (abfd);
1450
1451       abfd->tdata.mmo_data->symbol_position--;
1452     }
1453
1454   if (m & MMO3_RIGHT)
1455     /* Traverse right trie.  */
1456     mmo_get_symbols (abfd);
1457
1458   return ! abfd->tdata.mmo_data->have_error;
1459 }
1460
1461 /* Get the location of memory area [VMA..VMA + SIZE - 1], which we think
1462    is in section SEC.  Adjust and reallocate zero-initialized contents.
1463    If there's new contents, allocate to the next multiple of
1464    MMO_SEC_CONTENTS_CHUNK_SIZE.  */
1465
1466 static INLINE bfd_byte *
1467 mmo_get_loc (sec, vma, size)
1468      asection *sec;
1469      bfd_vma vma;
1470      int size;
1471 {
1472   bfd_size_type allocated_size;
1473   struct mmo_section_data_struct *sdatap
1474     = (struct mmo_section_data_struct *) sec->used_by_bfd;
1475   struct mmo_data_list_struct *datap = sdatap->head;
1476   struct mmo_data_list_struct *entry;
1477
1478   /* First search the list to see if we have the requested chunk in one
1479      piece, or perhaps if we have a suitable chunk with room to fit.  */
1480   for (; datap != NULL; datap = datap->next)
1481     {
1482       if (datap->where <= vma
1483           && datap->where + datap->size >= vma + size)
1484         return datap->data + vma - datap->where;
1485       else if (datap->where <= vma
1486                && datap->where + datap->allocated_size >= vma + size
1487                /* Only munch on the "allocated size" if it does not
1488                   overlap the next chunk.  */
1489                && (datap->next == NULL || datap->next->where >= vma + size))
1490         {
1491           /* There was room allocated, but the size wasn't set to include
1492              it.  Do that now.  */
1493           datap->size += (vma + size) - (datap->where + datap->size);
1494
1495           /* Update the section size.  This happens only if we update the
1496              32-bit-aligned chunk size.  Callers that have
1497              non-32-bit-aligned sections should do all allocation and
1498              size-setting by themselves or at least set the section size
1499              after the last allocating call to this function.  */
1500           if (vma + size > sec->vma + sec->_raw_size)
1501             sec->_raw_size += (vma + size) - (sec->vma + sec->_raw_size);
1502
1503           return datap->data + vma - datap->where;
1504         }
1505     }
1506
1507   /* Not found; allocate a new block.  First check in case we get a
1508      request for a size split up over several blocks; we'll have to return
1509      NULL for those cases, requesting the caller to split up the request.
1510      Requests with an address aligned on MMO_SEC_CONTENTS_CHUNK_SIZE bytes and
1511      for no more than MMO_SEC_CONTENTS_CHUNK_SIZE will always get resolved.  */
1512
1513   for (datap = sdatap->head; datap != NULL; datap = datap->next)
1514     if ((datap->where <= vma && datap->where + datap->size > vma)
1515         || (datap->where < vma + size
1516             && datap->where + datap->size >= vma + size))
1517       return NULL;
1518
1519   allocated_size
1520     = (size + MMO_SEC_CONTENTS_CHUNK_SIZE - 1) & ~(MMO_SEC_CONTENTS_CHUNK_SIZE - 1);
1521   entry = (mmo_data_list_type *)
1522     bfd_zalloc (sec->owner, sizeof (mmo_data_list_type) + allocated_size);
1523   if (entry == NULL)
1524     return NULL;
1525   entry->where = vma;
1526   entry->size = size;
1527   entry->allocated_size = allocated_size;
1528
1529   datap = sdatap->head;
1530
1531   /* Sort the records by address.  Optimize for the common case of adding
1532      a record to the end of the list.  */
1533   if (sdatap->tail != NULL && entry->where >= sdatap->tail->where)
1534     {
1535       sdatap->tail->next = entry;
1536       entry->next = NULL;
1537       sdatap->tail = entry;
1538     }
1539   else
1540     {
1541       mmo_data_list_type **look;
1542       for (look = &sdatap->head;
1543            *look != NULL && (*look)->where < entry->where;
1544            look = &(*look)->next)
1545         ;
1546       entry->next = *look;
1547       *look = entry;
1548       if (entry->next == NULL)
1549         {
1550           sdatap->tail = entry;
1551
1552           /* We get here for the first time (at other times too) for this
1553              section.  Say we have contents.  */
1554           if (! bfd_set_section_flags (sec->owner, sec,
1555                                        bfd_get_section_flags (sec->owner, sec)
1556                                        | SEC_HAS_CONTENTS))
1557             return NULL;
1558         }
1559     }
1560
1561   /* Update the section size.  This happens only when we add contents and
1562      re-size as we go.  The section size will then be aligned to 32 bits.  */
1563   if (vma + size > sec->vma + sec->_raw_size)
1564     sec->_raw_size += (vma + size) - (sec->vma + sec->_raw_size);
1565   return entry->data;
1566 }
1567
1568 /* Set sizes once we've read in all sections.  */
1569
1570 static void
1571 mmo_map_set_sizes (abfd, sec, ignored)
1572      bfd *abfd ATTRIBUTE_UNUSED;
1573      asection *sec;
1574      PTR ignored ATTRIBUTE_UNUSED;
1575 {
1576   sec->_cooked_size = sec->_raw_size;
1577   sec->lma = sec->vma;
1578 }
1579
1580 /* Read the mmo file and turn it into sections.  */
1581
1582 static boolean
1583 mmo_scan (abfd)
1584      bfd *abfd;
1585 {
1586   unsigned int i;
1587   unsigned int lineno = 1;
1588   boolean error = false;
1589   bfd_vma vma = 0;
1590   asection *sec = bfd_make_section_old_way (abfd, MMO_TEXT_SECTION_NAME);
1591   asection *non_spec_sec = NULL;
1592   bfd_vma non_spec_vma = 0;
1593   char *current_filename = NULL;
1594   bfd_size_type nbytes_read = 0;
1595   /* Buffer with room to read a 64-bit value.  */
1596   bfd_byte buf[8];
1597   long stab_loc = -1;
1598   char *file_names[256];
1599
1600   memset (file_names, 0, sizeof (file_names));
1601
1602   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
1603     goto error_return;
1604
1605   while ((nbytes_read = bfd_bread (buf, 4, abfd)) == 4)
1606     {
1607       if (buf[0] == LOP)
1608         {
1609           unsigned int y = bfd_get_8 (abfd, buf + 2);
1610           unsigned int z = bfd_get_8 (abfd, buf + 3);
1611
1612           /* Change back to the original section for lopcodes other
1613              than LOP_QUOTE that comes after a LOP_SPEC.  */
1614           if ((buf[1] != LOP_QUOTE || y != 0 || z != 1)
1615               && non_spec_sec != NULL)
1616             {
1617               sec = non_spec_sec;
1618               vma = non_spec_vma;
1619               non_spec_sec = NULL;
1620             }
1621
1622           switch (buf[1])
1623             {
1624             default:
1625               (*_bfd_error_handler)
1626                 (_("%s: invalid mmo file: unsupported lopcode `%d'\n"),
1627                  bfd_get_filename (abfd), buf[1]);
1628               bfd_set_error (bfd_error_bad_value);
1629               goto error_return;
1630
1631             case LOP_QUOTE:
1632               /* Quote the next 32-bit word.  */
1633               if (y != 0 || z != 1)
1634                 {
1635                   (*_bfd_error_handler)
1636                     (_("%s: invalid mmo file: expected YZ = 1 got YZ = %d for lop_quote\n"),
1637                      bfd_get_filename (abfd), y*256+z);
1638                   bfd_set_error (bfd_error_bad_value);
1639                   goto error_return;
1640                 }
1641               if (bfd_bread (buf, 4, abfd) != 4)
1642                 goto error_return;
1643
1644               mmo_xore_32 (sec, vma, bfd_get_32 (abfd, buf));
1645               vma += 4;
1646               vma &= ~3;
1647               lineno++;
1648               break;
1649
1650             case LOP_LOC:
1651               /* Set vma (and section).  */
1652               vma = (bfd_vma) y << 56;
1653               if (z == 1)
1654                 {
1655                   /* Get a 32-bit value.  */
1656                   if (bfd_bread (buf, 4, abfd) != 4)
1657                     goto error_return;
1658
1659                   vma += bfd_get_32 (abfd, buf);
1660                 }
1661               else if (z == 2)
1662                 {
1663                   /* Get a 64-bit value.  */
1664                   if (bfd_bread (buf, 8, abfd) != 8)
1665                     goto error_return;
1666
1667                   vma += bfd_get_64 (abfd, buf);
1668                 }
1669               else
1670                 {
1671                   (*_bfd_error_handler)
1672                     (_("%s: invalid mmo file: expected z = 1 or z = 2, got z = %d for lop_loc\n"),
1673                      bfd_get_filename (abfd), z);
1674                   bfd_set_error (bfd_error_bad_value);
1675                   goto error_return;
1676                 }
1677
1678               sec = mmo_decide_section (abfd, vma);
1679               if (sec == NULL)
1680                 goto error_return;
1681               break;
1682
1683             case LOP_SKIP:
1684               /* Move forward within the same section.  */
1685               vma += y * 256 + z;
1686
1687               sec = mmo_decide_section (abfd, vma);
1688               if (sec == NULL)
1689                 goto error_return;
1690               break;
1691
1692             case LOP_FIXO:
1693               /* A fixup: Store the current vma somewhere.  Position using
1694                  same format as LOP_LOC.  */
1695               {
1696                 bfd_vma p = (bfd_vma) y << 56;
1697                 asection *fixosec;
1698
1699                 if (z == 1)
1700                   {
1701                     /* Get a 32-bit value.  */
1702                     if (bfd_bread (buf, 4, abfd) != 4)
1703                       goto error_return;
1704
1705                     p += bfd_get_32 (abfd, buf);
1706                   }
1707                 else if (z == 2)
1708                   {
1709                     /* Get a 64-bit value.  */
1710                     if (bfd_bread (buf, 8, abfd) != 8)
1711                       goto error_return;
1712
1713                     p += bfd_get_64 (abfd, buf);
1714                   }
1715                 else
1716                   {
1717                     (*_bfd_error_handler)
1718                       (_("%s: invalid mmo file: expected z = 1 or z = 2, got z = %d for lop_fixo\n"),
1719                        bfd_get_filename (abfd), z);
1720                     bfd_set_error (bfd_error_bad_value);
1721                     goto error_return;
1722                   }
1723
1724                 /* The section where we store this address might be a
1725                    different one than the current section.  */
1726                 fixosec = mmo_decide_section (abfd, p);
1727                 if (fixosec == NULL)
1728                   goto error_return;
1729                 mmo_xore_64 (fixosec, p, vma);
1730               }
1731             break;
1732
1733             case LOP_FIXR:
1734               /* A fixup: Store YZ of this lopcode into YZ at vma - 4 * yz.  */
1735               {
1736                 unsigned int yz = (y * 256 + z);
1737                 bfd_vma p = vma + 2 - 4 * yz;
1738                 asection *fixrsec = mmo_decide_section (abfd, p);
1739                 if (fixrsec == NULL)
1740                   goto error_return;
1741                 mmo_xore_16 (fixrsec, p, yz);
1742               }
1743             break;
1744
1745             case LOP_FIXRX:
1746               /* A fixup, similar to lop_fixr, but taking larger numbers
1747                  and can change branches into the opposite direction
1748                  (gasp!).  */
1749               {
1750                 bfd_vma delta;
1751                 bfd_vma p;
1752                 asection *fixrsec;
1753
1754                 if (y != 0)
1755                   {
1756                     (*_bfd_error_handler)
1757                       (_("%s: invalid mmo file: expected y = 0, got y = %d for lop_fixrx\n"),
1758                        bfd_get_filename (abfd), y);
1759                     bfd_set_error (bfd_error_bad_value);
1760                     goto error_return;
1761                   }
1762
1763                 if (z != 16 && z != 24)
1764                   {
1765                     (*_bfd_error_handler)
1766                       (_("%s: invalid mmo file: expected z = 16 or z = 24, got z = %d for lop_fixrx\n"),
1767                        bfd_get_filename (abfd), z);
1768                     bfd_set_error (bfd_error_bad_value);
1769                     goto error_return;
1770                   }
1771
1772                 /* Get the next 32-bit value.  */
1773                 if (bfd_bread (buf, 4, abfd) != 4)
1774                   goto error_return;
1775
1776                 delta = bfd_get_32 (abfd, buf);
1777
1778                 /* Do an, ehm, involved calculation for the location of
1779                    the fixup.  See mmixal documentation for a verbose
1780                    explanation.  We follow it verbosely here for the
1781                    readers delight.  */
1782                 if (buf[0] == 0)
1783                   p = vma - 4 * delta;
1784                 else if (buf[0] == 1)
1785                   p = vma - 4 * ((delta & 0xffffff) - (1 << z));
1786                 else
1787                   {
1788                     (*_bfd_error_handler)
1789                       (_("%s: invalid mmo file: leading byte of operand word must be 0 or 1, got %d for lop_fixrx\n"),
1790                        bfd_get_filename (abfd), buf[0]);
1791                     bfd_set_error (bfd_error_bad_value);
1792                     goto error_return;
1793                   }
1794
1795                 fixrsec = mmo_decide_section (abfd, vma);
1796                 if (fixrsec == NULL)
1797                   goto error_return;
1798                 mmo_xore_32 (fixrsec, p, delta);
1799               }
1800             break;
1801
1802             case LOP_FILE:
1803               /* Set current file and perhaps the file name.  Reset line
1804                  number.  */
1805               if (z != 0)
1806                 {
1807                   char *fname = bfd_malloc (z * 4 + 1);
1808
1809                   if (fname == NULL)
1810                     {
1811                       (*_bfd_error_handler)
1812                         (_("%s: cannot allocate file name for file number %d, %d bytes\n"),
1813                          bfd_get_filename (abfd), y, z * 4 + 1);
1814                       bfd_set_error (bfd_error_system_call);
1815                       goto error_return;
1816                     }
1817
1818                   fname[z * 4] = 0;
1819
1820                   for (i = 0; i < z; i++)
1821                     {
1822                       if (bfd_bread (fname + i * 4, 4, abfd) != 4)
1823                         {
1824                           free (fname);
1825                           goto error_return;
1826                         }
1827                     }
1828
1829                   if (file_names[y] != NULL)
1830                     {
1831                       (*_bfd_error_handler)
1832                         (_("%s: invalid mmo file: file number %d `%s',\
1833  was already entered as `%s'\n"),
1834                          bfd_get_filename (abfd), y, fname, file_names[y]);
1835                       bfd_set_error (bfd_error_bad_value);
1836                       goto error_return;
1837                     }
1838
1839                   file_names[y] = fname;
1840                 }
1841
1842               if (file_names[y] == NULL)
1843                 {
1844                   (*_bfd_error_handler)
1845                     (_("%s: invalid mmo file: file name for number %d\
1846  was not specified before use\n"),
1847                      bfd_get_filename (abfd), y);
1848                   bfd_set_error (bfd_error_bad_value);
1849                   goto error_return;
1850                 }
1851
1852               current_filename = file_names[y];
1853               lineno = 0;
1854               break;
1855
1856             case LOP_LINE:
1857               /* Set line number.  */
1858               lineno = y * 256 + z;
1859               /* FIXME: Create a sequence of mmo-specific line number
1860                  entries for each section, then translate into canonical
1861                  format.  */
1862               break;
1863
1864             case LOP_SPEC:
1865               /* Special data follows until the next non-lop_quote
1866                  lopcode.  */
1867               non_spec_sec = sec;
1868               non_spec_vma = vma;
1869               sec = mmo_get_spec_section (abfd, y * 256 + z);
1870               if (sec == NULL)
1871                 goto error_return;
1872
1873               vma = sec->vma;
1874               break;
1875
1876             case LOP_PRE:
1877               {
1878                 /* We ignore header information, except we read in the
1879                    creation time from the first 32-bit word with the time
1880                    in seconds since era.  */
1881                 if (z >= 1
1882                     && bfd_bread (abfd->tdata.mmo_data->created, 4,
1883                                  abfd) != 4)
1884                   goto error_return;
1885
1886                 for (i = 1; i < z; i++)
1887                   if (bfd_bread (buf, 4, abfd) != 4)
1888                     goto error_return;
1889               }
1890               break;
1891
1892             case LOP_POST:
1893               /* This tells of the contents of registers $Z..$255 at
1894                  startup.  We make a section out of it, with VMA = Z * 8,
1895                  but only if Z != 255 or the contents is non-zero.  */
1896               {
1897                 asection *rsec;
1898                 bfd_byte *loc;
1899                 bfd_vma first_octa;
1900                 bfd_vma startaddr_octa;
1901
1902                 /* Read first octaword outside loop to simplify logic when
1903                    excluding the Z == 255, octa == 0 case.  */
1904                 if (bfd_bread (buf, 8, abfd) != 8)
1905                   goto error_return;
1906
1907                 first_octa = bfd_get_64 (abfd, buf);
1908
1909                 /* Don't emit contents for the trivial case which is
1910                    always present; $255 pointing to Main.  */
1911                 if (z != 255)
1912                   {
1913                     rsec
1914                       = bfd_make_section_old_way (abfd,
1915                                                   MMIX_REG_CONTENTS_SECTION_NAME);
1916                     rsec->vma = z * 8;
1917                     loc = mmo_get_loc (rsec, z * 8, (255 - z) * 8);
1918                     bfd_put_64 (abfd, first_octa, loc);
1919
1920                     for (i = z + 1; i < 255; i++)
1921                       {
1922                         if (bfd_bread (loc + (i - z) * 8, 8, abfd) != 8)
1923                           goto error_return;
1924                       }
1925
1926                     /* Read out the last octabyte, and use it to set the
1927                        start address.  */
1928                     if (bfd_bread (buf, 8, abfd) != 8)
1929                       goto error_return;
1930
1931                     startaddr_octa = bfd_get_64 (abfd, buf);
1932                   }
1933                 else
1934                   startaddr_octa = first_octa;
1935
1936                 if (! bfd_set_start_address (abfd, startaddr_octa))
1937                   {
1938                     /* Currently this can't fail, but this should handle
1939                        future failures.  */
1940                     bfd_set_error (bfd_error_bad_value);
1941                     goto error_return;
1942                   }
1943               }
1944               break;
1945
1946             case LOP_STAB:
1947               /* We read in the symbols now, not later.  */
1948               if (y != 0 || z != 0)
1949                 {
1950                   (*_bfd_error_handler)
1951                     (_("%s: invalid mmo file: fields y and z of lop_stab\
1952  non-zero, y: %d, z: %d\n"),
1953                      bfd_get_filename (abfd), y, z);
1954                   bfd_set_error (bfd_error_bad_value);
1955                   goto error_return;
1956                 }
1957
1958               /* Save the location, so we can check that YZ in the LOP_END
1959                  is correct.  */
1960               stab_loc = bfd_tell (abfd);
1961
1962               /* It's not said that an MMO can be without symbols (though
1963                  mmixal will refuse to assemble files without Main), but
1964                  it seems it would still be a valid mmo-file, so allow it.
1965                  We detect the absence of a symbol area in that the upper
1966                  limit is computed (from the lop_end YZ field) as 0.
1967                  Don't call mmo_get_symbols; it can only detect the end of
1968                  a valid symbol trie, not the absence of one.  */
1969               if (abfd->tdata.mmo_data->max_symbol_length != 0
1970                   && ! mmo_get_symbols (abfd))
1971                 goto error_return;
1972               break;
1973
1974             case LOP_END:
1975               {
1976                 /* This must be the last 32-bit word in an mmo file.
1977                    Let's find out.  */
1978                 struct stat statbuf;
1979                 long curpos = bfd_tell (abfd);
1980
1981                 if (bfd_stat (abfd, &statbuf) < 0)
1982                   goto error_return;
1983
1984                 if (statbuf.st_size != curpos)
1985                   {
1986                     (*_bfd_error_handler)
1987                       (_("%s: invalid mmo file: lop_end not last item in\
1988  file\n"),
1989                        bfd_get_filename (abfd));
1990                     bfd_set_error (bfd_error_bad_value);
1991                     goto error_return;
1992                   }
1993
1994                 /* Check that the YZ field is right.  Subtract the size of
1995                    this LOP_END in the calculation; YZ does not include
1996                    it.  */
1997                 if ((long) (y * 256 + z) * 4 != (curpos - stab_loc) - 4)
1998                   {
1999                     (*_bfd_error_handler)
2000                       (_("%s: invalid mmo file: YZ of lop_end (%ld)\
2001  not equal to the number of tetras to the preceding lop_stab (%ld)\n"),
2002                        bfd_get_filename (abfd), (long) (y * 256 + z),
2003                        (curpos - stab_loc - 4)/4);
2004                     bfd_set_error (bfd_error_bad_value);
2005                     goto error_return;
2006                   }
2007
2008                 bfd_map_over_sections (abfd, mmo_map_set_sizes, NULL);
2009                 goto done;
2010               }
2011             }
2012         }
2013       else
2014         {
2015           /* This wasn't a lopcode, so store it in the current section.  */
2016           mmo_xore_32 (sec, vma & ~3, bfd_get_32 (abfd, buf));
2017           vma += 4;
2018           vma &= ~3;
2019           lineno++;
2020         }
2021     }
2022
2023   /* We know this file is a multiple of four bytes (checked in
2024      mmo_object_p), so if we got something other than 0, this was a bad
2025      file (although it's more likely we'll get 0 in that case too).
2026      If we got end-of-file, then there was no lop_stab, so the file has
2027      invalid format.  */
2028
2029   if (nbytes_read != 0)
2030     bfd_set_error (bfd_error_system_call);
2031   else
2032     bfd_set_error (bfd_error_bad_value);
2033
2034  error_return:
2035   error = true;
2036  done:
2037   /* Mark the .text and .data section with their normal attribute if they
2038      contain anything.  This is not redundant wrt. mmo_decide_section,
2039      since that code might never execute, and conversely the alloc+code
2040      section flags must be set then.  */
2041   sec = bfd_get_section_by_name (abfd, MMO_TEXT_SECTION_NAME);
2042   if (sec != NULL
2043       && (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
2044       && ! bfd_set_section_flags (abfd, sec,
2045                                   bfd_get_section_flags (abfd, sec)
2046                                   | SEC_ALLOC | SEC_LOAD | SEC_CODE))
2047     error = true;
2048
2049   sec = bfd_get_section_by_name (abfd, MMO_DATA_SECTION_NAME);
2050   if (sec != NULL
2051       && (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
2052       && ! bfd_set_section_flags (abfd, sec,
2053                                   bfd_get_section_flags (abfd, sec)
2054                                   | SEC_ALLOC | SEC_LOAD))
2055     error = true;
2056
2057   /* Free whatever resources we took.  */
2058   for (i = 0; i < sizeof (file_names) / sizeof (file_names[0]); i++)
2059     if (file_names[i])
2060       free (file_names[i]);
2061   return ! error;
2062 }
2063
2064 /* A hook to set up object file dependent section information.  For mmo,
2065    we point out the shape of allocated section contents.  */
2066
2067 static boolean
2068 mmo_new_section_hook (abfd, newsect)
2069      bfd *abfd ATTRIBUTE_UNUSED;
2070      asection *newsect;
2071 {
2072   /* We zero-fill all fields and assume NULL is represented by an all
2073      zero-bit pattern.  */
2074   newsect->used_by_bfd =
2075     (PTR) bfd_zalloc (abfd, sizeof (struct mmo_section_data_struct));
2076
2077   if (!newsect->used_by_bfd)
2078     return false;
2079
2080   /* Always align to at least 32-bit words.  */
2081   newsect->alignment_power = 2;
2082   return true;
2083 }
2084
2085 /* We already have section contents loaded for sections that have
2086    contents.  */
2087
2088 static boolean
2089 mmo_get_section_contents (abfd, sec, location, offset, bytes_to_do)
2090      bfd *abfd ATTRIBUTE_UNUSED;
2091      asection *sec ATTRIBUTE_UNUSED;
2092      PTR location ATTRIBUTE_UNUSED;
2093      file_ptr offset ATTRIBUTE_UNUSED;
2094      bfd_size_type bytes_to_do ATTRIBUTE_UNUSED;
2095 {
2096   /* Iterate over diminishing chunk sizes, copying contents, like
2097      mmo_set_section_contents.  */
2098   while (bytes_to_do)
2099     {
2100       /* A minor song-and-dance to make sure we're not bitten by the
2101          distant possibility of the cast from bfd_vma to int making the
2102          chunk zero-sized.  */
2103       int chunk_size
2104         = (int) bytes_to_do != 0 ? bytes_to_do : MMO_SEC_CONTENTS_CHUNK_SIZE;
2105       bfd_byte *loc;
2106
2107       do
2108         loc = mmo_get_loc (sec, sec->vma + offset, chunk_size);
2109       while (loc == NULL && (chunk_size /= 2) != 0);
2110
2111       if (chunk_size == 0)
2112         return false;
2113
2114       memcpy (location, loc, chunk_size);
2115
2116       location += chunk_size;
2117       bytes_to_do -= chunk_size;
2118       offset += chunk_size;
2119     }
2120   return true;
2121 }
2122
2123 /* Return the amount of memory needed to read the symbol table.  */
2124
2125 static long
2126 mmo_get_symtab_upper_bound (abfd)
2127      bfd *abfd ATTRIBUTE_UNUSED;
2128 {
2129   return (abfd->symcount + 1) * sizeof (asymbol *);
2130 }
2131
2132 /* Sort mmo symbols by serial number.  */
2133
2134 static int
2135 mmo_sort_mmo_symbols (arg1, arg2)
2136      const PTR arg1;
2137      const PTR arg2;
2138 {
2139   const struct mmo_symbol *sym1 = *(const struct mmo_symbol **) arg1;
2140   const struct mmo_symbol *sym2 = *(const struct mmo_symbol **) arg2;
2141
2142   /* Sort by serial number first.  */
2143   if (sym1->serno < sym2->serno)
2144     return -1;
2145   else if (sym1->serno > sym2->serno)
2146     return 1;
2147
2148   /* Then sort by address of the table entries.  */
2149   return ((const char *) arg1 - (const char *) arg2);
2150 }
2151
2152 /* Translate the symbol table.  */
2153
2154 static long
2155 mmo_get_symtab (abfd, alocation)
2156      bfd *abfd;
2157      asymbol **alocation;
2158 {
2159   unsigned int symcount = bfd_get_symcount (abfd);
2160   asymbol *csymbols;
2161   unsigned int i;
2162
2163   csymbols = abfd->tdata.mmo_data->csymbols;
2164   if (csymbols == NULL)
2165     {
2166       asymbol *c;
2167       struct mmo_symbol *s;
2168       struct mmo_symbol **msp;
2169
2170       /* First we store the symbols into the table we'll return, then we
2171          qsort it on the serial number, with secondary on the address of
2172          the symbol, to preserve order if there would be non-unique serial
2173          numbers.  */
2174       for (s = abfd->tdata.mmo_data->symbols,
2175              msp = (struct mmo_symbol **) alocation;
2176            s != NULL;
2177            s = s->next, ++msp)
2178         *msp = s;
2179
2180       *msp = NULL;
2181
2182       qsort (alocation, symcount, sizeof (struct mmo_symbol *),
2183              mmo_sort_mmo_symbols);
2184
2185       csymbols = (asymbol *) bfd_alloc (abfd, symcount * sizeof (asymbol));
2186       if (csymbols == NULL && symcount != 0)
2187         return false;
2188       abfd->tdata.mmo_data->csymbols = csymbols;
2189
2190       for (msp = (struct mmo_symbol **) alocation, c = csymbols;
2191            *msp != NULL;
2192            msp++, ++c)
2193         {
2194           s = *msp;
2195           c->the_bfd = abfd;
2196           c->name = s->name;
2197           c->value = s->value;
2198           c->flags = BSF_GLOBAL;
2199
2200           if (s->sym_type == mmo_data_sym)
2201             {
2202               c->section
2203                 = bfd_get_section_by_name (abfd, MMO_DATA_SECTION_NAME);
2204
2205               if (c->section == NULL)
2206                 c->section = bfd_abs_section_ptr;
2207               else
2208                 c->value -= c->section->vma;
2209             }
2210           else if (s->sym_type == mmo_undef_sym)
2211             c->section = bfd_und_section_ptr;
2212           else if (s->sym_type == mmo_reg_sym)
2213             {
2214               c->section
2215                 = bfd_make_section_old_way (abfd, MMIX_REG_SECTION_NAME);
2216             }
2217           else
2218             {
2219               asection *textsec
2220                 = bfd_get_section_by_name (abfd, MMO_TEXT_SECTION_NAME);
2221
2222               if (textsec != NULL
2223                   && c->value >= textsec->vma
2224                   && c->value <= textsec->vma + textsec->_cooked_size)
2225                 {
2226                   c->section = textsec;
2227                   c->value -= c->section->vma;
2228                 }
2229               else
2230                 c->section = bfd_abs_section_ptr;
2231             }
2232
2233           c->udata.p = NULL;
2234         }
2235     }
2236
2237   /* Last, overwrite the incoming table with the right-type entries.  */
2238   for (i = 0; i < symcount; i++)
2239     *alocation++ = csymbols++;
2240   *alocation = NULL;
2241
2242   return symcount;
2243 }
2244
2245 /* Get information about a symbol.  */
2246
2247 static void
2248 mmo_get_symbol_info (ignore_abfd, symbol, ret)
2249      bfd *ignore_abfd ATTRIBUTE_UNUSED;
2250      asymbol *symbol;
2251      symbol_info *ret;
2252 {
2253   bfd_symbol_info (symbol, ret);
2254 }
2255
2256 static void
2257 mmo_print_symbol (abfd, afile, symbol, how)
2258      bfd *abfd;
2259      PTR afile;
2260      asymbol *symbol;
2261      bfd_print_symbol_type how;
2262 {
2263   FILE *file = (FILE *) afile;
2264
2265   switch (how)
2266     {
2267     case bfd_print_symbol_name:
2268       fprintf (file, "%s", symbol->name);
2269       break;
2270     default:
2271       bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
2272
2273       fprintf (file, " %-5s %s",
2274                symbol->section->name,
2275                symbol->name);
2276     }
2277 }
2278
2279 /* We can't map a file directly into executable code, so the
2280    size of header information is irrelevant.  */
2281
2282 static int
2283 mmo_sizeof_headers (abfd, exec)
2284      bfd *abfd ATTRIBUTE_UNUSED;
2285      boolean exec ATTRIBUTE_UNUSED;
2286 {
2287   return 0;
2288 }
2289
2290 /* Write the (section-neutral) file preamble.  */
2291
2292 static boolean
2293 mmo_internal_write_header (abfd)
2294      bfd *abfd;
2295 {
2296   const char lop_pre_bfd[] = { LOP, LOP_PRE, 1, 1};
2297
2298   if (bfd_bwrite (lop_pre_bfd, 4, abfd) != 4)
2299     return false;
2300
2301   /* Copy creation time of original file.  */
2302   if (bfd_bwrite (abfd->tdata.mmo_data->created, 4, abfd) != 4)
2303     return false;
2304
2305   return true;
2306 }
2307
2308 /* Write the LOP_POST record, with global register initializations.
2309    Z is the Z field of the LOP_POST, corresponding to 255 - number of
2310    registers at DATA.  The Z = 255 field is filled in with the
2311    start-address.  */
2312
2313 static boolean
2314 mmo_internal_write_post (abfd, z, sec)
2315      bfd *abfd;
2316      int z;
2317      asection *sec;
2318 {
2319   int i;
2320   bfd_byte buf[8];
2321   mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_POST << 16) | z);
2322
2323   for (i = z; i < 255; i++)
2324     {
2325       bfd_byte *data = mmo_get_loc (sec, i * 8, 8);
2326
2327       if (bfd_bwrite (data, 8, abfd) != 8)
2328         return false;
2329     }
2330
2331   /* For Z == $255, we always emit the start location; supposedly Main,
2332      but we have it handy at bfd_get_start_address.  If we're called with
2333      Z == 255, don't assume DATA is valid.  */
2334   bfd_put_64 (abfd, bfd_get_start_address (abfd), buf);
2335
2336   return ! abfd->tdata.mmo_data->have_error && bfd_bwrite (buf, 8, abfd) == 8;
2337 }
2338
2339 /* Translate to and from BFD flags.  This is to make sure that we don't
2340    get bitten by BFD flag number changes.  */
2341
2342 static flagword
2343 mmo_sec_flags_from_bfd_flags (flags)
2344      flagword flags;
2345 {
2346   flagword oflags = 0;
2347
2348   if (flags & SEC_ALLOC)
2349     oflags |= MMO_SEC_ALLOC;
2350   if (flags & SEC_LOAD)
2351     oflags |= MMO_SEC_LOAD;
2352   if (flags & SEC_RELOC)
2353     oflags |= MMO_SEC_RELOC;
2354   if (flags & SEC_READONLY)
2355     oflags |= MMO_SEC_READONLY;
2356   if (flags & SEC_CODE)
2357     oflags |= MMO_SEC_CODE;
2358   if (flags & SEC_DATA)
2359     oflags |= MMO_SEC_DATA;
2360   if (flags & SEC_NEVER_LOAD)
2361     oflags |= MMO_SEC_NEVER_LOAD;
2362   if (flags & SEC_IS_COMMON)
2363     oflags |= MMO_SEC_IS_COMMON;
2364   if (flags & SEC_DEBUGGING)
2365     oflags |= MMO_SEC_DEBUGGING;
2366
2367   return oflags;
2368 }
2369
2370 static flagword
2371 bfd_sec_flags_from_mmo_flags (flags)
2372      flagword flags;
2373 {
2374   flagword oflags = 0;
2375
2376   if (flags & MMO_SEC_ALLOC)
2377     oflags |= SEC_ALLOC;
2378   if (flags & MMO_SEC_LOAD)
2379     oflags |= SEC_LOAD;
2380   if (flags & MMO_SEC_RELOC)
2381     oflags |= SEC_RELOC;
2382   if (flags & MMO_SEC_READONLY)
2383     oflags |= SEC_READONLY;
2384   if (flags & MMO_SEC_CODE)
2385     oflags |= SEC_CODE;
2386   if (flags & MMO_SEC_DATA)
2387     oflags |= SEC_DATA;
2388   if (flags & MMO_SEC_NEVER_LOAD)
2389     oflags |= SEC_NEVER_LOAD;
2390   if (flags & MMO_SEC_IS_COMMON)
2391     oflags |= SEC_IS_COMMON;
2392   if (flags & MMO_SEC_DEBUGGING)
2393     oflags |= SEC_DEBUGGING;
2394
2395   return oflags;
2396 }
2397
2398 /* Write a section.  */
2399
2400 static boolean
2401 mmo_internal_write_section (abfd, sec)
2402      bfd *abfd;
2403      asection *sec;
2404 {
2405   /* We do it differently depending on what section this is:
2406
2407    ".text": Output, prepended by information about the first source file
2408    (not yet implemented.)
2409
2410    ".data": Output.
2411
2412    (".MMIX.reg_contents": Not handled here.)
2413
2414    Anything else: Output inside a lop_spec 80, in the format described
2415    above.  */
2416
2417   if (strcmp (sec->name, MMO_TEXT_SECTION_NAME) == 0)
2418     /* FIXME: Output source file name and line number.  */
2419     return
2420       mmo_write_loc_chunk_list (abfd,
2421                                 ((struct mmo_section_data_struct *)
2422                                  (sec->used_by_bfd))->head);
2423   else if (strcmp (sec->name, MMO_DATA_SECTION_NAME) == 0)
2424     return
2425       mmo_write_loc_chunk_list (abfd,
2426                                 ((struct mmo_section_data_struct *)
2427                                  (sec->used_by_bfd))->head);
2428   else if (strcmp (sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
2429     /* Not handled here.  */
2430     {
2431       /* This would normally be an abort call since this can't happen, but
2432          we don't do that.  */
2433       bfd_set_error (bfd_error_bad_value);
2434       return false;
2435     }
2436   else if (strncmp (sec->name, MMIX_OTHER_SPEC_SECTION_PREFIX,
2437                     strlen (MMIX_OTHER_SPEC_SECTION_PREFIX)) == 0)
2438     {
2439       int n = atoi (sec->name + strlen (MMIX_OTHER_SPEC_SECTION_PREFIX));
2440       mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_SPEC << 16) | n);
2441       return (! abfd->tdata.mmo_data->have_error
2442               && mmo_write_chunk_list (abfd,
2443                                        ((struct mmo_section_data_struct *)
2444                                         (sec->used_by_bfd))->head));
2445     }
2446   /* Ignore sections that are just allocated or empty; we write out
2447      _contents_ here.  */
2448   else if ((bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS) != 0
2449            && sec->_raw_size != 0)
2450     {
2451       /* Keep the document-comment formatted the way it is.  */
2452 /*
2453 INODE
2454 mmo section mapping, , Symbol-table, mmo
2455 SUBSECTION
2456         mmo section mapping
2457
2458         The implementation in BFD uses special data type 80 (decimal) to
2459         encapsulate and describe named sections, containing e.g.@: debug
2460         information.  If needed, any datum in the encapsulation will be
2461         quoted using lop_quote.  First comes a 32-bit word holding the
2462         number of 32-bit words containing the zero-terminated zero-padded
2463         segment name.  After the name there's a 32-bit word holding flags
2464         describing the section type.  Then comes a 64-bit big-endian word
2465         with the section length (in bytes), then another with the section
2466         start address.  Depending on the type of section, the contents
2467         might follow, zero-padded to 32-bit boundary.  For a loadable
2468         section (such as data or code), the contents might follow at some
2469         later point, not necessarily immediately, as a lop_loc with the
2470         same start address as in the section description, followed by the
2471         contents.  This in effect forms a descriptor that must be emitted
2472         before the actual contents.  Sections described this way must not
2473         overlap.
2474
2475         For areas that don't have such descriptors, synthetic sections are
2476         formed by BFD.  Consecutive contents in the two memory areas
2477         @samp{0x0000@dots{}00} to @samp{0x01ff@dots{}ff} and
2478         @samp{0x2000@dots{}00} to @samp{0x20ff@dots{}ff} are entered in
2479         sections named <<.text>> and <<.data>> respectively.  If an area
2480         is not otherwise described, but would together with a neighboring
2481         lower area be less than @samp{0x40000000} bytes long, it is joined
2482         with the lower area and the gap is zero-filled.  For other cases,
2483         a new section is formed, named <<.MMIX.sec.@var{n}>>.  Here,
2484         @var{n} is a number, a running count through the mmo file,
2485         starting at 0.
2486
2487 EXAMPLE
2488         A loadable section specified as:
2489
2490 | .section secname,"ax"
2491 | TETRA 1,2,3,4,-1,-2009
2492 | BYTE 80
2493
2494         and linked to address @samp{0x4}, is represented by the sequence:
2495
2496 | 0x98080050 - lop_spec 80
2497 | 0x00000002 - two 32-bit words for the section name
2498 | 0x7365636e - "secn"
2499 | 0x616d6500 - "ame\0"
2500 | 0x00000033 - flags CODE, READONLY, LOAD, ALLOC
2501 | 0x00000000 - high 32 bits of section length
2502 | 0x0000001c - section length is 28 bytes; 6 * 4 + 1 + alignment to 32 bits
2503 | 0x00000000 - high 32 bits of section address
2504 | 0x00000004 - section address is 4
2505 | 0x98010002 - 64 bits with address of following data
2506 | 0x00000000 - high 32 bits of address
2507 | 0x00000004 - low 32 bits: data starts at address 4
2508 | 0x00000001 - 1
2509 | 0x00000002 - 2
2510 | 0x00000003 - 3
2511 | 0x00000004 - 4
2512 | 0xffffffff - -1
2513 | 0xfffff827 - -2009
2514 | 0x50000000 - 80 as a byte, padded with zeros.
2515
2516         Note that the lop_spec wrapping does not include the section
2517         contents.  Compare this to a non-loaded section specified as:
2518
2519 | .section thirdsec
2520 | TETRA 200001,100002
2521 | BYTE 38,40
2522
2523         This, when linked to address @samp{0x200000000000001c}, is
2524         represented by:
2525
2526 | 0x98080050 - lop_spec 80
2527 | 0x00000002 - two 32-bit words for the section name
2528 | 0x7365636e - "thir"
2529 | 0x616d6500 - "dsec"
2530 | 0x00000010 - flag READONLY
2531 | 0x00000000 - high 32 bits of section length
2532 | 0x0000000c - section length is 12 bytes; 2 * 4 + 2 + alignment to 32 bits
2533 | 0x20000000 - high 32 bits of address
2534 | 0x0000001c - low 32 bits of address 0x200000000000001c
2535 | 0x00030d41 - 200001
2536 | 0x000186a2 - 100002
2537 | 0x26280000 - 38, 40 as bytes, padded with zeros
2538
2539         For the latter example, the section contents must not be
2540         loaded in memory, and is therefore specified as part of the
2541         special data.  The address is usually unimportant but might
2542         provide information for e.g.@: the DWARF 2 debugging format.  */
2543
2544       mmo_write_tetra_raw (abfd, LOP_SPEC_SECTION);
2545       mmo_write_tetra (abfd, (strlen (sec->name) + 3) / 4);
2546       mmo_write_chunk (abfd, sec->name, strlen (sec->name));
2547       mmo_flush_chunk (abfd);
2548       /* FIXME: We can get debug sections (.debug_line & Co.) with a
2549          section flag still having SEC_RELOC set.  Investigate.  This
2550          might be true for all alien sections; perhaps mmo.em should clear
2551          that flag.  Might be related to weak references.  */
2552       mmo_write_tetra (abfd,
2553                        mmo_sec_flags_from_bfd_flags
2554                        (bfd_get_section_flags (abfd, sec)));
2555       mmo_write_octa (abfd, sec->_raw_size);
2556       mmo_write_octa (abfd, bfd_get_section_vma (abfd, sec));
2557
2558       /* Writing a LOP_LOC ends the LOP_SPEC data, and makes data actually
2559          loaded.  */
2560       if (bfd_get_section_flags (abfd, sec) & SEC_LOAD)
2561           return 
2562             ! abfd->tdata.mmo_data->have_error
2563             && mmo_write_loc_chunk_list (abfd,
2564                                          ((struct mmo_section_data_struct *)
2565                                           (sec->used_by_bfd))->head);
2566       return
2567         ! abfd->tdata.mmo_data->have_error
2568         && mmo_write_chunk_list (abfd,
2569                                  ((struct mmo_section_data_struct *)
2570                                   (sec->used_by_bfd))->head);
2571     }
2572   return true;
2573 }
2574
2575 /* We save up all data before output.  */
2576
2577 static boolean
2578 mmo_set_section_contents (abfd, sec, location, offset, bytes_to_do)
2579      bfd *abfd ATTRIBUTE_UNUSED;
2580      sec_ptr sec;
2581      PTR location;
2582      file_ptr offset;
2583      bfd_size_type bytes_to_do;
2584 {
2585   /* Iterate over diminishing chunk sizes, copying contents.  */
2586   while (bytes_to_do)
2587     {
2588       /* A minor song-and-dance to make sure we're not bitten by the
2589          distant possibility of the cast from bfd_vma to int making the
2590          chunk zero-sized.  */
2591       int chunk_size
2592         = (int) bytes_to_do != 0 ? bytes_to_do : MMO_SEC_CONTENTS_CHUNK_SIZE;
2593       bfd_byte *loc;
2594
2595       do
2596         loc = mmo_get_loc (sec, sec->vma + offset, chunk_size);
2597       while (loc == NULL && (chunk_size /= 2) != 0);
2598
2599       if (chunk_size == 0)
2600         return false;
2601
2602       memcpy (loc, location, chunk_size);
2603
2604       location += chunk_size;
2605       bytes_to_do -= chunk_size;
2606       offset += chunk_size;
2607     }
2608   return true;
2609 }
2610
2611 /* Add a symbol to a trie-tree.  */
2612
2613 static boolean
2614 mmo_internal_add_3_sym (abfd, rootp, symp)
2615      bfd *abfd;
2616      struct mmo_symbol_trie *rootp;
2617      const struct mmo_symbol *symp;
2618 {
2619   const char *name = symp->name;
2620   struct mmo_symbol_trie *trie = rootp;
2621   struct mmo_symbol_trie **triep = NULL;
2622
2623   while (*name && trie != NULL)
2624     {
2625       if (*name < trie->symchar)
2626         {
2627           triep = &trie->left;
2628           trie = trie->left;
2629         }
2630       else if (*name > trie->symchar)
2631         {
2632           triep = &trie->right;
2633           trie = trie->right;
2634         }
2635       else if (*name == trie->symchar)
2636         {
2637           triep = &trie->middle;
2638           name++;
2639
2640           /* Make sure "trie" points to where we should fill in the
2641              current symbol whenever we've iterated through "name".  We
2642              would lose the right position if we encounter "foobar" then
2643              "foo".  */
2644           if (*name)
2645             trie = trie->middle;
2646         }
2647     }
2648
2649   while (*name != 0)
2650     {
2651       /* Create middle branches for the rest of the characters.  */
2652       trie = bfd_zalloc (abfd, sizeof (struct mmo_symbol_trie));
2653       *triep = trie;
2654       trie->symchar = *name++;
2655       triep = &trie->middle;
2656     }
2657
2658   /* We discover a duplicate symbol rather late in the process, but still;
2659      we discover it and bail out.  */
2660   if (trie->sym.name != NULL)
2661     {
2662       (*_bfd_error_handler)
2663         (_("%s: invalid symbol table: duplicate symbol `%s'\n"),
2664          bfd_get_filename (abfd), trie->sym.name);
2665       bfd_set_error (bfd_error_bad_value);
2666       return false;
2667     }
2668
2669   memcpy (&trie->sym, symp, sizeof *symp);
2670   return true;
2671 }
2672
2673 /* Find out the length of the serialized version of a trie in bytes.  */
2674
2675 static unsigned int
2676 mmo_internal_3_length (abfd, trie)
2677      bfd *abfd;
2678      struct mmo_symbol_trie *trie;
2679 {
2680   /* First, one for the control byte.  */
2681   unsigned int length = 1;
2682
2683   if (trie == NULL)
2684     return 0;
2685
2686   /* Add in the recursion to the left.  */
2687   length += mmo_internal_3_length (abfd, trie->left);
2688
2689   /* Add in the middle trie and the character.  */
2690   length += 1 + mmo_internal_3_length (abfd, trie->middle);
2691
2692   /* Add in the recursion to the right.  */
2693   length += mmo_internal_3_length (abfd, trie->right);
2694
2695   /* Add in bytes for the symbol (if this is an endnode). */
2696   if (trie->sym.name != NULL)
2697     {
2698       unsigned int serno = trie->sym.serno;
2699
2700       /* First what it takes to encode the value. */
2701       if (trie->sym.sym_type == mmo_reg_sym)
2702         length++;
2703       else if (trie->sym.sym_type == mmo_undef_sym)
2704         length += 2;
2705       else
2706         {
2707           bfd_vma value = trie->sym.value;
2708
2709           /* Coded in one to eight following bytes.  */
2710           if (trie->sym.sym_type == mmo_data_sym)
2711             value -= (bfd_vma) 0x20 << 56;
2712
2713           do
2714             {
2715               value >>= 8;
2716               length++;
2717             }
2718           while (value != 0);
2719         }
2720
2721       /* Find out what it takes to encode the serial number.  */
2722       do
2723         {
2724           serno >>= 7;
2725           length++;
2726         }
2727       while (serno != 0);
2728     }
2729
2730   return length;
2731 }
2732
2733 /* Helper function for outputting the serial number of a symbol, output as
2734    a variant of leb128 (see dwarf2 documentation) which could be called
2735    beb128.  Using a helper function and recursion simplifies debugging.  */
2736
2737 static void
2738 mmo_beb128_out (abfd, serno, marker)
2739      bfd *abfd;
2740      int serno;
2741      int marker;
2742 {
2743   if (serno & ~0x7f)
2744     mmo_beb128_out (abfd, serno >> 7, 0);
2745   mmo_write_byte (abfd, marker | (serno & 0x7f));
2746 }
2747
2748 /* Serialize a trie.  */
2749
2750 static void
2751 mmo_internal_3_dump (abfd, trie)
2752      bfd *abfd;
2753      struct mmo_symbol_trie *trie;
2754 {
2755   bfd_byte control = 0;
2756
2757   if (trie == NULL)
2758     return;
2759
2760   if (trie->left)
2761     control |= MMO3_LEFT;
2762
2763   if (trie->middle)
2764     control |= MMO3_MIDDLE;
2765
2766   if (trie->right)
2767     control |= MMO3_RIGHT;
2768
2769   if (trie->sym.name != NULL)
2770     {
2771       /* Encode the symbol type and length of value bytes.  */
2772       if (trie->sym.sym_type == mmo_reg_sym)
2773         control |= MMO3_REGQUAL_BITS;
2774       else if (trie->sym.sym_type == mmo_undef_sym)
2775         control |= MMO3_UNDEF;
2776       else
2777         {
2778           bfd_vma value = trie->sym.value;
2779
2780           /* Coded in 1..8 following bytes.  */
2781           if (trie->sym.sym_type == mmo_data_sym)
2782             {
2783               control |= MMO3_DATA;
2784               value -= (bfd_vma) 0x20 << 56;
2785             }
2786
2787           do
2788             {
2789               value >>= 8;
2790               control++;
2791             }
2792           while (value != 0);
2793         }
2794     }
2795
2796   /* The control byte is output before recursing.  */
2797   mmo_write_byte (abfd, control);
2798
2799   mmo_internal_3_dump (abfd, trie->left);
2800
2801   if (control & MMO3_SYMBITS)
2802     {
2803       mmo_write_byte (abfd, trie->symchar);
2804
2805       if (trie->sym.name != NULL)
2806         {
2807           if (trie->sym.sym_type == mmo_reg_sym)
2808             mmo_write_byte (abfd, trie->sym.value);
2809           else if (trie->sym.sym_type == mmo_undef_sym)
2810             {
2811               mmo_write_byte (abfd, 0);
2812               mmo_write_byte (abfd, 0);
2813             }
2814           else
2815             {
2816               bfd_vma value = trie->sym.value;
2817
2818               bfd_byte byte_n = control & 15;
2819
2820               /* Coded in 1..8 following bytes.  Note that the value is
2821                  shifted out big-endian.  */
2822               if (trie->sym.sym_type == mmo_data_sym)
2823                 {
2824                   value -= (bfd_vma) 0x20 << 56;
2825                   byte_n -= 8;
2826                 }
2827
2828               do
2829                 {
2830                   mmo_write_byte (abfd, (value >> ((byte_n - 1) * 8)) & 0xff);
2831                   byte_n--;
2832                 }
2833               while (byte_n != 0);
2834             }
2835
2836           mmo_beb128_out (abfd, trie->sym.serno, 128);
2837         }
2838       mmo_internal_3_dump (abfd, trie->middle);
2839     }
2840   mmo_internal_3_dump (abfd, trie->right);
2841 }
2842
2843 /* Write symbols in mmo format.  Also write the lop_end terminator.  */
2844
2845 static boolean
2846 mmo_write_symbols_and_terminator (abfd)
2847      bfd *abfd;
2848 {
2849   int count = bfd_get_symcount (abfd);
2850   asymbol *maintable[2];
2851   asymbol **table;
2852   asymbol **orig_table = bfd_get_outsymbols (abfd);
2853   int serno;
2854   struct mmo_symbol_trie root;
2855   int trie_len;
2856   int i;
2857   bfd_byte buf[4];
2858
2859   /* Create a symbol for "Main".  */
2860   asymbol *fakemain = bfd_make_empty_symbol (abfd);
2861
2862   fakemain->flags = BSF_GLOBAL;
2863   fakemain->value = bfd_get_start_address (abfd);
2864   fakemain->name = MMIX_START_SYMBOL_NAME;
2865   fakemain->section = bfd_abs_section_ptr;
2866   maintable[0] = fakemain;
2867   maintable[1] = NULL;
2868
2869   memset (&root, 0, sizeof (root));
2870
2871   /* Make all symbols take a left turn.  */
2872   root.symchar = 0xff;
2873
2874   /* There must always be a ":Main", so we'll add one if there are no
2875      symbols.  Make sure we have room for it.  */
2876   table = bfd_alloc (abfd, (count + 1) * sizeof (asymbol *));
2877   if (table == NULL)
2878     return false;
2879
2880   memcpy (table, orig_table, count * sizeof (asymbol *));
2881
2882   /* Move :Main (if there is one) to the first position.  This is
2883      necessary to get the same layout of the trie-tree when linking as
2884      when objcopying the result as in the objcopy.exp test "simple objcopy
2885      of executable".  It also automatically takes care of assigning serial
2886      number 1 to :Main (as is mandatory).  */
2887   for (i = 0; i < count; i++)
2888     if (table[i] != NULL
2889         && strcmp (table[i]->name, MMIX_START_SYMBOL_NAME) == 0
2890         && (table[i]->flags & (BSF_DEBUGGING|BSF_GLOBAL)) == BSF_GLOBAL)
2891       {
2892         asymbol *mainsym = table[i];
2893         memcpy (table + 1, orig_table, i * sizeof (asymbol *));
2894         table[0] = mainsym;
2895
2896         /* Check that the value assigned to :Main is the same as the entry
2897            address.  The default linker script asserts this.  This is as
2898            good a place as any to check this consistency. */
2899         if ((mainsym->value
2900              + mainsym->section->output_section->vma
2901              + mainsym->section->output_offset)
2902             != bfd_get_start_address (abfd))
2903           {
2904             /* Arbitrary buffer to hold the printable representation of a
2905                vma.  */
2906             char vmas_main[40];
2907             char vmas_start[40];
2908             bfd_vma vma_start = bfd_get_start_address (abfd);
2909
2910             sprintf_vma (vmas_main, mainsym->value);
2911             sprintf_vma (vmas_start, vma_start);
2912
2913             (*_bfd_error_handler)
2914               (_("%s: Bad symbol definition: `Main' set to %s rather\
2915  than the start address %s\n"),
2916                bfd_get_filename (abfd), vmas_main, vmas_start);
2917             bfd_set_error (bfd_error_bad_value);
2918             return false;
2919           }
2920         break;
2921       }
2922   if (i == count && count != 0)
2923     {
2924       /* When there are symbols, there must be a :Main.  There was no
2925          :Main, so we need to add it manually.  */
2926       memcpy (table + 1, orig_table, count * sizeof (asymbol *));
2927       table[0] = fakemain;
2928       count++;
2929     }
2930
2931   for (i = 0, serno = 1; i < count && table[i] != NULL; i++)
2932     {
2933       asymbol *s = table[i];
2934
2935       /* It's not enough to consult bfd_is_local_label, since it does not
2936          mean "local" in the sense of linkable-and-observable-after-link.
2937          Let's just check the BSF_GLOBAL flag.
2938
2939          Also, don't export symbols with characters not in the allowed set.  */
2940       if ((s->flags & (BSF_DEBUGGING|BSF_GLOBAL)) == BSF_GLOBAL
2941           && strspn (s->name,
2942                      valid_mmo_symbol_character_set) == strlen (s->name))
2943         {
2944           struct mmo_symbol sym;
2945           memset (&sym, 0, sizeof (sym));
2946
2947           sym.name = s->name;
2948           sym.value =
2949             s->value
2950             + s->section->output_section->vma
2951             + s->section->output_offset;
2952
2953           if (bfd_is_und_section (s->section))
2954             sym.sym_type = mmo_undef_sym;
2955           else if (strcmp (s->section->name, MMO_DATA_SECTION_NAME) == 0
2956                    /* The encoding of data symbols require that the "rest"
2957                       of the value fits in 6 bytes, so the upper two bytes
2958                       must be 0x2000.  All other symbols get to be the
2959                       absolute type.  */
2960                    && (sym.value >> 48) == 0x2000)
2961             sym.sym_type = mmo_data_sym;
2962           else if (strcmp (s->section->name, MMIX_REG_SECTION_NAME) == 0)
2963             sym.sym_type = mmo_reg_sym;
2964           else if (strcmp (s->section->name,
2965                            MMIX_REG_CONTENTS_SECTION_NAME) == 0)
2966             {
2967               sym.sym_type = mmo_reg_sym;
2968               sym.value /= 8;
2969             }
2970           else
2971             sym.sym_type = mmo_abs_sym;
2972
2973           /* FIXME: We assume the order of the received symbols is an
2974              ordered mapping of the serial numbers.  This is not
2975              necessarily true if we e.g. objcopy a mmo file to another and
2976              there are gaps in the numbering.  Not sure if this can
2977              happen.  Not sure what to do.  */
2978           sym.serno = serno++;
2979
2980           if (! mmo_internal_add_3_sym (abfd, &root, &sym))
2981             return false;
2982         }
2983     }
2984
2985   /* Change the root node to be a ":"-prefix.  */
2986   root.symchar = ':';
2987   root.middle = root.left;
2988   root.right = NULL;
2989   root.left = NULL;
2990
2991   /* We have to find out if we can fit the whole symbol table in the mmo
2992      symtab.  It would be bad to assume we can always fit it in 262144
2993      bytes.  If we can't, just leave the Main symbol.  */
2994   trie_len = (mmo_internal_3_length (abfd, &root) + 3)/4;
2995
2996   if (trie_len > 0xffff)
2997     {
2998       /* Test this code by using a lower limit in the test above and check
2999          that the single "Main" symbol is emitted and handled properly.
3000          There's no specific test-case.  */
3001       struct mmo_symbol sym;
3002
3003       (*_bfd_error_handler)
3004         (_("%s: warning: symbol table too large for mmo, larger than 65535\
3005  32-bit words: %d.  Only `Main' will be emitted.\n"),
3006          bfd_get_filename (abfd), trie_len);
3007
3008       memset (&sym, 0, sizeof (sym));
3009       sym.sym_type = mmo_abs_sym;
3010       sym.name = MMIX_START_SYMBOL_NAME;
3011       sym.serno = 1;
3012       sym.value = bfd_get_start_address (abfd);
3013
3014       /* Then patch up a symbol table to be just the ":Main" symbol.  */
3015       memset (&root, 0, sizeof (root));
3016       root.left = root.middle;
3017       root.symchar = 0xff;
3018       root.middle = NULL;
3019       root.right = NULL;
3020
3021       if (! mmo_internal_add_3_sym (abfd, &root, &sym))
3022         return false;
3023
3024       root.symchar = ':';
3025       root.middle = root.left;
3026       root.right = NULL;
3027       root.left = NULL;
3028
3029       trie_len = (mmo_internal_3_length (abfd, &root) + 3)/4;
3030     }
3031
3032   /* Reset the written-bytes counter.  */
3033   abfd->tdata.mmo_data->byte_no = 0;
3034
3035   /* Put out the lop_stab mark.  */
3036   bfd_put_32 (abfd, (LOP << 24) | (LOP_STAB << 16), buf);
3037   if (bfd_bwrite (buf, 4, abfd) != 4)
3038     return false;
3039
3040   /* Dump out symbols.  */
3041   mmo_internal_3_dump (abfd, &root);
3042
3043   if (trie_len != (abfd->tdata.mmo_data->byte_no + 3)/4)
3044     {
3045       /* I haven't seen this trig.  It seems no use claiming this case
3046          isn't debugged and abort if we get here.  Instead emit a
3047          diagnostic and fail "normally".  */
3048       (*_bfd_error_handler)
3049         (_("%s: internal error, symbol table changed size from %d to %d\
3050  words\n"),
3051          bfd_get_filename (abfd), trie_len,
3052          (abfd->tdata.mmo_data->byte_no + 3)/4);
3053       bfd_set_error (bfd_error_bad_value);
3054       return false;
3055     }
3056
3057   /* Dump out remaining bytes in the buffer and handle I/O errors by
3058      propagating errors.  */
3059   if ((abfd->tdata.mmo_data->byte_no % 4) != 0
3060       || abfd->tdata.mmo_data->have_error)
3061     {
3062       memset (abfd->tdata.mmo_data->buf + (abfd->tdata.mmo_data->byte_no % 4),
3063               0, 4 - (abfd->tdata.mmo_data->byte_no % 4));
3064
3065       if (abfd->tdata.mmo_data->have_error
3066           || bfd_bwrite (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
3067         return false;
3068     }
3069
3070   bfd_put_32 (abfd, (LOP << 24) | (LOP_END << 16) | trie_len, buf);
3071   return bfd_bwrite (buf, 4, abfd) == 4;
3072 }
3073
3074 /* Write section unless it is the register contents section.  For that, we
3075    instead store the section in the supplied pointer.  This function is
3076    used through bfd_map_over_sections.  */
3077
3078 static void
3079 mmo_write_section_unless_reg_contents (abfd, sec, p)
3080      bfd *abfd;
3081      asection *sec;
3082      PTR p;
3083 {
3084   struct mmo_write_sec_info *infop = (struct mmo_write_sec_info *) p;
3085
3086   if (! infop->retval)
3087     return;
3088
3089   if (strcmp (sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
3090     {
3091       infop->reg_section = sec;
3092       return;
3093     }
3094
3095   /* Exclude the convenience register section.  */
3096   if (strcmp (sec->name, MMIX_REG_SECTION_NAME) == 0)
3097     {
3098       if (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
3099         {
3100           /* Make sure it hasn't got contents.  It seems impossible to
3101              make it carry contents, so we don't have a test-case for
3102              this.  */
3103           (*_bfd_error_handler)
3104             (_("%s: internal error, internal register section %s had\
3105  contents\n"),
3106              bfd_get_filename (abfd), sec->name);
3107           bfd_set_error (bfd_error_bad_value);
3108           infop->retval = false;
3109           return;
3110         }
3111
3112       return;
3113     }
3114
3115   infop->retval = mmo_internal_write_section (abfd, sec);
3116 }
3117
3118 /* Do the actual output of a file.  Assumes mmo_set_section_contents is
3119    already called. */
3120
3121 static boolean
3122 mmo_write_object_contents (abfd)
3123      bfd *abfd;
3124 {
3125   struct mmo_write_sec_info wsecinfo;
3126
3127   /* First, there are a few words of preamble.  */
3128   if (! mmo_internal_write_header (abfd))
3129     return false;
3130
3131   wsecinfo.reg_section = NULL;
3132   wsecinfo.retval = true;
3133
3134   bfd_map_over_sections (abfd, mmo_write_section_unless_reg_contents,
3135                          (PTR) &wsecinfo);
3136
3137   if (! wsecinfo.retval)
3138     return false;
3139
3140   if (wsecinfo.reg_section != NULL)
3141     {
3142       asection *sec = wsecinfo.reg_section;
3143       unsigned int z = (unsigned int) (sec->vma / 8);
3144
3145       /* Registers 0..31 must not be global.  Do sanity check on the "vma"
3146          of the register contents section and check that it corresponds to
3147          the length of the section.  */
3148       if (z < 32 || z >= 255 || (sec->vma & 7) != 0
3149           || sec->vma != 256 * 8 - sec->_raw_size - 8)
3150         {
3151           bfd_set_error (bfd_error_bad_value);
3152
3153           if (sec->_raw_size == 0)
3154             /* There must always be at least one such register.  */
3155             (*_bfd_error_handler)
3156               (_("%s: no initialized registers; section length 0\n"),
3157                bfd_get_filename (abfd));
3158           else if (sec->vma > (256 - 32) * 8)
3159             /* Provide better error message for the case of too many
3160                global registers.  */
3161             (*_bfd_error_handler)
3162               (_("%s: too many initialized registers; section length %ld\n"),
3163                bfd_get_filename (abfd),
3164                (long) sec->_raw_size);
3165           else
3166             (*_bfd_error_handler)
3167               (_("%s: invalid start address for initialized registers of\
3168  length %ld: 0x%lx%08lx\n"),
3169                bfd_get_filename (abfd),
3170                (long) sec->_raw_size,
3171                (unsigned long) (sec->vma >> 32), (unsigned long) (sec->vma));
3172
3173           return false;
3174         }
3175
3176       if (! mmo_internal_write_post (abfd, z, sec))
3177         return false;
3178     }
3179   else
3180     if (! mmo_internal_write_post (abfd, 255, NULL))
3181       return false;
3182
3183   return mmo_write_symbols_and_terminator (abfd);
3184 }
3185
3186 /* Return the size of a NULL pointer, so we support linking in an mmo
3187    object.  */
3188
3189 static long
3190 mmo_get_reloc_upper_bound (abfd, sec)
3191      bfd *abfd ATTRIBUTE_UNUSED;
3192      asection *sec ATTRIBUTE_UNUSED;
3193 {
3194   return sizeof (PTR);
3195 }
3196
3197 /* Similarly canonicalize relocs to empty, filling in the terminating NULL
3198    pointer.  */
3199
3200 long
3201 mmo_canonicalize_reloc (abfd, section, relptr, symbols)
3202      bfd *abfd ATTRIBUTE_UNUSED;
3203      sec_ptr section ATTRIBUTE_UNUSED;
3204      arelent **relptr;
3205      asymbol **symbols ATTRIBUTE_UNUSED;
3206 {
3207   *relptr = NULL;
3208   return 0;
3209 }
3210
3211 /* If there's anything in particular in a mmo bfd that we want to free,
3212    make this a real function.  Only do this if you see major memory
3213    thrashing; zealous free:ing will cause unwanted behavior, especially if
3214    you "free" memory allocated with "bfd_alloc", or even "bfd_release" a
3215    block allocated with "bfd_alloc"; they're really allocated from an
3216    obstack, and we don't know what was allocated there since this
3217    particular allocation.  */
3218
3219 #define mmo_close_and_cleanup _bfd_generic_close_and_cleanup
3220 #define mmo_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
3221
3222 /* Perhaps we need to adjust this one; mmo labels (originally) without a
3223    leading ':' might more appropriately be called local.  */
3224 #define mmo_bfd_is_local_label_name bfd_generic_is_local_label_name
3225
3226 /* Is this one really used or defined by anyone?  */
3227 #define mmo_get_lineno _bfd_nosymbols_get_lineno
3228
3229 /* FIXME: We can do better on this one, if we have a dwarf2 .debug_line
3230    section or if MMO line numbers are implemented.  */
3231 #define mmo_find_nearest_line _bfd_nosymbols_find_nearest_line
3232 #define mmo_make_empty_symbol _bfd_generic_make_empty_symbol
3233 #define mmo_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
3234 #define mmo_read_minisymbols _bfd_generic_read_minisymbols
3235 #define mmo_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
3236
3237 #define mmo_get_section_contents_in_window \
3238   _bfd_generic_get_section_contents_in_window
3239 #define mmo_bfd_get_relocated_section_contents \
3240   bfd_generic_get_relocated_section_contents
3241 #define mmo_bfd_gc_sections bfd_generic_gc_sections
3242 #define mmo_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
3243 #define mmo_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
3244 #define mmo_bfd_link_add_symbols _bfd_generic_link_add_symbols
3245 #define mmo_bfd_link_just_syms _bfd_generic_link_just_syms
3246 #define mmo_bfd_final_link _bfd_generic_final_link
3247 #define mmo_bfd_link_split_section _bfd_generic_link_split_section
3248
3249 /* Strictly speaking, only MMIX uses this restricted format, but let's not
3250    stop anybody from shooting themselves in the foot.  */
3251 #define mmo_set_arch_mach bfd_default_set_arch_mach
3252 #define mmo_bfd_relax_section bfd_generic_relax_section
3253 #define mmo_bfd_merge_sections bfd_generic_merge_sections
3254 #define mmo_bfd_discard_group bfd_generic_discard_group
3255
3256 /* objcopy will be upset if we return -1 from bfd_get_reloc_upper_bound by
3257    using BFD_JUMP_TABLE_RELOCS (_bfd_norelocs) rather than 0.  FIXME: Most
3258    likely a bug in the _bfd_norelocs definition.
3259
3260    On the other hand, we smuggle in an mmo object (because setting up ELF
3261    is too cumbersome) when linking (from other formats, presumably ELF) to
3262    represent the g255 entry.  We need to link that object, so need to say
3263    it has no relocs.  Upper bound for the size of the relocation table is
3264    the size of a NULL pointer, and we support "canonicalization" for that
3265    pointer.  */
3266 #define mmo_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
3267
3268 /* We want to copy time of creation, otherwise we'd use
3269    BFD_JUMP_TABLE_COPY (_bfd_generic).  */
3270 #define mmo_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
3271 #define mmo_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
3272 #define mmo_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
3273 #define mmo_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
3274 #define mmo_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
3275
3276 const bfd_target bfd_mmo_vec =
3277 {
3278   "mmo",                        /* name */
3279   bfd_target_mmo_flavour,
3280   BFD_ENDIAN_BIG,               /* target byte order */
3281   BFD_ENDIAN_BIG,               /* target headers byte order */
3282
3283   /* FIXME: Might need adjustments.  */
3284   (HAS_RELOC | EXEC_P |         /* object flags */
3285    HAS_LINENO | HAS_DEBUG |
3286    HAS_SYMS | HAS_LOCALS | WP_TEXT),
3287
3288   /* FIXME: Might need adjustments.  */
3289   (SEC_CODE | SEC_DATA | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
3290    | SEC_READONLY | SEC_EXCLUDE | SEC_DEBUGGING | SEC_IN_MEMORY),
3291                                 /* section flags */
3292   0,                            /* leading underscore */
3293   ' ',                          /* ar_pad_char */
3294   16,                           /* ar_max_namelen */
3295   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3296   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3297   bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* data */
3298   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3299   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3300   bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* hdrs */
3301
3302   {
3303     _bfd_dummy_target,
3304     mmo_object_p,               /* bfd_check_format */
3305     _bfd_dummy_target,
3306     _bfd_dummy_target,
3307   },
3308   {
3309     bfd_false,
3310     mmo_mkobject,
3311     bfd_false,
3312     bfd_false,
3313   },
3314   {                             /* bfd_write_contents */
3315     bfd_false,
3316     mmo_write_object_contents,
3317     bfd_false,
3318     bfd_false,
3319   },
3320
3321   BFD_JUMP_TABLE_GENERIC (mmo),
3322   BFD_JUMP_TABLE_COPY (mmo),
3323   BFD_JUMP_TABLE_CORE (_bfd_nocore),
3324   BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
3325   BFD_JUMP_TABLE_SYMBOLS (mmo),
3326   /* We have to provide a valid method for getting relocs, returning zero,
3327      so we can't say BFD_JUMP_TABLE_RELOCS (_bfd_norelocs).  */
3328   BFD_JUMP_TABLE_RELOCS (mmo),
3329   BFD_JUMP_TABLE_WRITE (mmo),
3330   BFD_JUMP_TABLE_LINK (mmo),
3331   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
3332
3333   NULL,
3334
3335   NULL
3336 };