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