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