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