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