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