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