oops - update date on newly created entry
[binutils.git] / bfd / mmo.c
blob31ba035645eab8391b9f0a701c285ddc17cc3a67
1 /* BFD back-end for mmo objects (MMIX-specific object-format).
2 Copyright 2001, 2002
3 Free Software Foundation, Inc.
4 Written by Hans-Peter Nilsson (hp@bitrange.com).
5 Infrastructure and other bits originally copied from srec.c and
6 binary.c.
8 This file is part of BFD, the Binary File Descriptor library.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 SECTION
26 mmo backend
28 The mmo object format is used exclusively together with Professor
29 Donald E.@: Knuth's educational 64-bit processor MMIX. The simulator
30 @command{mmix} which is available at
31 @url{http://www-cs-faculty.stanford.edu/~knuth/programs/mmix.tar.gz}
32 understands this format. That package also includes a combined
33 assembler and linker called @command{mmixal}. The mmo format has
34 no advantages feature-wise compared to e.g. ELF. It is a simple
35 non-relocatable object format with no support for archives or
36 debugging information, except for symbol value information and
37 line numbers (which is not yet implemented in BFD). See
38 @url{http://www-cs-faculty.stanford.edu/~knuth/mmix.html} for more
39 information about MMIX. The ELF format is used for intermediate
40 object files in the BFD implementation.
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
51 INODE
52 File layout, Symbol-table, mmo, mmo
53 SUBSECTION
54 File layout
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}.
64 Contents is entered as 32-bit words, xor:ed over previous
65 contents, always zero-initialized. A word that starts with the
66 byte @samp{0x98} forms a command called a @samp{lopcode}, where
67 the next byte distinguished between the thirteen lopcodes. The
68 two remaining bytes, called the @samp{Y} and @samp{Z} fields, or
69 the @samp{YZ} field (a 16-bit big-endian number), are used for
70 various purposes different for each lopcode. As documented in
71 @url{http://www-cs-faculty.stanford.edu/~knuth/mmixal-intro.ps.gz},
72 the lopcodes are:
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}.
80 @table @code
81 @item lop_quote
82 0x98000001. The next word is contents, regardless of whether it
83 starts with 0x98 or not.
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.
92 @item lop_skip
93 0x9802YYZZ. Increase the current location by @samp{YZ} bytes.
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}.
101 @item lop_fixr
102 0x9804YYZZ. @samp{YZ} is stored into the current location plus
103 @math{2 - 4 * YZ}.
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})}.
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.
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.
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}.
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.
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}.
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}.
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}).
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
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>>.
169 EXAMPLE
170 This trivial one-label, one-instruction file:
172 | :Main TRAP 1,2,3
174 can be represented this way in mmo:
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. */
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"
205 #define LOP 0x98
206 #define LOP_QUOTE 0
207 #define LOP_LOC 1
208 #define LOP_SKIP 2
209 #define LOP_FIXO 3
210 #define LOP_FIXR 4
211 #define LOP_FIXRX 5
212 #define LOP_FILE 6
213 #define LOP_LINE 7
214 #define LOP_SPEC 8
215 #define LOP_PRE 9
216 #define LOP_POST 10
217 #define LOP_STAB 11
218 #define LOP_END 12
220 #define LOP_QUOTE_NEXT ((LOP << 24) | (LOP_QUOTE << 16) | 1)
221 #define SPEC_DATA_SECTION 80
222 #define LOP_SPEC_SECTION \
223 ((LOP << 24) | (LOP_SPEC << 16) | SPEC_DATA_SECTION)
225 /* Must be a power of two. If you change this to be >= 64k, you need a
226 new test-case; the ld test b-loc64k.d touches chunk-size problem areas. */
227 #define MMO_SEC_CONTENTS_CHUNK_SIZE (1 << 15)
229 /* An arbitrary number for the maximum length section name size. */
230 #define MAX_SECTION_NAME_SIZE (1024 * 1024)
232 /* A quite arbitrary number for the maximum length section size. */
233 #define MAX_ARTIFICIAL_SECTION_SIZE (1024 * 1024 * 1024)
235 #define MMO3_WCHAR 0x80
236 #define MMO3_LEFT 0x40
237 #define MMO3_MIDDLE 0x20
238 #define MMO3_RIGHT 0x10
239 #define MMO3_TYPEBITS 0xf
240 #define MMO3_REGQUAL_BITS 0xf
241 #define MMO3_UNDEF 2
242 #define MMO3_DATA 8
243 #define MMO3_SYMBITS 0x2f
245 /* Put these everywhere in new code. */
246 #define FATAL_DEBUG \
247 _bfd_abort (__FILE__, __LINE__, \
248 "Internal: Non-debugged code (test-case missing)")
250 #define BAD_CASE(x) \
251 _bfd_abort (__FILE__, __LINE__, \
252 "bad case for " #x)
254 enum mmo_sym_type { mmo_reg_sym, mmo_undef_sym, mmo_data_sym, mmo_abs_sym};
256 /* When scanning the mmo file, a linked list of mmo_symbol
257 structures is built to represent the symbol table (if there is
258 one). */
260 struct mmo_symbol
262 struct mmo_symbol *next;
263 CONST char *name;
264 bfd_vma value;
265 enum mmo_sym_type sym_type;
266 unsigned int serno;
269 struct mmo_data_list_struct
271 struct mmo_data_list_struct *next;
272 bfd_vma where;
273 bfd_size_type size;
274 bfd_size_type allocated_size;
275 bfd_byte data[1];
278 typedef struct mmo_data_list_struct mmo_data_list_type;
280 struct mmo_symbol_trie
282 struct mmo_symbol_trie *left;
283 struct mmo_symbol_trie *right;
284 struct mmo_symbol_trie *middle;
286 bfd_byte symchar;
288 /* A zero name means there's nothing here. */
289 struct mmo_symbol sym;
292 /* The mmo tdata information. */
294 struct mmo_data_struct
296 struct mmo_symbol *symbols;
297 struct mmo_symbol *symtail;
298 asymbol *csymbols;
300 /* File representation of time (NULL) when this file was created. */
301 bfd_byte created[4];
303 /* When we're reading bytes recursively, check this occasionally.
304 Also holds write errors. */
305 boolean have_error;
307 /* Max symbol length that may appear in the lop_stab table. Note that
308 this table might just hold a subset of symbols for not-really large
309 programs, as it can only be 65536 * 4 bytes large. */
310 int max_symbol_length;
312 /* Here's the symbol we build in lop_stab. */
313 char *lop_stab_symbol;
315 /* Index into lop_stab_symbol for the next character when parsing the
316 symbol information. */
317 int symbol_position;
319 /* When creating arbitrary sections, we need to count section numbers. */
320 int sec_no;
322 /* When writing or reading byte-wise, we need to count the bytes
323 within a 32-bit word. */
324 int byte_no;
326 /* We also need a buffer to hold the bytes we count reading or writing. */
327 bfd_byte buf[4];
330 typedef struct mmo_data_struct tdata_type;
332 struct mmo_section_data_struct
334 mmo_data_list_type *head;
335 mmo_data_list_type *tail;
338 /* These structures are used in bfd_map_over_sections constructs. */
340 /* Used when writing out sections; all but the register contents section
341 which is stored in reg_section. */
342 struct mmo_write_sec_info
344 asection *reg_section;
345 boolean retval;
348 /* Used when trying to find a section corresponding to addr. */
349 struct mmo_find_sec_info
351 asection *sec;
352 bfd_vma addr;
355 static boolean mmo_bfd_copy_private_bfd_data PARAMS ((bfd *, bfd *));
356 static void mmo_write_section_unless_reg_contents
357 PARAMS ((bfd *, asection *, PTR));
358 static void mmo_find_sec_w_addr PARAMS ((bfd *, asection *, PTR));
359 static void mmo_find_sec_w_addr_grow PARAMS ((bfd *, asection *, PTR));
360 static asection *mmo_make_section PARAMS ((bfd *, CONST char *));
361 static void mmo_get_symbol_info PARAMS ((bfd *, asymbol *, symbol_info *));
362 static void mmo_print_symbol
363 PARAMS ((bfd *, PTR, asymbol *, bfd_print_symbol_type));
364 static void mmo_init PARAMS ((void));
365 static boolean mmo_mkobject PARAMS ((bfd *));
366 static boolean mmo_scan PARAMS ((bfd *));
367 static asection *mmo_decide_section PARAMS ((bfd *, bfd_vma));
368 static asection *mmo_get_generic_spec_data_section PARAMS ((bfd *, int));
369 static asection *mmo_get_spec_section PARAMS ((bfd *, int));
370 static INLINE bfd_byte *mmo_get_loc PARAMS ((asection *, bfd_vma, int));
371 static void mmo_xore_64 PARAMS ((asection *, bfd_vma vma, bfd_vma value));
372 static void mmo_xore_32 PARAMS ((asection *, bfd_vma vma, unsigned int));
373 static void mmo_xore_16 PARAMS ((asection *, bfd_vma vma, unsigned int));
374 static CONST bfd_target *mmo_object_p PARAMS ((bfd *));
375 static void mmo_map_set_sizes PARAMS ((bfd *, asection *, PTR));
376 static boolean mmo_get_symbols PARAMS ((bfd *));
377 static boolean mmo_create_symbol PARAMS ((bfd *, CONST char *, bfd_vma,
378 enum mmo_sym_type, unsigned int));
379 static boolean mmo_get_section_contents
380 PARAMS ((bfd *, asection *, PTR, file_ptr, bfd_size_type));
381 static long mmo_get_symtab_upper_bound PARAMS ((bfd *));
382 static long mmo_get_symtab PARAMS ((bfd *, asymbol **));
383 static void mmo_get_symbol_info PARAMS ((bfd *, asymbol *, symbol_info *));
384 static void mmo_print_symbol PARAMS ((bfd *, PTR, asymbol *,
385 bfd_print_symbol_type));
386 static boolean mmo_set_section_contents
387 PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type));
388 static int mmo_sizeof_headers PARAMS ((bfd *, boolean));
389 static long mmo_get_reloc_upper_bound PARAMS ((bfd *, asection *));
391 static boolean mmo_internal_write_header PARAMS ((bfd *));
392 static boolean mmo_internal_write_post PARAMS ((bfd *, int, asection *));
393 static boolean mmo_internal_add_3_sym
394 PARAMS ((bfd *, struct mmo_symbol_trie *, CONST struct mmo_symbol *));
395 static unsigned int mmo_internal_3_length
396 PARAMS ((bfd *, struct mmo_symbol_trie *));
397 static void mmo_internal_3_dump
398 PARAMS ((bfd *, struct mmo_symbol_trie *));
399 static void mmo_beb128_out PARAMS ((bfd *, int, int));
400 static boolean mmo_internal_write_section
401 PARAMS ((bfd *, asection *));
402 static void mmo_write_tetra PARAMS ((bfd *, unsigned int));
403 static void mmo_write_tetra_raw PARAMS ((bfd *, unsigned int));
404 static void mmo_write_octa PARAMS ((bfd *, bfd_vma));
405 static void mmo_write_octa_raw PARAMS ((bfd *, bfd_vma));
406 static boolean mmo_write_chunk
407 PARAMS ((bfd *, CONST bfd_byte *, unsigned int));
408 static boolean mmo_flush_chunk PARAMS ((bfd *));
409 static boolean mmo_write_loc_chunk
410 PARAMS ((bfd *, bfd_vma, CONST bfd_byte *, unsigned int, bfd_vma *));
411 static boolean mmo_write_chunk_list PARAMS ((bfd *, mmo_data_list_type *));
412 static boolean mmo_write_loc_chunk_list
413 PARAMS ((bfd *, mmo_data_list_type *));
414 static boolean mmo_write_symbols_and_terminator PARAMS ((bfd *));
415 static flagword mmo_sec_flags_from_bfd_flags PARAMS ((flagword));
416 static flagword bfd_sec_flags_from_mmo_flags PARAMS ((flagword));
417 static bfd_byte mmo_get_byte PARAMS ((bfd *));
418 static void mmo_write_byte PARAMS ((bfd *, bfd_byte));
419 static boolean mmo_new_section_hook PARAMS ((bfd *, asection *));
420 static int mmo_sort_mmo_symbols PARAMS ((CONST PTR, CONST PTR));
421 static boolean mmo_write_object_contents PARAMS ((bfd *));
422 static long mmo_canonicalize_reloc
423 PARAMS ((bfd *, sec_ptr, arelent **, asymbol **));
425 /* Global "const" variables initialized once. Must not depend on
426 particular input or caller; put such things into the bfd or elsewhere.
427 Look ma, no static per-invocation data! */
429 static unsigned
430 char valid_mmo_symbol_character_set[/* A-Z a-z (we assume consecutive
431 codes; sorry EBCDIC:ers!). */
432 + 'Z' - 'A' + 1 + 'z' - 'a' + 1
433 /* Digits. */
434 + 10
435 /* ':' and '_'. */
436 + 1 + 1
437 /* Codes higher than 126. */
438 + 256 - 126
439 /* Ending zero. */
440 + 1];
443 /* Get section SECNAME or create one if it doesn't exist. When creating
444 one, new memory for the name is allocated. */
446 static asection *
447 mmo_make_section (abfd, secname)
448 bfd *abfd;
449 CONST char *secname;
451 asection *sec = bfd_get_section_by_name (abfd, secname);
453 if (sec == NULL)
455 char *newsecname = strdup (secname);
457 if (newsecname == NULL)
459 (*_bfd_error_handler)
460 (_("%s: No core to allocate section name %s\n"),
461 bfd_get_filename (abfd), secname);
462 bfd_set_error (bfd_error_system_call);
463 return NULL;
465 sec = bfd_make_section (abfd, newsecname);
468 return sec;
471 /* Nothing to do, but keep as a placeholder if we need it.
472 Note that state that might differ between bfd:s must not be initialized
473 here, nor must it be static. Add it to tdata information instead. */
475 static void
476 mmo_init ()
478 static boolean inited = false;
479 int i = 0;
480 int j = 0;
481 static const char letters[]
482 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:_";
484 if (inited == true)
485 return;
486 inited = true;
488 /* Fill in the set of valid symbol characters. */
489 strcpy (valid_mmo_symbol_character_set, letters);
490 i = strlen (letters);
492 for (j = 126; j < 256; j++)
493 valid_mmo_symbol_character_set[i++] = j;
496 /* Check whether an existing file is an mmo file. */
498 static CONST bfd_target *
499 mmo_object_p (abfd)
500 bfd *abfd;
502 struct stat statbuf;
503 bfd_byte b[4];
505 mmo_init ();
507 if (bfd_stat (abfd, &statbuf) < 0
508 || bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
509 || bfd_bread (b, 4, abfd) != 4)
510 goto bad_final;
512 /* All mmo files are a multiple of four bytes long.
513 Only recognize version one. */
514 if ((statbuf.st_size % 4) != 0
515 || b[0] != LOP || b[1] != LOP_PRE || b[2] != 1)
516 goto bad_format;
518 /* Get the last 32-bit word. */
519 if (bfd_seek (abfd, (file_ptr) statbuf.st_size - 4, SEEK_SET) != 0
520 || bfd_bread (b, 4, abfd) != 4)
521 goto bad_final;
523 /* Check if the file ends in a lop_end lopcode. */
524 if (b[0] != LOP || b[1] != LOP_END || ! mmo_mkobject (abfd))
525 goto bad_format;
527 /* Compute an upper bound on the max symbol length. Not really
528 important as all of the symbol information can only be 256k. */
529 abfd->tdata.mmo_data->max_symbol_length = (b[2] * 256 + b[3]) * 4;
530 abfd->tdata.mmo_data->lop_stab_symbol
531 = bfd_malloc (abfd->tdata.mmo_data->max_symbol_length + 1);
533 if (abfd->tdata.mmo_data->lop_stab_symbol == NULL)
535 (*_bfd_error_handler)
536 (_("%s: No core to allocate a symbol %d bytes long\n"),
537 bfd_get_filename (abfd), abfd->tdata.mmo_data->max_symbol_length);
538 goto bad_final;
541 /* Read in everything. */
542 if (! mmo_scan (abfd))
543 goto bad_format_free;
545 if (abfd->symcount > 0)
546 abfd->flags |= HAS_SYMS;
548 /* You'll have to tweak this if you want to use this format for other
549 arches (not recommended due to its small-size limitations). Look at
550 the ELF format for how to make it target-generic. */
551 if (! bfd_default_set_arch_mach (abfd, bfd_arch_mmix, 0))
552 goto bad_format_free;
554 return abfd->xvec;
556 bad_format_free:
557 free (abfd->tdata.mmo_data->lop_stab_symbol);
558 bad_format:
559 bfd_set_error (bfd_error_wrong_format);
560 bad_final:
561 return NULL;
564 /* Set up the mmo tdata information. */
566 static boolean
567 mmo_mkobject (abfd)
568 bfd *abfd;
570 mmo_init ();
572 if (abfd->tdata.mmo_data == NULL)
574 time_t created;
576 /* All fields are zero-initialized, so we don't have to explicitly
577 initialize most. */
578 tdata_type *tdata = (tdata_type *) bfd_zmalloc (sizeof (tdata_type));
579 if (tdata == NULL)
580 return false;
582 created = time (NULL);
583 bfd_put_32 (abfd, created, tdata->created);
585 abfd->tdata.mmo_data = tdata;
588 return true;
591 static boolean
592 mmo_bfd_copy_private_bfd_data (ibfd, obfd)
593 bfd *ibfd;
594 bfd *obfd;
596 if (bfd_get_flavour (ibfd) != bfd_target_mmo_flavour
597 || bfd_get_flavour (obfd) != bfd_target_mmo_flavour)
598 return true;
600 /* Copy the time the copied-from file was created. If people want the
601 time the file was last *modified*, they have that in the normal file
602 information. */
603 memcpy (obfd->tdata.mmo_data->created, ibfd->tdata.mmo_data->created,
604 sizeof (obfd->tdata.mmo_data->created));
605 return true;
608 /* Helper functions for mmo_decide_section, used through
609 bfd_map_over_sections. */
611 static void
612 mmo_find_sec_w_addr (abfd, sec, p)
613 bfd *abfd ATTRIBUTE_UNUSED;
614 asection *sec;
615 PTR p;
617 struct mmo_find_sec_info *infop = (struct mmo_find_sec_info *) p;
618 bfd_vma vma = bfd_get_section_vma (abfd, sec);
620 /* Ignore sections that aren't loaded. */
621 if ((bfd_get_section_flags (abfd, sec) & (SEC_LOAD | SEC_ALLOC))
622 != (SEC_LOAD | SEC_ALLOC))
623 return;
625 if (infop->addr >= vma && infop->addr < vma + sec->_raw_size)
626 infop->sec = sec;
629 static void
630 mmo_find_sec_w_addr_grow (abfd, sec, p)
631 bfd *abfd ATTRIBUTE_UNUSED;
632 asection *sec;
633 PTR p;
635 struct mmo_find_sec_info *infop = (struct mmo_find_sec_info *) p;
636 bfd_vma vma = bfd_get_section_vma (abfd, sec);
638 /* Ignore sections that aren't loaded. */
639 if ((bfd_get_section_flags (abfd, sec) & (SEC_LOAD | SEC_ALLOC))
640 != (SEC_LOAD | SEC_ALLOC))
641 return;
643 if (infop->addr >= vma && infop->addr < vma + MAX_ARTIFICIAL_SECTION_SIZE)
644 infop->sec = sec;
647 /* Find a section that corresponds to a VMA. Automatically create .text
648 or .data and set current section to it, depending on what vma. If we
649 can't deduce a section, make one up as ".MMIX.sec.N", where N is an
650 increasing number. */
652 static asection *
653 mmo_decide_section (abfd, vma)
654 bfd *abfd;
655 bfd_vma vma;
657 asection *sec = NULL;
658 char sec_name[sizeof (".MMIX.sec.") + 20];
659 struct mmo_find_sec_info info;
661 info.addr = vma;
662 info.sec = NULL;
664 /* First see if there's a section that would match exactly. */
665 bfd_map_over_sections (abfd, mmo_find_sec_w_addr, &info);
667 if (info.sec != NULL)
668 return info.sec;
670 /* If there's no such section, try and expand one of the existing ones,
671 up to a limit. Make sure we have .text and .data before we try that;
672 create them corresponding to expected addresses and set flags to make
673 them match the "loaded and with contents" expectation. */
674 if ((vma >> 56) == 0)
676 sec = bfd_make_section_old_way (abfd, MMO_TEXT_SECTION_NAME);
678 if (sec == NULL)
679 return NULL;
681 if (! sec->user_set_vma)
682 bfd_set_section_vma (abfd, sec, vma);
683 if (! bfd_set_section_flags (abfd, sec,
684 bfd_get_section_flags (abfd, sec)
685 | SEC_CODE | SEC_LOAD | SEC_ALLOC))
686 return NULL;
688 else if ((vma >> 56) == 0x20)
690 sec = bfd_make_section_old_way (abfd, MMO_DATA_SECTION_NAME);
692 if (sec == NULL)
693 return NULL;
695 if (! sec->user_set_vma)
696 bfd_set_section_vma (abfd, sec, vma);
697 if (! bfd_set_section_flags (abfd, sec,
698 bfd_get_section_flags (abfd, sec)
699 | SEC_LOAD | SEC_ALLOC))
700 return NULL;
703 bfd_map_over_sections (abfd, mmo_find_sec_w_addr_grow, &info);
705 if (info.sec != NULL)
706 return info.sec;
708 /* If there's still no suitable section, make a new one. */
709 sprintf (sec_name, ".MMIX.sec.%d", abfd->tdata.mmo_data->sec_no++);
710 sec = mmo_make_section (abfd, sec_name);
711 if (! sec->user_set_vma)
712 bfd_set_section_vma (abfd, sec, vma);
714 if (! bfd_set_section_flags (abfd, sec,
715 bfd_get_section_flags (abfd, sec)
716 | SEC_LOAD | SEC_ALLOC))
717 return NULL;
718 return sec;
721 /* Xor in a 64-bit value VALUE at VMA. */
723 static INLINE void
724 mmo_xore_64 (sec, vma, value)
725 asection *sec;
726 bfd_vma vma;
727 bfd_vma value;
729 bfd_byte *loc = mmo_get_loc (sec, vma, 8);
730 bfd_vma prev = bfd_get_64 (sec->owner, loc);
732 value ^= prev;
733 bfd_put_64 (sec->owner, value, loc);
736 /* Xor in a 32-bit value VALUE at VMA. */
738 static INLINE void
739 mmo_xore_32 (sec, vma, value)
740 asection *sec;
741 bfd_vma vma;
742 unsigned int value;
744 bfd_byte *loc = mmo_get_loc (sec, vma, 4);
745 unsigned int prev = bfd_get_32 (sec->owner, loc);
747 value ^= prev;
748 bfd_put_32 (sec->owner, value, loc);
751 /* Xor in a 16-bit value VALUE at VMA. */
753 static INLINE void
754 mmo_xore_16 (sec, vma, value)
755 asection *sec;
756 bfd_vma vma;
757 unsigned int value;
759 bfd_byte *loc = mmo_get_loc (sec, vma, 2);
760 unsigned int prev = bfd_get_16 (sec->owner, loc);
762 value ^= prev;
763 bfd_put_16 (sec->owner, value, loc);
766 /* Write a 32-bit word to output file, no lop_quote generated. */
768 static INLINE void
769 mmo_write_tetra_raw (abfd, value)
770 bfd *abfd;
771 unsigned int value;
773 bfd_byte buf[4];
775 bfd_put_32 (abfd, value, buf);
777 if (bfd_bwrite ((PTR) buf, 4, abfd) != 4)
778 abfd->tdata.mmo_data->have_error = true;
781 /* Write a 32-bit word to output file; lop_quote if necessary. */
783 static INLINE void
784 mmo_write_tetra (abfd, value)
785 bfd *abfd;
786 unsigned int value;
788 if (((value >> 24) & 0xff) == LOP)
789 mmo_write_tetra_raw (abfd, LOP_QUOTE_NEXT);
791 mmo_write_tetra_raw (abfd, value);
794 /* Write a 64-bit word to output file, perhaps with lop_quoting. */
796 static INLINE void
797 mmo_write_octa (abfd, value)
798 bfd *abfd;
799 bfd_vma value;
801 mmo_write_tetra (abfd, (unsigned int) (value >> 32));
802 mmo_write_tetra (abfd, (unsigned int) value);
805 /* Write a 64-bit word to output file, without lop_quoting. */
807 static INLINE void
808 mmo_write_octa_raw (abfd, value)
809 bfd *abfd;
810 bfd_vma value;
812 mmo_write_tetra_raw (abfd, (unsigned int) (value >> 32));
813 mmo_write_tetra_raw (abfd, (unsigned int) value);
816 /* Write quoted contents. Intended to be called multiple times in
817 sequence, followed by a call to mmo_flush_chunk. */
819 static INLINE boolean
820 mmo_write_chunk (abfd, loc, len)
821 bfd *abfd;
822 CONST bfd_byte *loc;
823 unsigned int len;
825 boolean retval = true;
827 /* Fill up a tetra from bytes remaining from a previous chunk. */
828 if (abfd->tdata.mmo_data->byte_no != 0)
830 while (abfd->tdata.mmo_data->byte_no < 4 && len != 0)
832 abfd->tdata.mmo_data->buf[abfd->tdata.mmo_data->byte_no++] = *loc++;
833 len--;
836 if (abfd->tdata.mmo_data->byte_no == 4)
838 mmo_write_tetra (abfd,
839 bfd_get_32 (abfd, abfd->tdata.mmo_data->buf));
840 abfd->tdata.mmo_data->byte_no = 0;
844 while (len >= 4)
846 if (loc[0] == LOP)
847 mmo_write_tetra_raw (abfd, LOP_QUOTE_NEXT);
849 retval
850 = (retval == true
851 && abfd->tdata.mmo_data->have_error == false
852 && 4 == bfd_bwrite ((PTR) loc, 4, abfd));
854 loc += 4;
855 len -= 4;
858 if (len)
860 memcpy (abfd->tdata.mmo_data->buf, loc, len);
861 abfd->tdata.mmo_data->byte_no = len;
864 if (retval == false)
865 abfd->tdata.mmo_data->have_error = true;
866 return retval;
869 /* Flush remaining bytes, from a previous mmo_write_chunk, zero-padded to
870 4 bytes. */
872 static INLINE boolean
873 mmo_flush_chunk (abfd)
874 bfd *abfd;
876 if (abfd->tdata.mmo_data->byte_no != 0)
878 memset (abfd->tdata.mmo_data->buf + abfd->tdata.mmo_data->byte_no,
879 0, 4 - abfd->tdata.mmo_data->byte_no);
880 mmo_write_tetra (abfd,
881 bfd_get_32 (abfd, abfd->tdata.mmo_data->buf));
882 abfd->tdata.mmo_data->byte_no = 0;
885 return abfd->tdata.mmo_data->have_error == false;
888 /* Same, but from a list. */
890 static INLINE boolean
891 mmo_write_chunk_list (abfd, datap)
892 bfd *abfd;
893 mmo_data_list_type *datap;
895 for (; datap != NULL; datap = datap->next)
896 if (! mmo_write_chunk (abfd, datap->data, datap->size))
897 return false;
899 return mmo_flush_chunk (abfd);
902 /* Write a lop_loc and some contents. A caller needs to call
903 mmo_flush_chunk after calling this function. The location is only
904 output if different than *LAST_VMAP, which is updated after this call. */
906 static boolean
907 mmo_write_loc_chunk (abfd, vma, loc, len, last_vmap)
908 bfd *abfd;
909 bfd_vma vma;
910 CONST bfd_byte *loc;
911 unsigned int len;
912 bfd_vma *last_vmap;
914 /* Find an initial and trailing section of zero tetras; we don't need to
915 write out zeros. FIXME: When we do this, we should emit section size
916 and address specifiers, else objcopy can't always perform an identity
917 translation. */
918 while (len >= 4 && bfd_get_32 (abfd, loc) == 0)
920 vma += 4;
921 len -= 4;
922 loc += 4;
925 while (len >= 4 && bfd_get_32 (abfd, loc + len - 4) == 0)
926 len -= 4;
928 /* Only write out the location if it's different than the one the caller
929 (supposedly) previously handled, accounting for omitted leading zeros. */
930 if (vma != *last_vmap)
932 /* We might be in the middle of a sequence. */
933 mmo_flush_chunk (abfd);
935 /* We always write the location as 64 bits; no use saving bytes
936 here. */
937 mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_LOC << 16) | 2);
938 mmo_write_octa_raw (abfd, vma);
941 /* Update to reflect end of this chunk, with trailing zeros omitted. */
942 *last_vmap = vma + len;
944 return
945 abfd->tdata.mmo_data->have_error == false
946 && mmo_write_chunk (abfd, loc, len);
949 /* Same, but from a list. */
951 static INLINE boolean
952 mmo_write_loc_chunk_list (abfd, datap)
953 bfd *abfd;
954 mmo_data_list_type *datap;
956 /* Get an address different than the address of the first chunk. */
957 bfd_vma last_vma = datap ? datap->where - 1 : 0;
959 for (; datap != NULL; datap = datap->next)
960 if (! mmo_write_loc_chunk (abfd, datap->where, datap->data, datap->size,
961 &last_vma))
962 return false;
964 return mmo_flush_chunk (abfd);
967 /* Make a .MMIX.spec_data.N section. */
969 static asection *
970 mmo_get_generic_spec_data_section (abfd, spec_data_number)
971 bfd *abfd;
972 int spec_data_number;
974 asection *sec;
975 char secname[sizeof (MMIX_OTHER_SPEC_SECTION_PREFIX) + 20]
976 = MMIX_OTHER_SPEC_SECTION_PREFIX;
978 sprintf (secname + strlen (MMIX_OTHER_SPEC_SECTION_PREFIX),
979 "%d", spec_data_number);
981 sec = mmo_make_section (abfd, secname);
983 return sec;
986 /* Make a special section for SPEC_DATA_NUMBER. If it is the one we use
987 ourselves, parse some of its data to get at the section name. */
989 static asection *
990 mmo_get_spec_section (abfd, spec_data_number)
991 bfd *abfd;
992 int spec_data_number;
994 bfd_byte *secname;
995 asection *sec;
996 bfd_byte buf[4];
997 unsigned int secname_length;
998 unsigned int i;
999 bfd_vma section_length;
1000 bfd_vma section_vma;
1001 mmo_data_list_type *loc;
1002 flagword flags;
1003 long orig_pos;
1005 /* If this isn't the "special" special data, then make a placeholder
1006 section. */
1007 if (spec_data_number != SPEC_DATA_SECTION)
1008 return mmo_get_generic_spec_data_section (abfd, spec_data_number);
1010 /* Seek back to this position if there was a format error. */
1011 orig_pos = bfd_tell (abfd);
1013 /* Read the length (in 32-bit words). */
1014 if (bfd_bread (buf, 4, abfd) != 4)
1015 goto format_error;
1017 if (buf[0] == LOP)
1019 if (buf[1] != LOP_QUOTE)
1020 goto format_error;
1022 if (bfd_bread (buf, 4, abfd) != 4)
1023 goto format_error;
1026 /* We don't care to keep the name length accurate. It's
1027 zero-terminated. */
1028 secname_length = bfd_get_32 (abfd, buf) * 4;
1030 /* Check section name length for sanity. */
1031 if (secname_length > MAX_SECTION_NAME_SIZE)
1032 goto format_error;
1034 /* This should be free'd regardless if a section is created. */
1035 secname = bfd_malloc (secname_length + 1);
1036 secname[secname_length] = 0;
1038 for (i = 0; i < secname_length / 4; i++)
1040 if (bfd_bread (secname + i * 4, 4, abfd) != 4)
1041 goto format_error_free;
1043 if (secname[i * 4] == LOP)
1045 /* A bit of overkill, but we handle char 0x98 in a section name,
1046 and recognize misparsing. */
1047 if (secname[i * 4 + 1] != LOP_QUOTE
1048 || bfd_bread (secname + i * 4, 4, abfd) != 4)
1049 /* Whoops. We thought this was a name, and now we found a
1050 non-lop_quote lopcode before we parsed the whole length of
1051 the name. Signal end-of-file in the same manner. */
1052 goto format_error_free;
1056 /* Get the section flags. */
1057 if (bfd_bread (buf, 4, abfd) != 4
1058 || (buf[0] == LOP
1059 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1060 goto format_error_free;
1062 flags = bfd_get_32 (abfd, buf);
1064 /* Get the section length. */
1065 if (bfd_bread (buf, 4, abfd) != 4
1066 || (buf[0] == LOP
1067 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1068 goto format_error_free;
1070 section_length = (bfd_vma) bfd_get_32 (abfd, buf) << 32;
1072 /* That's the first, high-part. Now get the low part. */
1074 if (bfd_bread (buf, 4, abfd) != 4
1075 || (buf[0] == LOP
1076 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1077 goto format_error_free;
1079 section_length |= (bfd_vma) bfd_get_32 (abfd, buf);
1081 /* Check the section length for sanity. */
1082 if (section_length > MAX_ARTIFICIAL_SECTION_SIZE)
1083 goto format_error_free;
1085 /* Get the section VMA. */
1086 if (bfd_bread (buf, 4, abfd) != 4
1087 || (buf[0] == LOP
1088 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1089 goto format_error_free;
1091 section_vma = (bfd_vma) bfd_get_32 (abfd, buf) << 32;
1093 /* That's the first, high-part. Now get the low part. */
1094 if (bfd_bread (buf, 4, abfd) != 4
1095 || (buf[0] == LOP
1096 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1097 goto format_error_free;
1099 section_vma |= (bfd_vma) bfd_get_32 (abfd, buf);
1101 sec = mmo_make_section (abfd, secname);
1102 free (secname);
1103 if (sec == NULL)
1104 goto format_error;
1106 /* We allocate a buffer here for the advertised size, with head room for
1107 tetrabyte alignment. */
1108 loc = bfd_zmalloc (section_length + 3
1109 + sizeof (struct mmo_data_list_struct));
1110 if (loc == NULL)
1111 goto format_error;
1113 /* Use a TETRA-rounded size for the allocated buffer; we set the
1114 "visible" section size below. */
1115 loc->size = (section_length + 3) & ~3;
1117 /* Add in the section flags we found to those bfd entered during this
1118 process and set the contents. */
1119 if (! bfd_set_section_flags (abfd, sec,
1120 bfd_sec_flags_from_mmo_flags (flags)
1121 | bfd_get_section_flags (abfd, sec)
1122 | (section_length != 0 ? SEC_HAS_CONTENTS : 0))
1123 || ! bfd_set_section_size (abfd, sec,
1124 sec->_cooked_size + section_length)
1125 /* Set VMA only for the first occurrence. */
1126 || (! sec->user_set_vma
1127 && ! bfd_set_section_vma (abfd, sec, section_vma)))
1129 /* If we get an error for any of the calls above, signal more than
1130 just a format error for the spec section. */
1131 return NULL;
1134 loc->next = NULL;
1135 if (((struct mmo_section_data_struct *) (sec->used_by_bfd))->tail != NULL)
1136 ((struct mmo_section_data_struct *) (sec->used_by_bfd))->tail->next
1137 = loc;
1138 else
1139 ((struct mmo_section_data_struct *) (sec->used_by_bfd))->head = loc;
1140 ((struct mmo_section_data_struct *) (sec->used_by_bfd))->tail = loc;
1141 loc->where = section_vma;
1143 return sec;
1145 format_error_free:
1146 free (secname);
1147 format_error:
1148 if (bfd_seek (abfd, orig_pos, SEEK_SET) != 0)
1149 return NULL;
1151 return mmo_get_generic_spec_data_section (abfd, spec_data_number);
1154 /* Read a byte, but read from file in multiples of 32-bit words. */
1156 static bfd_byte
1157 mmo_get_byte (abfd)
1158 bfd *abfd;
1160 bfd_byte retval;
1162 if (abfd->tdata.mmo_data->byte_no == 0)
1164 if (abfd->tdata.mmo_data->have_error == false
1165 && bfd_bread (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
1167 abfd->tdata.mmo_data->have_error = true;
1169 /* A value somewhat safe against tripping on some inconsistency
1170 when mopping up after this error. */
1171 return 128;
1175 retval = abfd->tdata.mmo_data->buf[abfd->tdata.mmo_data->byte_no];
1176 abfd->tdata.mmo_data->byte_no = (abfd->tdata.mmo_data->byte_no + 1) % 4;
1178 return retval;
1181 /* Write a byte, in multiples of 32-bit words. */
1183 static void
1184 mmo_write_byte (abfd, value)
1185 bfd *abfd;
1186 bfd_byte value;
1188 abfd->tdata.mmo_data->buf[(abfd->tdata.mmo_data->byte_no++ % 4)] = value;
1189 if ((abfd->tdata.mmo_data->byte_no % 4) == 0)
1191 if (abfd->tdata.mmo_data->have_error == false
1192 && bfd_bwrite (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
1193 abfd->tdata.mmo_data->have_error = true;
1197 /* Create a symbol. */
1199 static boolean
1200 mmo_create_symbol (abfd, symname, addr, sym_type, serno)
1201 bfd *abfd;
1202 CONST char *symname;
1203 bfd_vma addr;
1204 enum mmo_sym_type sym_type;
1205 unsigned int serno;
1207 struct mmo_symbol *n;
1209 n = (struct mmo_symbol *) bfd_alloc (abfd, sizeof (struct mmo_symbol));
1210 if (n == NULL)
1211 return false;
1213 n->name = bfd_alloc (abfd, strlen (symname) + 1);
1214 if (n->name == NULL)
1215 return false;
1217 strcpy ((PTR) n->name, symname);
1219 n->value = addr;
1220 n->sym_type = sym_type;
1221 n->serno = serno;
1223 if (abfd->tdata.mmo_data->symbols == NULL)
1224 abfd->tdata.mmo_data->symbols = n;
1225 else
1226 abfd->tdata.mmo_data->symtail->next = n;
1227 abfd->tdata.mmo_data->symtail = n;
1228 n->next = NULL;
1230 ++abfd->symcount;
1232 /* Check that :Main equals the last octa of the .MMIX.reg_contents
1233 section, as it's the one place we're sure to pass when reading a mmo
1234 object. For written objects, we do it while setting the symbol
1235 table. */
1236 if (strcmp (symname, MMIX_START_SYMBOL_NAME) == 0
1237 && bfd_get_start_address (abfd) != addr)
1239 (*_bfd_error_handler)
1240 (_("%s: invalid mmo file: initialization value for $255 is not `Main'\n"),
1241 bfd_get_filename (abfd));
1242 bfd_set_error (bfd_error_bad_value);
1243 return false;
1246 return true;
1249 /* Read in symbols. */
1251 static boolean
1252 mmo_get_symbols (abfd)
1253 bfd *abfd;
1256 INODE
1257 Symbol-table, mmo section mapping, File layout, mmo
1258 SUBSECTION
1259 Symbol table format
1261 From mmixal.w (or really, the generated mmixal.tex) in
1262 @url{http://www-cs-faculty.stanford.edu/~knuth/programs/mmix.tar.gz}):
1263 ``Symbols are stored and retrieved by means of a @samp{ternary
1264 search trie}, following ideas of Bentley and Sedgewick. (See
1265 ACM--SIAM Symp.@: on Discrete Algorithms @samp{8} (1997), 360--369;
1266 R.@:Sedgewick, @samp{Algorithms in C} (Reading, Mass.@:
1267 Addison--Wesley, 1998), @samp{15.4}.) Each trie node stores a
1268 character, and there are branches to subtries for the cases where
1269 a given character is less than, equal to, or greater than the
1270 character in the trie. There also is a pointer to a symbol table
1271 entry if a symbol ends at the current node.''
1273 So it's a tree encoded as a stream of bytes. The stream of bytes
1274 acts on a single virtual global symbol, adding and removing
1275 characters and signalling complete symbol points. Here, we read
1276 the stream and create symbols at the completion points.
1278 First, there's a control byte <<m>>. If any of the listed bits
1279 in <<m>> is nonzero, we execute what stands at the right, in
1280 the listed order:
1282 | (MMO3_LEFT)
1283 | 0x40 - Traverse left trie.
1284 | (Read a new command byte and recurse.)
1286 | (MMO3_SYMBITS)
1287 | 0x2f - Read the next byte as a character and store it in the
1288 | current character position; increment character position.
1289 | Test the bits of <<m>>:
1291 | (MMO3_WCHAR)
1292 | 0x80 - The character is 16-bit (so read another byte,
1293 | merge into current character.
1295 | (MMO3_TYPEBITS)
1296 | 0xf - We have a complete symbol; parse the type, value
1297 | and serial number and do what should be done
1298 | with a symbol. The type and length information
1299 | is in j = (m & 0xf).
1301 | (MMO3_REGQUAL_BITS)
1302 | j == 0xf: A register variable. The following
1303 | byte tells which register.
1304 | j <= 8: An absolute symbol. Read j bytes as the
1305 | big-endian number the symbol equals.
1306 | A j = 2 with two zero bytes denotes an
1307 | unknown symbol.
1308 | j > 8: As with j <= 8, but add (0x20 << 56)
1309 | to the value in the following j - 8
1310 | bytes.
1312 | Then comes the serial number, as a variant of
1313 | uleb128, but better named ubeb128:
1314 | Read bytes and shift the previous value left 7
1315 | (multiply by 128). Add in the new byte, repeat
1316 | until a byte has bit 7 set. The serial number
1317 | is the computed value minus 128.
1319 | (MMO3_MIDDLE)
1320 | 0x20 - Traverse middle trie. (Read a new command byte
1321 | and recurse.) Decrement character position.
1323 | (MMO3_RIGHT)
1324 | 0x10 - Traverse right trie. (Read a new command byte and
1325 | recurse.)
1327 Let's look again at the <<lop_stab>> for the trivial file
1328 (@pxref{File layout}).
1330 | 0x980b0000 - lop_stab for ":Main" = 0, serial 1.
1331 | 0x203a4040
1332 | 0x10404020
1333 | 0x4d206120
1334 | 0x69016e00
1335 | 0x81000000
1337 This forms the trivial trie (note that the path between ``:'' and
1338 ``M'' is redundant):
1340 | 203a ":"
1341 | 40 /
1342 | 40 /
1343 | 10 \
1344 | 40 /
1345 | 40 /
1346 | 204d "M"
1347 | 2061 "a"
1348 | 2069 "i"
1349 | 016e "n" is the last character in a full symbol, and
1350 | with a value represented in one byte.
1351 | 00 The value is 0.
1352 | 81 The serial number is 1. */
1354 bfd_byte m = mmo_get_byte (abfd);
1356 /* Check first if we have a bad hair day. */
1357 if (abfd->tdata.mmo_data->have_error == true)
1358 return false;
1360 if (m & MMO3_LEFT)
1361 /* Traverse left trie. */
1362 mmo_get_symbols (abfd);
1364 if (m & MMO3_SYMBITS)
1366 bfd_byte c = mmo_get_byte (abfd);
1367 bfd_byte j = m & MMO3_TYPEBITS;
1368 bfd_vma addr = 0;
1369 enum mmo_sym_type sym_type;
1370 unsigned int serno = 0;
1371 bfd_byte k;
1373 if (m & MMO3_WCHAR)
1375 bfd_byte c2 = mmo_get_byte (abfd);
1377 /* A two-byte character. We can't grok this, but neither can
1378 mmotype, for other cases than the second byte being zero. */
1380 if (c != 0)
1382 abfd->tdata.mmo_data->lop_stab_symbol
1383 [abfd->tdata.mmo_data->symbol_position] = 0;
1385 (*_bfd_error_handler)
1386 (_("%s: unsupported wide character sequence\
1387 0x%02X 0x%02X after symbol name starting with `%s'\n"),
1388 bfd_get_filename (abfd), c, c2,
1389 abfd->tdata.mmo_data->lop_stab_symbol);
1390 bfd_set_error (bfd_error_bad_value);
1391 abfd->tdata.mmo_data->have_error = true;
1392 return false;
1394 else
1395 c = c2;
1398 abfd->tdata.mmo_data->lop_stab_symbol[abfd->tdata.mmo_data->symbol_position++] = c;
1399 abfd->tdata.mmo_data->lop_stab_symbol[abfd->tdata.mmo_data->symbol_position] = 0;
1401 if (j & MMO3_REGQUAL_BITS)
1403 if (j == MMO3_REGQUAL_BITS)
1405 sym_type = mmo_reg_sym;
1406 addr = mmo_get_byte (abfd);
1408 else if (j <= 8)
1410 unsigned int i;
1412 for (i = 0; i < j; i++)
1413 addr = (addr << 8) + mmo_get_byte (abfd);
1415 if (addr == 0 && j == MMO3_UNDEF)
1416 sym_type = mmo_undef_sym;
1417 else
1418 sym_type = mmo_abs_sym;
1420 else
1422 unsigned int i;
1424 for (i = MMO3_DATA; i < j; i++)
1425 addr = (addr << 8) + mmo_get_byte (abfd);
1427 addr += (bfd_vma) 0x20 << 56;
1428 sym_type = mmo_data_sym;
1431 /* Get the serial number. */
1434 k = mmo_get_byte (abfd);
1435 serno = (serno << 7) + k;
1437 while (k < 128);
1438 serno -= 128;
1440 /* Got it. Now enter it. Skip a leading ":". */
1441 if (abfd->tdata.mmo_data->have_error == false
1442 && ! mmo_create_symbol (abfd,
1443 abfd->tdata.mmo_data->lop_stab_symbol
1444 + 1,
1445 addr, sym_type, serno))
1446 abfd->tdata.mmo_data->have_error = true;
1449 if (m & MMO3_MIDDLE)
1450 /* Traverse middle trie. */
1451 mmo_get_symbols (abfd);
1453 abfd->tdata.mmo_data->symbol_position--;
1456 if (m & MMO3_RIGHT)
1457 /* Traverse right trie. */
1458 mmo_get_symbols (abfd);
1460 return abfd->tdata.mmo_data->have_error == false;
1463 /* Get the location of memory area [VMA..VMA + SIZE - 1], which we think
1464 is in section SEC. Adjust and reallocate zero-initialized contents.
1465 If there's new contents, allocate to the next multiple of
1466 MMO_SEC_CONTENTS_CHUNK_SIZE. */
1468 static INLINE bfd_byte *
1469 mmo_get_loc (sec, vma, size)
1470 asection *sec;
1471 bfd_vma vma;
1472 int size;
1474 bfd_size_type allocated_size;
1475 struct mmo_section_data_struct *sdatap
1476 = (struct mmo_section_data_struct *) sec->used_by_bfd;
1477 struct mmo_data_list_struct *datap = sdatap->head;
1478 struct mmo_data_list_struct *entry;
1480 /* First search the list to see if we have the requested chunk in one
1481 piece, or perhaps if we have a suitable chunk with room to fit. */
1482 for (; datap != NULL; datap = datap->next)
1484 if (datap->where <= vma
1485 && datap->where + datap->size >= vma + size)
1486 return datap->data + vma - datap->where;
1487 else if (datap->where <= vma
1488 && datap->where + datap->allocated_size >= vma + size
1489 /* Only munch on the "allocated size" if it does not
1490 overlap the next chunk. */
1491 && (datap->next == NULL || datap->next->where >= vma + size))
1493 /* There was room allocated, but the size wasn't set to include
1494 it. Do that now. */
1495 datap->size += (vma + size) - (datap->where + datap->size);
1497 /* Update the section size. This happens only if we update the
1498 32-bit-aligned chunk size. Callers that have
1499 non-32-bit-aligned sections should do all allocation and
1500 size-setting by themselves or at least set the section size
1501 after the last allocating call to this function. */
1502 if (vma + size > sec->vma + sec->_raw_size)
1503 sec->_raw_size += (vma + size) - (sec->vma + sec->_raw_size);
1505 return datap->data + vma - datap->where;
1509 /* Not found; allocate a new block. First check in case we get a
1510 request for a size split up over several blocks; we'll have to return
1511 NULL for those cases, requesting the caller to split up the request.
1512 Requests with an address aligned on MMO_SEC_CONTENTS_CHUNK_SIZE bytes and
1513 for no more than MMO_SEC_CONTENTS_CHUNK_SIZE will always get resolved. */
1515 for (datap = sdatap->head; datap != NULL; datap = datap->next)
1516 if ((datap->where <= vma && datap->where + datap->size > vma)
1517 || (datap->where < vma + size
1518 && datap->where + datap->size >= vma + size))
1519 return NULL;
1521 allocated_size
1522 = (size + MMO_SEC_CONTENTS_CHUNK_SIZE - 1) & ~(MMO_SEC_CONTENTS_CHUNK_SIZE - 1);
1523 entry = (mmo_data_list_type *)
1524 bfd_zalloc (sec->owner, sizeof (mmo_data_list_type) + allocated_size);
1525 if (entry == NULL)
1526 return false;
1527 entry->where = vma;
1528 entry->size = size;
1529 entry->allocated_size = allocated_size;
1531 datap = sdatap->head;
1533 /* Sort the records by address. Optimize for the common case of adding
1534 a record to the end of the list. */
1535 if (sdatap->tail != NULL && entry->where >= sdatap->tail->where)
1537 sdatap->tail->next = entry;
1538 entry->next = NULL;
1539 sdatap->tail = entry;
1541 else
1543 mmo_data_list_type **look;
1544 for (look = &sdatap->head;
1545 *look != NULL && (*look)->where < entry->where;
1546 look = &(*look)->next)
1548 entry->next = *look;
1549 *look = entry;
1550 if (entry->next == NULL)
1552 sdatap->tail = entry;
1554 /* We get here for the first time (at other times too) for this
1555 section. Say we have contents. */
1556 if (! bfd_set_section_flags (sec->owner, sec,
1557 bfd_get_section_flags (sec->owner, sec)
1558 | SEC_HAS_CONTENTS))
1559 return NULL;
1563 /* Update the section size. This happens only when we add contents and
1564 re-size as we go. The section size will then be aligned to 32 bits. */
1565 if (vma + size > sec->vma + sec->_raw_size)
1566 sec->_raw_size += (vma + size) - (sec->vma + sec->_raw_size);
1567 return entry->data;
1570 /* Set sizes once we've read in all sections. */
1572 static void
1573 mmo_map_set_sizes (abfd, sec, ignored)
1574 bfd *abfd ATTRIBUTE_UNUSED;
1575 asection *sec;
1576 PTR ignored ATTRIBUTE_UNUSED;
1578 sec->_cooked_size = sec->_raw_size;
1579 sec->lma = sec->vma;
1582 /* Read the mmo file and turn it into sections. */
1584 static boolean
1585 mmo_scan (abfd)
1586 bfd *abfd;
1588 unsigned int i;
1589 unsigned int lineno = 1;
1590 boolean error = false;
1591 bfd_vma vma = 0;
1592 asection *sec = bfd_make_section_old_way (abfd, MMO_TEXT_SECTION_NAME);
1593 asection *non_spec_sec = NULL;
1594 bfd_vma non_spec_vma = 0;
1595 char *current_filename = NULL;
1596 bfd_size_type nbytes_read = 0;
1597 /* Buffer with room to read a 64-bit value. */
1598 bfd_byte buf[8];
1599 long stab_loc = -1;
1600 char *file_names[256];
1602 memset (file_names, 0, sizeof (file_names));
1604 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
1605 goto error_return;
1607 while ((nbytes_read = bfd_bread (buf, 4, abfd)) == 4)
1609 if (buf[0] == LOP)
1611 unsigned int y = bfd_get_8 (abfd, buf + 2);
1612 unsigned int z = bfd_get_8 (abfd, buf + 3);
1614 /* Change back to the original section for lopcodes other
1615 than LOP_QUOTE that comes after a LOP_SPEC. */
1616 if ((buf[1] != LOP_QUOTE || y != 0 || z != 1)
1617 && non_spec_sec != NULL)
1619 sec = non_spec_sec;
1620 vma = non_spec_vma;
1621 non_spec_sec = NULL;
1624 switch (buf[1])
1626 default:
1627 (*_bfd_error_handler)
1628 (_("%s: invalid mmo file: unsupported lopcode `%d'\n"),
1629 bfd_get_filename (abfd), buf[1]);
1630 bfd_set_error (bfd_error_bad_value);
1631 goto error_return;
1633 case LOP_QUOTE:
1634 /* Quote the next 32-bit word. */
1635 if (y != 0 || z != 1)
1637 (*_bfd_error_handler)
1638 (_("%s: invalid mmo file: expected YZ = 1 got YZ = %d for lop_quote\n"),
1639 bfd_get_filename (abfd), y*256+z);
1640 bfd_set_error (bfd_error_bad_value);
1641 goto error_return;
1643 if (bfd_bread (buf, 4, abfd) != 4)
1644 goto error_return;
1646 mmo_xore_32 (sec, vma, bfd_get_32 (abfd, buf));
1647 vma += 4;
1648 vma &= ~3;
1649 lineno++;
1650 break;
1652 case LOP_LOC:
1653 /* Set vma (and section). */
1654 vma = (bfd_vma) y << 56;
1655 if (z == 1)
1657 /* Get a 32-bit value. */
1658 if (bfd_bread (buf, 4, abfd) != 4)
1659 goto error_return;
1661 vma += bfd_get_32 (abfd, buf);
1663 else if (z == 2)
1665 /* Get a 64-bit value. */
1666 if (bfd_bread (buf, 8, abfd) != 8)
1667 goto error_return;
1669 vma += bfd_get_64 (abfd, buf);
1671 else
1673 (*_bfd_error_handler)
1674 (_("%s: invalid mmo file: expected z = 1 or z = 2, got z = %d for lop_loc\n"),
1675 bfd_get_filename (abfd), z);
1676 bfd_set_error (bfd_error_bad_value);
1677 goto error_return;
1680 sec = mmo_decide_section (abfd, vma);
1681 if (sec == NULL)
1682 goto error_return;
1683 break;
1685 case LOP_SKIP:
1686 /* Move forward within the same section. */
1687 vma += y * 256 + z;
1689 sec = mmo_decide_section (abfd, vma);
1690 if (sec == NULL)
1691 goto error_return;
1692 break;
1694 case LOP_FIXO:
1695 /* A fixup: Store the current vma somewhere. Position using
1696 same format as LOP_LOC. */
1698 bfd_vma p = (bfd_vma) y << 56;
1699 asection *fixosec;
1701 if (z == 1)
1703 /* Get a 32-bit value. */
1704 if (bfd_bread (buf, 4, abfd) != 4)
1705 goto error_return;
1707 p += bfd_get_32 (abfd, buf);
1709 else if (z == 2)
1711 /* Get a 64-bit value. */
1712 if (bfd_bread (buf, 8, abfd) != 8)
1713 goto error_return;
1715 p += bfd_get_64 (abfd, buf);
1717 else
1719 (*_bfd_error_handler)
1720 (_("%s: invalid mmo file: expected z = 1 or z = 2, got z = %d for lop_fixo\n"),
1721 bfd_get_filename (abfd), z);
1722 bfd_set_error (bfd_error_bad_value);
1723 goto error_return;
1726 /* The section where we store this address might be a
1727 different one than the current section. */
1728 fixosec = mmo_decide_section (abfd, p);
1729 if (fixosec == NULL)
1730 goto error_return;
1731 mmo_xore_64 (fixosec, p, vma);
1733 break;
1735 case LOP_FIXR:
1736 /* A fixup: Store YZ of this lopcode into YZ at vma - 4 * yz. */
1738 unsigned int yz = (y * 256 + z);
1739 bfd_vma p = vma + 2 - 4 * yz;
1740 asection *fixrsec = mmo_decide_section (abfd, p);
1741 if (fixrsec == NULL)
1742 goto error_return;
1743 mmo_xore_16 (fixrsec, p, yz);
1745 break;
1747 case LOP_FIXRX:
1748 /* A fixup, similar to lop_fixr, but taking larger numbers
1749 and can change branches into the opposite direction
1750 (gasp!). */
1752 bfd_vma delta;
1753 bfd_vma p;
1754 asection *fixrsec;
1756 if (y != 0)
1758 (*_bfd_error_handler)
1759 (_("%s: invalid mmo file: expected y = 0, got y = %d for lop_fixrx\n"),
1760 bfd_get_filename (abfd), y);
1761 bfd_set_error (bfd_error_bad_value);
1762 goto error_return;
1765 if (z != 16 && z != 24)
1767 (*_bfd_error_handler)
1768 (_("%s: invalid mmo file: expected z = 16 or z = 24, got z = %d for lop_fixrx\n"),
1769 bfd_get_filename (abfd), z);
1770 bfd_set_error (bfd_error_bad_value);
1771 goto error_return;
1774 /* Get the next 32-bit value. */
1775 if (bfd_bread (buf, 4, abfd) != 4)
1776 goto error_return;
1778 delta = bfd_get_32 (abfd, buf);
1780 /* Do an, ehm, involved calculation for the location of
1781 the fixup. See mmixal documentation for a verbose
1782 explanation. We follow it verbosely here for the
1783 readers delight. */
1784 if (buf[0] == 0)
1785 p = vma - 4 * delta;
1786 else if (buf[0] == 1)
1787 p = vma - 4 * ((delta & 0xffffff) - (1 << z));
1788 else
1790 (*_bfd_error_handler)
1791 (_("%s: invalid mmo file: leading byte of operand word must be 0 or 1, got %d for lop_fixrx\n"),
1792 bfd_get_filename (abfd), buf[0]);
1793 bfd_set_error (bfd_error_bad_value);
1794 goto error_return;
1797 fixrsec = mmo_decide_section (abfd, vma);
1798 if (fixrsec == NULL)
1799 goto error_return;
1800 mmo_xore_32 (fixrsec, p, delta);
1802 break;
1804 case LOP_FILE:
1805 /* Set current file and perhaps the file name. Reset line
1806 number. */
1807 if (z != 0)
1809 char *fname = bfd_malloc (z * 4 + 1);
1811 if (fname == NULL)
1813 (*_bfd_error_handler)
1814 (_("%s: cannot allocate file name for file number %d, %d bytes\n"),
1815 bfd_get_filename (abfd), y, z * 4 + 1);
1816 bfd_set_error (bfd_error_system_call);
1817 goto error_return;
1820 fname[z * 4] = 0;
1822 for (i = 0; i < z; i++)
1824 if (bfd_bread (fname + i * 4, 4, abfd) != 4)
1826 free (fname);
1827 goto error_return;
1831 if (file_names[y] != NULL)
1833 (*_bfd_error_handler)
1834 (_("%s: invalid mmo file: file number %d `%s',\
1835 was already entered as `%s'\n"),
1836 bfd_get_filename (abfd), y, fname, file_names[y]);
1837 bfd_set_error (bfd_error_bad_value);
1838 goto error_return;
1841 file_names[y] = fname;
1844 if (file_names[y] == NULL)
1846 (*_bfd_error_handler)
1847 (_("%s: invalid mmo file: file name for number %d\
1848 was not specified before use\n"),
1849 bfd_get_filename (abfd), y);
1850 bfd_set_error (bfd_error_bad_value);
1851 goto error_return;
1854 current_filename = file_names[y];
1855 lineno = 0;
1856 break;
1858 case LOP_LINE:
1859 /* Set line number. */
1860 lineno = y * 256 + z;
1861 /* FIXME: Create a sequence of mmo-specific line number
1862 entries for each section, then translate into canonical
1863 format. */
1864 break;
1866 case LOP_SPEC:
1867 /* Special data follows until the next non-lop_quote
1868 lopcode. */
1869 non_spec_sec = sec;
1870 non_spec_vma = vma;
1871 sec = mmo_get_spec_section (abfd, y * 256 + z);
1872 if (sec == NULL)
1873 goto error_return;
1875 vma = sec->vma;
1876 break;
1878 case LOP_PRE:
1880 /* We ignore header information, except we read in the
1881 creation time from the first 32-bit word with the time
1882 in seconds since era. */
1883 if (z >= 1
1884 && bfd_bread (abfd->tdata.mmo_data->created, 4,
1885 abfd) != 4)
1886 goto error_return;
1888 for (i = 1; i < z; i++)
1889 if (bfd_bread (buf, 4, abfd) != 4)
1890 goto error_return;
1892 break;
1894 case LOP_POST:
1895 /* This tells of the contents of registers $Z..$255 at
1896 startup. We make a section out of it, with VMA = Z * 8,
1897 but only if Z != 255 or the contents is non-zero. */
1899 asection *rsec;
1900 bfd_byte *loc;
1901 bfd_vma first_octa;
1902 bfd_vma startaddr_octa;
1904 /* Read first octaword outside loop to simplify logic when
1905 excluding the Z == 255, octa == 0 case. */
1906 if (bfd_bread (buf, 8, abfd) != 8)
1907 goto error_return;
1909 first_octa = bfd_get_64 (abfd, buf);
1911 /* Don't emit contents for the trivial case which is
1912 always present; $255 pointing to Main. */
1913 if (z != 255)
1915 rsec
1916 = bfd_make_section_old_way (abfd,
1917 MMIX_REG_CONTENTS_SECTION_NAME);
1918 rsec->vma = z * 8;
1919 loc = mmo_get_loc (rsec, z * 8, (255 - z) * 8);
1920 bfd_put_64 (abfd, first_octa, loc);
1922 for (i = z + 1; i < 255; i++)
1924 if (bfd_bread (loc + (i - z) * 8, 8, abfd) != 8)
1925 goto error_return;
1928 /* Read out the last octabyte, and use it to set the
1929 start address. */
1930 if (bfd_bread (buf, 8, abfd) != 8)
1931 goto error_return;
1933 startaddr_octa = bfd_get_64 (abfd, buf);
1935 else
1936 startaddr_octa = first_octa;
1938 if (! bfd_set_start_address (abfd, startaddr_octa))
1940 /* Currently this can't fail, but this should handle
1941 future failures. */
1942 bfd_set_error (bfd_error_bad_value);
1943 goto error_return;
1946 break;
1948 case LOP_STAB:
1949 /* We read in the symbols now, not later. */
1950 if (y != 0 || z != 0)
1952 (*_bfd_error_handler)
1953 (_("%s: invalid mmo file: fields y and z of lop_stab\
1954 non-zero, y: %d, z: %d\n"),
1955 bfd_get_filename (abfd), y, z);
1956 bfd_set_error (bfd_error_bad_value);
1957 goto error_return;
1960 /* Save the location, so we can check that YZ in the LOP_END
1961 is correct. */
1962 stab_loc = bfd_tell (abfd);
1964 /* It's not said that an MMO can be without symbols (though
1965 mmixal will refuse to assemble files without Main), but
1966 it seems it would still be a valid mmo-file, so allow it.
1967 We detect the absence of a symbol area in that the upper
1968 limit is computed (from the lop_end YZ field) as 0.
1969 Don't call mmo_get_symbols; it can only detect the end of
1970 a valid symbol trie, not the absence of one. */
1971 if (abfd->tdata.mmo_data->max_symbol_length != 0
1972 && ! mmo_get_symbols (abfd))
1973 goto error_return;
1974 break;
1976 case LOP_END:
1978 /* This must be the last 32-bit word in an mmo file.
1979 Let's find out. */
1980 struct stat statbuf;
1981 long curpos = bfd_tell (abfd);
1983 if (bfd_stat (abfd, &statbuf) < 0)
1984 goto error_return;
1986 if (statbuf.st_size != curpos)
1988 (*_bfd_error_handler)
1989 (_("%s: invalid mmo file: lop_end not last item in\
1990 file\n"),
1991 bfd_get_filename (abfd));
1992 bfd_set_error (bfd_error_bad_value);
1993 goto error_return;
1996 /* Check that the YZ field is right. Subtract the size of
1997 this LOP_END in the calculation; YZ does not include
1998 it. */
1999 if ((long) (y * 256 + z) * 4 != (curpos - stab_loc) - 4)
2001 (*_bfd_error_handler)
2002 (_("%s: invalid mmo file: YZ of lop_end (%ld)\
2003 not equal to the number of tetras to the preceding lop_stab (%ld)\n"),
2004 bfd_get_filename (abfd), (long) (y * 256 + z),
2005 (curpos - stab_loc - 4)/4);
2006 bfd_set_error (bfd_error_bad_value);
2007 goto error_return;
2010 bfd_map_over_sections (abfd, mmo_map_set_sizes, NULL);
2011 goto done;
2015 else
2017 /* This wasn't a lopcode, so store it in the current section. */
2018 mmo_xore_32 (sec, vma & ~3, bfd_get_32 (abfd, buf));
2019 vma += 4;
2020 vma &= ~3;
2021 lineno++;
2025 /* We know this file is a multiple of four bytes (checked in
2026 mmo_object_p), so if we got something other than 0, this was a bad
2027 file (although it's more likely we'll get 0 in that case too).
2028 If we got end-of-file, then there was no lop_stab, so the file has
2029 invalid format. */
2031 if (nbytes_read != 0)
2032 bfd_set_error (bfd_error_system_call);
2033 else
2034 bfd_set_error (bfd_error_bad_value);
2036 error_return:
2037 error = true;
2038 done:
2039 /* Mark the .text and .data section with their normal attribute if they
2040 contain anything. This is not redundant wrt. mmo_decide_section,
2041 since that code might never execute, and conversely the alloc+code
2042 section flags must be set then. */
2043 sec = bfd_get_section_by_name (abfd, MMO_TEXT_SECTION_NAME);
2044 if (sec != NULL
2045 && (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
2046 && ! bfd_set_section_flags (abfd, sec,
2047 bfd_get_section_flags (abfd, sec)
2048 | SEC_ALLOC | SEC_LOAD | SEC_CODE))
2049 error = true;
2051 sec = bfd_get_section_by_name (abfd, MMO_DATA_SECTION_NAME);
2052 if (sec != NULL
2053 && (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
2054 && ! bfd_set_section_flags (abfd, sec,
2055 bfd_get_section_flags (abfd, sec)
2056 | SEC_ALLOC | SEC_LOAD))
2057 error = true;
2059 /* Free whatever resources we took. */
2060 for (i = 0; i < sizeof (file_names) / sizeof (file_names[0]); i++)
2061 if (file_names[i])
2062 free (file_names[i]);
2063 return error ? false : true;
2066 /* A hook to set up object file dependent section information. For mmo,
2067 we point out the shape of allocated section contents. */
2069 static boolean
2070 mmo_new_section_hook (abfd, newsect)
2071 bfd *abfd ATTRIBUTE_UNUSED;
2072 asection *newsect;
2074 /* We zero-fill all fields and assume NULL is represented by an all
2075 zero-bit pattern. */
2076 newsect->used_by_bfd =
2077 (PTR) bfd_zalloc (abfd, sizeof (struct mmo_section_data_struct));
2079 if (!newsect->used_by_bfd)
2080 return false;
2082 /* Always align to at least 32-bit words. */
2083 newsect->alignment_power = 2;
2084 return true;
2087 /* We already have section contents loaded for sections that have
2088 contents. */
2090 static boolean
2091 mmo_get_section_contents (abfd, sec, location, offset, bytes_to_do)
2092 bfd *abfd ATTRIBUTE_UNUSED;
2093 asection *sec ATTRIBUTE_UNUSED;
2094 PTR location ATTRIBUTE_UNUSED;
2095 file_ptr offset ATTRIBUTE_UNUSED;
2096 bfd_size_type bytes_to_do ATTRIBUTE_UNUSED;
2098 /* Iterate over diminishing chunk sizes, copying contents, like
2099 mmo_set_section_contents. */
2100 while (bytes_to_do)
2102 /* A minor song-and-dance to make sure we're not bitten by the
2103 distant possibility of the cast from bfd_vma to int making the
2104 chunk zero-sized. */
2105 int chunk_size
2106 = (int) bytes_to_do != 0 ? bytes_to_do : MMO_SEC_CONTENTS_CHUNK_SIZE;
2107 bfd_byte *loc;
2110 loc = mmo_get_loc (sec, sec->vma + offset, chunk_size);
2111 while (loc == NULL && (chunk_size /= 2) != 0);
2113 if (chunk_size == 0)
2114 return false;
2116 memcpy (location, loc, chunk_size);
2118 location += chunk_size;
2119 bytes_to_do -= chunk_size;
2120 offset += chunk_size;
2122 return true;
2125 /* Return the amount of memory needed to read the symbol table. */
2127 static long
2128 mmo_get_symtab_upper_bound (abfd)
2129 bfd *abfd ATTRIBUTE_UNUSED;
2131 return (abfd->symcount + 1) * sizeof (asymbol *);
2134 /* Sort mmo symbols by serial number. */
2136 static int
2137 mmo_sort_mmo_symbols (arg1, arg2)
2138 CONST PTR arg1;
2139 CONST PTR arg2;
2141 CONST struct mmo_symbol *sym1 = *(CONST struct mmo_symbol **) arg1;
2142 CONST struct mmo_symbol *sym2 = *(CONST struct mmo_symbol **) arg2;
2144 /* Sort by serial number first. */
2145 if (sym1->serno < sym2->serno)
2146 return -1;
2147 else if (sym1->serno > sym2->serno)
2148 return 1;
2150 /* Then sort by address of the table entries. */
2151 return ((CONST char *) arg1 - (CONST char *) arg2);
2154 /* Translate the symbol table. */
2156 static long
2157 mmo_get_symtab (abfd, alocation)
2158 bfd *abfd;
2159 asymbol **alocation;
2161 unsigned int symcount = bfd_get_symcount (abfd);
2162 asymbol *csymbols;
2163 unsigned int i;
2165 csymbols = abfd->tdata.mmo_data->csymbols;
2166 if (csymbols == NULL)
2168 asymbol *c;
2169 struct mmo_symbol *s;
2170 struct mmo_symbol **msp;
2172 /* First we store the symbols into the table we'll return, then we
2173 qsort it on the serial number, with secondary on the address of
2174 the symbol, to preserve order if there would be non-unique serial
2175 numbers. */
2176 for (s = abfd->tdata.mmo_data->symbols,
2177 msp = (struct mmo_symbol **) alocation;
2178 s != NULL;
2179 s = s->next, ++msp)
2180 *msp = s;
2182 *msp = NULL;
2184 qsort (alocation, symcount, sizeof (struct mmo_symbol *),
2185 mmo_sort_mmo_symbols);
2187 csymbols = (asymbol *) bfd_alloc (abfd, symcount * sizeof (asymbol));
2188 if (csymbols == NULL && symcount != 0)
2189 return false;
2190 abfd->tdata.mmo_data->csymbols = csymbols;
2192 for (msp = (struct mmo_symbol **) alocation, c = csymbols;
2193 *msp != NULL;
2194 msp++, ++c)
2196 s = *msp;
2197 c->the_bfd = abfd;
2198 c->name = s->name;
2199 c->value = s->value;
2200 c->flags = BSF_GLOBAL;
2202 if (s->sym_type == mmo_data_sym)
2204 c->section
2205 = bfd_get_section_by_name (abfd, MMO_DATA_SECTION_NAME);
2207 if (c->section == NULL)
2208 c->section = bfd_abs_section_ptr;
2209 else
2210 c->value -= c->section->vma;
2212 else if (s->sym_type == mmo_undef_sym)
2213 c->section = bfd_und_section_ptr;
2214 else if (s->sym_type == mmo_reg_sym)
2216 c->section
2217 = bfd_make_section_old_way (abfd, MMIX_REG_SECTION_NAME);
2219 else
2221 asection *textsec
2222 = bfd_get_section_by_name (abfd, MMO_TEXT_SECTION_NAME);
2224 if (textsec != NULL
2225 && c->value >= textsec->vma
2226 && c->value <= textsec->vma + textsec->_cooked_size)
2228 c->section = textsec;
2229 c->value -= c->section->vma;
2231 else
2232 c->section = bfd_abs_section_ptr;
2235 c->udata.p = NULL;
2239 /* Last, overwrite the incoming table with the right-type entries. */
2240 for (i = 0; i < symcount; i++)
2241 *alocation++ = csymbols++;
2242 *alocation = NULL;
2244 return symcount;
2247 /* Get information about a symbol. */
2249 static void
2250 mmo_get_symbol_info (ignore_abfd, symbol, ret)
2251 bfd *ignore_abfd ATTRIBUTE_UNUSED;
2252 asymbol *symbol;
2253 symbol_info *ret;
2255 bfd_symbol_info (symbol, ret);
2258 static void
2259 mmo_print_symbol (abfd, afile, symbol, how)
2260 bfd *abfd;
2261 PTR afile;
2262 asymbol *symbol;
2263 bfd_print_symbol_type how;
2265 FILE *file = (FILE *) afile;
2267 switch (how)
2269 case bfd_print_symbol_name:
2270 fprintf (file, "%s", symbol->name);
2271 break;
2272 default:
2273 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
2275 fprintf (file, " %-5s %s",
2276 symbol->section->name,
2277 symbol->name);
2281 /* We can't map a file directly into executable code, so the
2282 size of header information is irrelevant. */
2284 static int
2285 mmo_sizeof_headers (abfd, exec)
2286 bfd *abfd ATTRIBUTE_UNUSED;
2287 boolean exec ATTRIBUTE_UNUSED;
2289 return 0;
2292 /* Write the (section-neutral) file preamble. */
2294 static boolean
2295 mmo_internal_write_header (abfd)
2296 bfd *abfd;
2298 CONST char lop_pre_bfd[] = { LOP, LOP_PRE, 1, 1};
2300 if (bfd_bwrite (lop_pre_bfd, 4, abfd) != 4)
2301 return false;
2303 /* Copy creation time of original file. */
2304 if (bfd_bwrite (abfd->tdata.mmo_data->created, 4, abfd) != 4)
2305 return false;
2307 return true;
2310 /* Write the LOP_POST record, with global register initializations.
2311 Z is the Z field of the LOP_POST, corresponding to 255 - number of
2312 registers at DATA. The Z = 255 field is filled in with the
2313 start-address. */
2315 static boolean
2316 mmo_internal_write_post (abfd, z, sec)
2317 bfd *abfd;
2318 int z;
2319 asection *sec;
2321 int i;
2322 bfd_byte buf[8];
2323 mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_POST << 16) | z);
2325 for (i = z; i < 255; i++)
2327 bfd_byte *data = mmo_get_loc (sec, i * 8, 8);
2329 if (bfd_bwrite (data, 8, abfd) != 8)
2330 return false;
2333 /* For Z == $255, we always emit the start location; supposedly Main,
2334 but we have it handy at bfd_get_start_address. If we're called with
2335 Z == 255, don't assume DATA is valid. */
2336 bfd_put_64 (abfd, bfd_get_start_address (abfd), buf);
2338 return
2339 abfd->tdata.mmo_data->have_error == false
2340 && bfd_bwrite (buf, 8, abfd) == 8;
2343 /* Translate to and from BFD flags. This is to make sure that we don't
2344 get bitten by BFD flag number changes. */
2346 static flagword
2347 mmo_sec_flags_from_bfd_flags (flags)
2348 flagword flags;
2350 flagword oflags = 0;
2352 if (flags & SEC_ALLOC)
2353 oflags |= MMO_SEC_ALLOC;
2354 if (flags & SEC_LOAD)
2355 oflags |= MMO_SEC_LOAD;
2356 if (flags & SEC_RELOC)
2357 oflags |= MMO_SEC_RELOC;
2358 if (flags & SEC_READONLY)
2359 oflags |= MMO_SEC_READONLY;
2360 if (flags & SEC_CODE)
2361 oflags |= MMO_SEC_CODE;
2362 if (flags & SEC_DATA)
2363 oflags |= MMO_SEC_DATA;
2364 if (flags & SEC_NEVER_LOAD)
2365 oflags |= MMO_SEC_NEVER_LOAD;
2366 if (flags & SEC_IS_COMMON)
2367 oflags |= MMO_SEC_IS_COMMON;
2368 if (flags & SEC_DEBUGGING)
2369 oflags |= MMO_SEC_DEBUGGING;
2371 return oflags;
2374 static flagword
2375 bfd_sec_flags_from_mmo_flags (flags)
2376 flagword flags;
2378 flagword oflags = 0;
2380 if (flags & MMO_SEC_ALLOC)
2381 oflags |= SEC_ALLOC;
2382 if (flags & MMO_SEC_LOAD)
2383 oflags |= SEC_LOAD;
2384 if (flags & MMO_SEC_RELOC)
2385 oflags |= SEC_RELOC;
2386 if (flags & MMO_SEC_READONLY)
2387 oflags |= SEC_READONLY;
2388 if (flags & MMO_SEC_CODE)
2389 oflags |= SEC_CODE;
2390 if (flags & MMO_SEC_DATA)
2391 oflags |= SEC_DATA;
2392 if (flags & MMO_SEC_NEVER_LOAD)
2393 oflags |= SEC_NEVER_LOAD;
2394 if (flags & MMO_SEC_IS_COMMON)
2395 oflags |= SEC_IS_COMMON;
2396 if (flags & MMO_SEC_DEBUGGING)
2397 oflags |= SEC_DEBUGGING;
2399 return oflags;
2402 /* Write a section. */
2404 static boolean
2405 mmo_internal_write_section (abfd, sec)
2406 bfd *abfd;
2407 asection *sec;
2409 /* We do it differently depending on what section this is:
2411 ".text": Output, prepended by information about the first source file
2412 (not yet implemented.)
2414 ".data": Output.
2416 (".MMIX.reg_contents": Not handled here.)
2418 Anything else: Output inside a lop_spec 80, in the format described
2419 above. */
2421 if (strcmp (sec->name, MMO_TEXT_SECTION_NAME) == 0)
2422 /* FIXME: Output source file name and line number. */
2423 return
2424 mmo_write_loc_chunk_list (abfd,
2425 ((struct mmo_section_data_struct *)
2426 (sec->used_by_bfd))->head);
2427 else if (strcmp (sec->name, MMO_DATA_SECTION_NAME) == 0)
2428 return
2429 mmo_write_loc_chunk_list (abfd,
2430 ((struct mmo_section_data_struct *)
2431 (sec->used_by_bfd))->head);
2432 else if (strcmp (sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
2433 /* Not handled here. */
2435 /* This would normally be an abort call since this can't happen, but
2436 we don't do that. */
2437 bfd_set_error (bfd_error_bad_value);
2438 return false;
2440 else if (strncmp (sec->name, MMIX_OTHER_SPEC_SECTION_PREFIX,
2441 strlen (MMIX_OTHER_SPEC_SECTION_PREFIX)) == 0)
2443 int n = atoi (sec->name + strlen (MMIX_OTHER_SPEC_SECTION_PREFIX));
2444 mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_SPEC << 16) | n);
2445 return
2446 abfd->tdata.mmo_data->have_error == false
2447 && mmo_write_chunk_list (abfd,
2448 ((struct mmo_section_data_struct *)
2449 (sec->used_by_bfd))->head);
2451 /* Ignore sections that are just allocated or empty; we write out
2452 _contents_ here. */
2453 else if ((bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS) != 0
2454 && sec->_raw_size != 0)
2456 /* Keep the document-comment formatted the way it is. */
2458 INODE
2459 mmo section mapping, , Symbol-table, mmo
2460 SUBSECTION
2461 mmo section mapping
2463 The implementation in BFD uses special data type 80 (decimal) to
2464 encapsulate and describe named sections, containing e.g.@: debug
2465 information. If needed, any datum in the encapsulation will be
2466 quoted using lop_quote. First comes a 32-bit word holding the
2467 number of 32-bit words containing the zero-terminated zero-padded
2468 segment name. After the name there's a 32-bit word holding flags
2469 describing the section type. Then comes a 64-bit big-endian word
2470 with the section length (in bytes), then another with the section
2471 start address. Depending on the type of section, the contents
2472 might follow, zero-padded to 32-bit boundary. For a loadable
2473 section (such as data or code), the contents might follow at some
2474 later point, not necessarily immediately, as a lop_loc with the
2475 same start address as in the section description, followed by the
2476 contents. This in effect forms a descriptor that must be emitted
2477 before the actual contents. Sections described this way must not
2478 overlap.
2480 For areas that don't have such descriptors, synthetic sections are
2481 formed by BFD. Consecutive contents in the two memory areas
2482 @samp{0x0000@dots{}00} to @samp{0x01ff@dots{}ff} and
2483 @samp{0x2000@dots{}00} to @samp{0x20ff@dots{}ff} are entered in
2484 sections named <<.text>> and <<.data>> respectively. If an area
2485 is not otherwise described, but would together with a neighboring
2486 lower area be less than @samp{0x40000000} bytes long, it is joined
2487 with the lower area and the gap is zero-filled. For other cases,
2488 a new section is formed, named <<.MMIX.sec.@var{n}>>. Here,
2489 @var{n} is a number, a running count through the mmo file,
2490 starting at 0.
2492 EXAMPLE
2493 A loadable section specified as:
2495 | .section secname,"ax"
2496 | TETRA 1,2,3,4,-1,-2009
2497 | BYTE 80
2499 and linked to address @samp{0x4}, is represented by the sequence:
2501 | 0x98080050 - lop_spec 80
2502 | 0x00000002 - two 32-bit words for the section name
2503 | 0x7365636e - "secn"
2504 | 0x616d6500 - "ame\0"
2505 | 0x00000033 - flags CODE, READONLY, LOAD, ALLOC
2506 | 0x00000000 - high 32 bits of section length
2507 | 0x0000001c - section length is 28 bytes; 6 * 4 + 1 + alignment to 32 bits
2508 | 0x00000000 - high 32 bits of section address
2509 | 0x00000004 - section address is 4
2510 | 0x98010002 - 64 bits with address of following data
2511 | 0x00000000 - high 32 bits of address
2512 | 0x00000004 - low 32 bits: data starts at address 4
2513 | 0x00000001 - 1
2514 | 0x00000002 - 2
2515 | 0x00000003 - 3
2516 | 0x00000004 - 4
2517 | 0xffffffff - -1
2518 | 0xfffff827 - -2009
2519 | 0x50000000 - 80 as a byte, padded with zeros.
2521 Note that the lop_spec wrapping does not include the section
2522 contents. Compare this to a non-loaded section specified as:
2524 | .section thirdsec
2525 | TETRA 200001,100002
2526 | BYTE 38,40
2528 This, when linked to address @samp{0x200000000000001c}, is
2529 represented by:
2531 | 0x98080050 - lop_spec 80
2532 | 0x00000002 - two 32-bit words for the section name
2533 | 0x7365636e - "thir"
2534 | 0x616d6500 - "dsec"
2535 | 0x00000010 - flag READONLY
2536 | 0x00000000 - high 32 bits of section length
2537 | 0x0000000c - section length is 12 bytes; 2 * 4 + 2 + alignment to 32 bits
2538 | 0x20000000 - high 32 bits of address
2539 | 0x0000001c - low 32 bits of address 0x200000000000001c
2540 | 0x00030d41 - 200001
2541 | 0x000186a2 - 100002
2542 | 0x26280000 - 38, 40 as bytes, padded with zeros
2544 For the latter example, the section contents must not be
2545 loaded in memory, and is therefore specified as part of the
2546 special data. The address is usually unimportant but might
2547 provide information for e.g.@: the DWARF 2 debugging format. */
2549 mmo_write_tetra_raw (abfd, LOP_SPEC_SECTION);
2550 mmo_write_tetra (abfd, (strlen (sec->name) + 3) / 4);
2551 mmo_write_chunk (abfd, sec->name, strlen (sec->name));
2552 mmo_flush_chunk (abfd);
2553 /* FIXME: We can get debug sections (.debug_line & Co.) with a
2554 section flag still having SEC_RELOC set. Investigate. This
2555 might be true for all alien sections; perhaps mmo.em should clear
2556 that flag. Might be related to weak references. */
2557 mmo_write_tetra (abfd,
2558 mmo_sec_flags_from_bfd_flags
2559 (bfd_get_section_flags (abfd, sec)));
2560 mmo_write_octa (abfd, sec->_raw_size);
2561 mmo_write_octa (abfd, bfd_get_section_vma (abfd, sec));
2563 /* Writing a LOP_LOC ends the LOP_SPEC data, and makes data actually
2564 loaded. */
2565 if (bfd_get_section_flags (abfd, sec) & SEC_LOAD)
2566 return
2567 abfd->tdata.mmo_data->have_error == false
2568 && mmo_write_loc_chunk_list (abfd,
2569 ((struct mmo_section_data_struct *)
2570 (sec->used_by_bfd))->head);
2571 return
2572 abfd->tdata.mmo_data->have_error == false
2573 && mmo_write_chunk_list (abfd,
2574 ((struct mmo_section_data_struct *)
2575 (sec->used_by_bfd))->head);
2577 return true;
2580 /* We save up all data before output. */
2582 static boolean
2583 mmo_set_section_contents (abfd, sec, location, offset, bytes_to_do)
2584 bfd *abfd ATTRIBUTE_UNUSED;
2585 sec_ptr sec;
2586 PTR location;
2587 file_ptr offset;
2588 bfd_size_type bytes_to_do;
2590 /* Iterate over diminishing chunk sizes, copying contents. */
2591 while (bytes_to_do)
2593 /* A minor song-and-dance to make sure we're not bitten by the
2594 distant possibility of the cast from bfd_vma to int making the
2595 chunk zero-sized. */
2596 int chunk_size
2597 = (int) bytes_to_do != 0 ? bytes_to_do : MMO_SEC_CONTENTS_CHUNK_SIZE;
2598 bfd_byte *loc;
2601 loc = mmo_get_loc (sec, sec->vma + offset, chunk_size);
2602 while (loc == NULL && (chunk_size /= 2) != 0);
2604 if (chunk_size == 0)
2605 return false;
2607 memcpy (loc, location, chunk_size);
2609 location += chunk_size;
2610 bytes_to_do -= chunk_size;
2611 offset += chunk_size;
2613 return true;
2616 /* Add a symbol to a trie-tree. */
2618 static boolean
2619 mmo_internal_add_3_sym (abfd, rootp, symp)
2620 bfd *abfd;
2621 struct mmo_symbol_trie *rootp;
2622 CONST struct mmo_symbol *symp;
2624 CONST char *name = symp->name;
2625 struct mmo_symbol_trie *trie = rootp;
2626 struct mmo_symbol_trie **triep = NULL;
2628 while (*name && trie != NULL)
2630 if (*name < trie->symchar)
2632 triep = &trie->left;
2633 trie = trie->left;
2635 else if (*name > trie->symchar)
2637 triep = &trie->right;
2638 trie = trie->right;
2640 else if (*name == trie->symchar)
2642 triep = &trie->middle;
2643 name++;
2645 /* Make sure "trie" points to where we should fill in the
2646 current symbol whenever we've iterated through "name". We
2647 would lose the right position if we encounter "foobar" then
2648 "foo". */
2649 if (*name)
2650 trie = trie->middle;
2654 while (*name != 0)
2656 /* Create middle branches for the rest of the characters. */
2657 trie = bfd_zalloc (abfd, sizeof (struct mmo_symbol_trie));
2658 *triep = trie;
2659 trie->symchar = *name++;
2660 triep = &trie->middle;
2663 /* We discover a duplicate symbol rather late in the process, but still;
2664 we discover it and bail out. */
2665 if (trie->sym.name != NULL)
2667 (*_bfd_error_handler)
2668 (_("%s: invalid symbol table: duplicate symbol `%s'\n"),
2669 bfd_get_filename (abfd), trie->sym.name);
2670 bfd_set_error (bfd_error_bad_value);
2671 return false;
2674 memcpy (&trie->sym, symp, sizeof *symp);
2675 return true;
2678 /* Find out the length of the serialized version of a trie in bytes. */
2680 static unsigned int
2681 mmo_internal_3_length (abfd, trie)
2682 bfd *abfd;
2683 struct mmo_symbol_trie *trie;
2685 /* First, one for the control byte. */
2686 unsigned int length = 1;
2688 if (trie == NULL)
2689 return 0;
2691 /* Add in the recursion to the left. */
2692 length += mmo_internal_3_length (abfd, trie->left);
2694 /* Add in the middle trie and the character. */
2695 length += 1 + mmo_internal_3_length (abfd, trie->middle);
2697 /* Add in the recursion to the right. */
2698 length += mmo_internal_3_length (abfd, trie->right);
2700 /* Add in bytes for the symbol (if this is an endnode). */
2701 if (trie->sym.name != NULL)
2703 unsigned int serno = trie->sym.serno;
2705 /* First what it takes to encode the value. */
2706 if (trie->sym.sym_type == mmo_reg_sym)
2707 length++;
2708 else if (trie->sym.sym_type == mmo_undef_sym)
2709 length += 2;
2710 else
2712 bfd_vma value = trie->sym.value;
2714 /* Coded in one to eight following bytes. */
2715 if (trie->sym.sym_type == mmo_data_sym)
2716 value -= (bfd_vma) 0x20 << 56;
2720 value >>= 8;
2721 length++;
2723 while (value != 0);
2726 /* Find out what it takes to encode the serial number. */
2729 serno >>= 7;
2730 length++;
2732 while (serno != 0);
2735 return length;
2738 /* Helper function for outputting the serial number of a symbol, output as
2739 a variant of leb128 (see dwarf2 documentation) which could be called
2740 beb128. Using a helper function and recursion simplifies debugging. */
2742 static void
2743 mmo_beb128_out (abfd, serno, marker)
2744 bfd *abfd;
2745 int serno;
2746 int marker;
2748 if (serno & ~0x7f)
2749 mmo_beb128_out (abfd, serno >> 7, 0);
2750 mmo_write_byte (abfd, marker | (serno & 0x7f));
2753 /* Serialize a trie. */
2755 static void
2756 mmo_internal_3_dump (abfd, trie)
2757 bfd *abfd;
2758 struct mmo_symbol_trie *trie;
2760 bfd_byte control = 0;
2762 if (trie == NULL)
2763 return;
2765 if (trie->left)
2766 control |= MMO3_LEFT;
2768 if (trie->middle)
2769 control |= MMO3_MIDDLE;
2771 if (trie->right)
2772 control |= MMO3_RIGHT;
2774 if (trie->sym.name != NULL)
2776 /* Encode the symbol type and length of value bytes. */
2777 if (trie->sym.sym_type == mmo_reg_sym)
2778 control |= MMO3_REGQUAL_BITS;
2779 else if (trie->sym.sym_type == mmo_undef_sym)
2780 control |= MMO3_UNDEF;
2781 else
2783 bfd_vma value = trie->sym.value;
2785 /* Coded in 1..8 following bytes. */
2786 if (trie->sym.sym_type == mmo_data_sym)
2788 control |= MMO3_DATA;
2789 value -= (bfd_vma) 0x20 << 56;
2794 value >>= 8;
2795 control++;
2797 while (value != 0);
2801 /* The control byte is output before recursing. */
2802 mmo_write_byte (abfd, control);
2804 mmo_internal_3_dump (abfd, trie->left);
2806 if (control & MMO3_SYMBITS)
2808 mmo_write_byte (abfd, trie->symchar);
2810 if (trie->sym.name != NULL)
2812 if (trie->sym.sym_type == mmo_reg_sym)
2813 mmo_write_byte (abfd, trie->sym.value);
2814 else if (trie->sym.sym_type == mmo_undef_sym)
2816 mmo_write_byte (abfd, 0);
2817 mmo_write_byte (abfd, 0);
2819 else
2821 bfd_vma value = trie->sym.value;
2823 bfd_byte byte_n = control & 15;
2825 /* Coded in 1..8 following bytes. Note that the value is
2826 shifted out big-endian. */
2827 if (trie->sym.sym_type == mmo_data_sym)
2829 value -= (bfd_vma) 0x20 << 56;
2830 byte_n -= 8;
2835 mmo_write_byte (abfd, (value >> ((byte_n - 1) * 8)) & 0xff);
2836 byte_n--;
2838 while (byte_n != 0);
2841 mmo_beb128_out (abfd, trie->sym.serno, 128);
2843 mmo_internal_3_dump (abfd, trie->middle);
2845 mmo_internal_3_dump (abfd, trie->right);
2848 /* Write symbols in mmo format. Also write the lop_end terminator. */
2850 static boolean
2851 mmo_write_symbols_and_terminator (abfd)
2852 bfd *abfd;
2854 int count = bfd_get_symcount (abfd);
2855 asymbol *maintable[2];
2856 asymbol **table;
2857 asymbol **orig_table = bfd_get_outsymbols (abfd);
2858 int serno;
2859 struct mmo_symbol_trie root;
2860 int trie_len;
2861 int i;
2862 bfd_byte buf[4];
2864 /* Create a symbol for "Main". */
2865 asymbol *fakemain = bfd_make_empty_symbol (abfd);
2867 fakemain->flags = BSF_GLOBAL;
2868 fakemain->value = bfd_get_start_address (abfd);
2869 fakemain->name = MMIX_START_SYMBOL_NAME;
2870 fakemain->section = bfd_abs_section_ptr;
2871 maintable[0] = fakemain;
2872 maintable[1] = NULL;
2874 memset (&root, 0, sizeof (root));
2876 /* Make all symbols take a left turn. */
2877 root.symchar = 0xff;
2879 /* There must always be a ":Main", so we'll add one if there are no
2880 symbols. Make sure we have room for it. */
2881 table = bfd_alloc (abfd, (count + 1) * sizeof (asymbol *));
2882 if (table == NULL)
2883 return false;
2885 memcpy (table, orig_table, count * sizeof (asymbol *));
2887 /* Move :Main (if there is one) to the first position. This is
2888 necessary to get the same layout of the trie-tree when linking as
2889 when objcopying the result as in the objcopy.exp test "simple objcopy
2890 of executable". It also automatically takes care of assigning serial
2891 number 1 to :Main (as is mandatory). */
2892 for (i = 0; i < count; i++)
2893 if (table[i] != NULL
2894 && strcmp (table[i]->name, MMIX_START_SYMBOL_NAME) == 0
2895 && (table[i]->flags & (BSF_DEBUGGING|BSF_GLOBAL)) == BSF_GLOBAL)
2897 asymbol *mainsym = table[i];
2898 memcpy (table + 1, orig_table, i * sizeof (asymbol *));
2899 table[0] = mainsym;
2901 /* Check that the value assigned to :Main is the same as the entry
2902 address. The default linker script asserts this. This is as
2903 good a place as any to check this consistency. */
2904 if ((mainsym->value
2905 + mainsym->section->output_section->vma
2906 + mainsym->section->output_offset)
2907 != bfd_get_start_address (abfd))
2909 /* Arbitrary buffer to hold the printable representation of a
2910 vma. */
2911 char vmas_main[40];
2912 char vmas_start[40];
2913 bfd_vma vma_start = bfd_get_start_address (abfd);
2915 sprintf_vma (vmas_main, mainsym->value);
2916 sprintf_vma (vmas_start, vma_start);
2918 (*_bfd_error_handler)
2919 (_("%s: Bad symbol definition: `Main' set to %s rather\
2920 than the start address %s\n"),
2921 bfd_get_filename (abfd), vmas_main, vmas_start);
2922 bfd_set_error (bfd_error_bad_value);
2923 return false;
2925 break;
2927 if (i == count && count != 0)
2929 /* When there are symbols, there must be a :Main. There was no
2930 :Main, so we need to add it manually. */
2931 memcpy (table + 1, orig_table, count * sizeof (asymbol *));
2932 table[0] = fakemain;
2933 count++;
2936 for (i = 0, serno = 1; i < count && table[i] != NULL; i++)
2938 asymbol *s = table[i];
2940 /* It's not enough to consult bfd_is_local_label, since it does not
2941 mean "local" in the sense of linkable-and-observable-after-link.
2942 Let's just check the BSF_GLOBAL flag.
2944 Also, don't export symbols with characters not in the allowed set. */
2945 if ((s->flags & (BSF_DEBUGGING|BSF_GLOBAL)) == BSF_GLOBAL
2946 && strspn (s->name,
2947 valid_mmo_symbol_character_set) == strlen (s->name))
2949 struct mmo_symbol sym;
2950 memset (&sym, 0, sizeof (sym));
2952 sym.name = s->name;
2953 sym.value =
2954 s->value
2955 + s->section->output_section->vma
2956 + s->section->output_offset;
2958 if (bfd_is_und_section (s->section))
2959 sym.sym_type = mmo_undef_sym;
2960 else if (strcmp (s->section->name, MMO_DATA_SECTION_NAME) == 0
2961 /* The encoding of data symbols require that the "rest"
2962 of the value fits in 6 bytes, so the upper two bytes
2963 must be 0x2000. All other symbols get to be the
2964 absolute type. */
2965 && (sym.value >> 48) == 0x2000)
2966 sym.sym_type = mmo_data_sym;
2967 else if (strcmp (s->section->name, MMIX_REG_SECTION_NAME) == 0)
2968 sym.sym_type = mmo_reg_sym;
2969 else if (strcmp (s->section->name,
2970 MMIX_REG_CONTENTS_SECTION_NAME) == 0)
2972 sym.sym_type = mmo_reg_sym;
2973 sym.value /= 8;
2975 else
2976 sym.sym_type = mmo_abs_sym;
2978 /* FIXME: We assume the order of the received symbols is an
2979 ordered mapping of the serial numbers. This is not
2980 necessarily true if we e.g. objcopy a mmo file to another and
2981 there are gaps in the numbering. Not sure if this can
2982 happen. Not sure what to do. */
2983 sym.serno = serno++;
2985 if (! mmo_internal_add_3_sym (abfd, &root, &sym))
2986 return false;
2990 /* Change the root node to be a ":"-prefix. */
2991 root.symchar = ':';
2992 root.middle = root.left;
2993 root.right = NULL;
2994 root.left = NULL;
2996 /* We have to find out if we can fit the whole symbol table in the mmo
2997 symtab. It would be bad to assume we can always fit it in 262144
2998 bytes. If we can't, just leave the Main symbol. */
2999 trie_len = (mmo_internal_3_length (abfd, &root) + 3)/4;
3001 if (trie_len > 0xffff)
3003 /* Test this code by using a lower limit in the test above and check
3004 that the single "Main" symbol is emitted and handled properly.
3005 There's no specific test-case. */
3006 struct mmo_symbol sym;
3008 (*_bfd_error_handler)
3009 (_("%s: warning: symbol table too large for mmo, larger than 65535\
3010 32-bit words: %d. Only `Main' will be emitted.\n"),
3011 bfd_get_filename (abfd), trie_len);
3013 memset (&sym, 0, sizeof (sym));
3014 sym.sym_type = mmo_abs_sym;
3015 sym.name = MMIX_START_SYMBOL_NAME;
3016 sym.serno = 1;
3017 sym.value = bfd_get_start_address (abfd);
3019 /* Then patch up a symbol table to be just the ":Main" symbol. */
3020 memset (&root, 0, sizeof (root));
3021 root.left = root.middle;
3022 root.symchar = 0xff;
3023 root.middle = NULL;
3024 root.right = NULL;
3026 if (! mmo_internal_add_3_sym (abfd, &root, &sym))
3027 return false;
3029 root.symchar = ':';
3030 root.middle = root.left;
3031 root.right = NULL;
3032 root.left = NULL;
3034 trie_len = (mmo_internal_3_length (abfd, &root) + 3)/4;
3037 /* Reset the written-bytes counter. */
3038 abfd->tdata.mmo_data->byte_no = 0;
3040 /* Put out the lop_stab mark. */
3041 bfd_put_32 (abfd, (LOP << 24) | (LOP_STAB << 16), buf);
3042 if (bfd_bwrite (buf, 4, abfd) != 4)
3043 return false;
3045 /* Dump out symbols. */
3046 mmo_internal_3_dump (abfd, &root);
3048 if (trie_len != (abfd->tdata.mmo_data->byte_no + 3)/4)
3050 /* I haven't seen this trig. It seems no use claiming this case
3051 isn't debugged and abort if we get here. Instead emit a
3052 diagnostic and fail "normally". */
3053 (*_bfd_error_handler)
3054 (_("%s: internal error, symbol table changed size from %d to %d\
3055 words\n"),
3056 bfd_get_filename (abfd), trie_len,
3057 (abfd->tdata.mmo_data->byte_no + 3)/4);
3058 bfd_set_error (bfd_error_bad_value);
3059 return false;
3062 /* Dump out remaining bytes in the buffer and handle I/O errors by
3063 propagating errors. */
3064 if ((abfd->tdata.mmo_data->byte_no % 4) != 0
3065 || abfd->tdata.mmo_data->have_error)
3067 memset (abfd->tdata.mmo_data->buf + (abfd->tdata.mmo_data->byte_no % 4),
3068 0, 4 - (abfd->tdata.mmo_data->byte_no % 4));
3070 if (abfd->tdata.mmo_data->have_error
3071 || bfd_bwrite (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
3072 return false;
3075 bfd_put_32 (abfd, (LOP << 24) | (LOP_END << 16) | trie_len, buf);
3076 return bfd_bwrite (buf, 4, abfd) == 4;
3079 /* Write section unless it is the register contents section. For that, we
3080 instead store the section in the supplied pointer. This function is
3081 used through bfd_map_over_sections. */
3083 static void
3084 mmo_write_section_unless_reg_contents (abfd, sec, p)
3085 bfd *abfd;
3086 asection *sec;
3087 PTR p;
3089 struct mmo_write_sec_info *infop = (struct mmo_write_sec_info *) p;
3091 if (infop->retval == false)
3092 return;
3094 if (strcmp (sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
3096 infop->reg_section = sec;
3097 return;
3100 /* Exclude the convenience register section. */
3101 if (strcmp (sec->name, MMIX_REG_SECTION_NAME) == 0)
3103 if (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
3105 /* Make sure it hasn't got contents. It seems impossible to
3106 make it carry contents, so we don't have a test-case for
3107 this. */
3108 (*_bfd_error_handler)
3109 (_("%s: internal error, internal register section %s had\
3110 contents\n"),
3111 bfd_get_filename (abfd), sec->name);
3112 bfd_set_error (bfd_error_bad_value);
3113 infop->retval = false;
3114 return;
3117 return;
3120 infop->retval = mmo_internal_write_section (abfd, sec);
3123 /* Do the actual output of a file. Assumes mmo_set_section_contents is
3124 already called. */
3126 static boolean
3127 mmo_write_object_contents (abfd)
3128 bfd *abfd;
3130 struct mmo_write_sec_info wsecinfo;
3132 /* First, there are a few words of preamble. */
3133 if (! mmo_internal_write_header (abfd))
3134 return false;
3136 wsecinfo.reg_section = NULL;
3137 wsecinfo.retval = true;
3139 bfd_map_over_sections (abfd, mmo_write_section_unless_reg_contents,
3140 (PTR) &wsecinfo);
3142 if (wsecinfo.retval == false)
3143 return false;
3145 if (wsecinfo.reg_section != NULL)
3147 asection *sec = wsecinfo.reg_section;
3148 unsigned int z = (unsigned int) (sec->vma / 8);
3150 /* Registers 0..31 must not be global. Do sanity check on the "vma"
3151 of the register contents section and check that it corresponds to
3152 the length of the section. */
3153 if (z < 32 || z >= 255 || (sec->vma & 7) != 0
3154 || sec->vma != 256 * 8 - sec->_raw_size - 8)
3156 bfd_set_error (bfd_error_bad_value);
3158 if (sec->_raw_size == 0)
3159 /* There must always be at least one such register. */
3160 (*_bfd_error_handler)
3161 (_("%s: no initialized registers; section length 0\n"),
3162 bfd_get_filename (abfd));
3163 else if (sec->vma > (256 - 32) * 8)
3164 /* Provide better error message for the case of too many
3165 global registers. */
3166 (*_bfd_error_handler)
3167 (_("%s: too many initialized registers; section length %ld\n"),
3168 bfd_get_filename (abfd),
3169 (long) sec->_raw_size);
3170 else
3171 (*_bfd_error_handler)
3172 (_("%s: invalid start address for initialized registers of\
3173 length %ld: 0x%lx%08lx\n"),
3174 bfd_get_filename (abfd),
3175 (long) sec->_raw_size,
3176 (unsigned long) (sec->vma >> 32), (unsigned long) (sec->vma));
3178 return false;
3181 if (! mmo_internal_write_post (abfd, z, sec))
3182 return false;
3184 else
3185 if (! mmo_internal_write_post (abfd, 255, NULL))
3186 return false;
3188 return mmo_write_symbols_and_terminator (abfd);
3191 /* Return the size of a NULL pointer, so we support linking in an mmo
3192 object. */
3194 static long
3195 mmo_get_reloc_upper_bound (abfd, sec)
3196 bfd *abfd ATTRIBUTE_UNUSED;
3197 asection *sec ATTRIBUTE_UNUSED;
3199 return sizeof (PTR);
3202 /* Similarly canonicalize relocs to empty, filling in the terminating NULL
3203 pointer. */
3205 long
3206 mmo_canonicalize_reloc (abfd, section, relptr, symbols)
3207 bfd *abfd ATTRIBUTE_UNUSED;
3208 sec_ptr section ATTRIBUTE_UNUSED;
3209 arelent **relptr;
3210 asymbol **symbols ATTRIBUTE_UNUSED;
3212 *relptr = NULL;
3213 return 0;
3216 /* If there's anything in particular in a mmo bfd that we want to free,
3217 make this a real function. Only do this if you see major memory
3218 thrashing; zealous free:ing will cause unwanted behavior, especially if
3219 you "free" memory allocated with "bfd_alloc", or even "bfd_release" a
3220 block allocated with "bfd_alloc"; they're really allocated from an
3221 obstack, and we don't know what was allocated there since this
3222 particular allocation. */
3224 #define mmo_close_and_cleanup _bfd_generic_close_and_cleanup
3225 #define mmo_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
3227 /* Perhaps we need to adjust this one; mmo labels (originally) without a
3228 leading ':' might more appropriately be called local. */
3229 #define mmo_bfd_is_local_label_name bfd_generic_is_local_label_name
3231 /* Is this one really used or defined by anyone? */
3232 #define mmo_get_lineno _bfd_nosymbols_get_lineno
3234 /* FIXME: We can do better on this one, if we have a dwarf2 .debug_line
3235 section or if MMO line numbers are implemented. */
3236 #define mmo_find_nearest_line _bfd_nosymbols_find_nearest_line
3237 #define mmo_make_empty_symbol _bfd_generic_make_empty_symbol
3238 #define mmo_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
3239 #define mmo_read_minisymbols _bfd_generic_read_minisymbols
3240 #define mmo_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
3242 #define mmo_get_section_contents_in_window \
3243 _bfd_generic_get_section_contents_in_window
3244 #define mmo_bfd_get_relocated_section_contents \
3245 bfd_generic_get_relocated_section_contents
3246 #define mmo_bfd_gc_sections bfd_generic_gc_sections
3247 #define mmo_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
3248 #define mmo_bfd_link_add_symbols _bfd_generic_link_add_symbols
3249 #define mmo_bfd_final_link _bfd_generic_final_link
3250 #define mmo_bfd_link_split_section _bfd_generic_link_split_section
3252 /* Strictly speaking, only MMIX uses this restricted format, but let's not
3253 stop anybody from shooting themselves in the foot. */
3254 #define mmo_set_arch_mach bfd_default_set_arch_mach
3255 #define mmo_bfd_relax_section bfd_generic_relax_section
3256 #define mmo_bfd_merge_sections bfd_generic_merge_sections
3258 /* objcopy will be upset if we return -1 from bfd_get_reloc_upper_bound by
3259 using BFD_JUMP_TABLE_RELOCS (_bfd_norelocs) rather than 0. FIXME: Most
3260 likely a bug in the _bfd_norelocs definition.
3262 On the other hand, we smuggle in an mmo object (because setting up ELF
3263 is too cumbersome) when linking (from other formats, presumably ELF) to
3264 represent the g255 entry. We need to link that object, so need to say
3265 it has no relocs. Upper bound for the size of the relocation table is
3266 the size of a NULL pointer, and we support "canonicalization" for that
3267 pointer. */
3268 #define mmo_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
3270 /* We want to copy time of creation, otherwise we'd use
3271 BFD_JUMP_TABLE_COPY (_bfd_generic). */
3272 #define mmo_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
3273 #define mmo_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
3274 #define mmo_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
3275 #define mmo_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
3276 #define mmo_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
3278 CONST bfd_target bfd_mmo_vec =
3280 "mmo", /* name */
3281 bfd_target_mmo_flavour,
3282 BFD_ENDIAN_BIG, /* target byte order */
3283 BFD_ENDIAN_BIG, /* target headers byte order */
3285 /* FIXME: Might need adjustments. */
3286 (HAS_RELOC | EXEC_P | /* object flags */
3287 HAS_LINENO | HAS_DEBUG |
3288 HAS_SYMS | HAS_LOCALS | WP_TEXT),
3290 /* FIXME: Might need adjustments. */
3291 (SEC_CODE | SEC_DATA | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
3292 | SEC_READONLY | SEC_EXCLUDE | SEC_DEBUGGING | SEC_IN_MEMORY),
3293 /* section flags */
3294 0, /* leading underscore */
3295 ' ', /* ar_pad_char */
3296 16, /* ar_max_namelen */
3297 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3298 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3299 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
3300 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3301 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3302 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
3305 _bfd_dummy_target,
3306 mmo_object_p, /* bfd_check_format */
3307 _bfd_dummy_target,
3308 _bfd_dummy_target,
3311 bfd_false,
3312 mmo_mkobject,
3313 bfd_false,
3314 bfd_false,
3316 { /* bfd_write_contents */
3317 bfd_false,
3318 mmo_write_object_contents,
3319 bfd_false,
3320 bfd_false,
3323 BFD_JUMP_TABLE_GENERIC (mmo),
3324 BFD_JUMP_TABLE_COPY (mmo),
3325 BFD_JUMP_TABLE_CORE (_bfd_nocore),
3326 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
3327 BFD_JUMP_TABLE_SYMBOLS (mmo),
3328 /* We have to provide a valid method for getting relocs, returning zero,
3329 so we can't say BFD_JUMP_TABLE_RELOCS (_bfd_norelocs). */
3330 BFD_JUMP_TABLE_RELOCS (mmo),
3331 BFD_JUMP_TABLE_WRITE (mmo),
3332 BFD_JUMP_TABLE_LINK (mmo),
3333 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
3335 NULL,
3337 NULL