Reset AIX vector function overrides for HP-UX.
[binutils.git] / bfd / mmo.c
blob019160c3386d8d44c580b61b74ea1c10b54b343d
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"
204 #include <ctype.h>
206 #define LOP 0x98
207 #define LOP_QUOTE 0
208 #define LOP_LOC 1
209 #define LOP_SKIP 2
210 #define LOP_FIXO 3
211 #define LOP_FIXR 4
212 #define LOP_FIXRX 5
213 #define LOP_FILE 6
214 #define LOP_LINE 7
215 #define LOP_SPEC 8
216 #define LOP_PRE 9
217 #define LOP_POST 10
218 #define LOP_STAB 11
219 #define LOP_END 12
221 #define LOP_QUOTE_NEXT ((LOP << 24) | (LOP_QUOTE << 16) | 1)
222 #define SPEC_DATA_SECTION 80
223 #define LOP_SPEC_SECTION \
224 ((LOP << 24) | (LOP_SPEC << 16) | SPEC_DATA_SECTION)
226 /* Must be a power of two. If you change this to be >= 64k, you need a
227 new test-case; the ld test b-loc64k.d touches chunk-size problem areas. */
228 #define MMO_SEC_CONTENTS_CHUNK_SIZE (1 << 15)
230 /* An arbitrary number for the maximum length section name size. */
231 #define MAX_SECTION_NAME_SIZE (1024 * 1024)
233 /* A quite arbitrary number for the maximum length section size. */
234 #define MAX_ARTIFICIAL_SECTION_SIZE (1024 * 1024 * 1024)
236 #define MMO3_WCHAR 0x80
237 #define MMO3_LEFT 0x40
238 #define MMO3_MIDDLE 0x20
239 #define MMO3_RIGHT 0x10
240 #define MMO3_TYPEBITS 0xf
241 #define MMO3_REGQUAL_BITS 0xf
242 #define MMO3_UNDEF 2
243 #define MMO3_DATA 8
244 #define MMO3_SYMBITS 0x2f
246 /* Put these everywhere in new code. */
247 #define FATAL_DEBUG \
248 _bfd_abort (__FILE__, __LINE__, \
249 "Internal: Non-debugged code (test-case missing)")
251 #define BAD_CASE(x) \
252 _bfd_abort (__FILE__, __LINE__, \
253 "bad case for " #x)
255 enum mmo_sym_type { mmo_reg_sym, mmo_undef_sym, mmo_data_sym, mmo_abs_sym};
257 /* When scanning the mmo file, a linked list of mmo_symbol
258 structures is built to represent the symbol table (if there is
259 one). */
261 struct mmo_symbol
263 struct mmo_symbol *next;
264 CONST char *name;
265 bfd_vma value;
266 enum mmo_sym_type sym_type;
267 unsigned int serno;
270 struct mmo_data_list_struct
272 struct mmo_data_list_struct *next;
273 bfd_vma where;
274 bfd_size_type size;
275 bfd_size_type allocated_size;
276 bfd_byte data[1];
279 typedef struct mmo_data_list_struct mmo_data_list_type;
281 struct mmo_symbol_trie
283 struct mmo_symbol_trie *left;
284 struct mmo_symbol_trie *right;
285 struct mmo_symbol_trie *middle;
287 bfd_byte symchar;
289 /* A zero name means there's nothing here. */
290 struct mmo_symbol sym;
293 /* The mmo tdata information. */
295 struct mmo_data_struct
297 struct mmo_symbol *symbols;
298 struct mmo_symbol *symtail;
299 asymbol *csymbols;
301 /* File representation of time (NULL) when this file was created. */
302 bfd_byte created[4];
304 /* When we're reading bytes recursively, check this occasionally.
305 Also holds write errors. */
306 boolean have_error;
308 /* Max symbol length that may appear in the lop_stab table. Note that
309 this table might just hold a subset of symbols for not-really large
310 programs, as it can only be 65536 * 4 bytes large. */
311 int max_symbol_length;
313 /* Here's the symbol we build in lop_stab. */
314 char *lop_stab_symbol;
316 /* Index into lop_stab_symbol for the next character when parsing the
317 symbol information. */
318 int symbol_position;
320 /* When creating arbitrary sections, we need to count section numbers. */
321 int sec_no;
323 /* When writing or reading byte-wise, we need to count the bytes
324 within a 32-bit word. */
325 int byte_no;
327 /* We also need a buffer to hold the bytes we count reading or writing. */
328 bfd_byte buf[4];
331 typedef struct mmo_data_struct tdata_type;
333 struct mmo_section_data_struct
335 mmo_data_list_type *head;
336 mmo_data_list_type *tail;
339 /* These structures are used in bfd_map_over_sections constructs. */
341 /* Used when writing out sections; all but the register contents section
342 which is stored in reg_section. */
343 struct mmo_write_sec_info
345 asection *reg_section;
346 boolean retval;
349 /* Used when trying to find a section corresponding to addr. */
350 struct mmo_find_sec_info
352 asection *sec;
353 bfd_vma addr;
356 static boolean mmo_bfd_copy_private_bfd_data PARAMS ((bfd *, bfd *));
357 static void mmo_write_section_unless_reg_contents
358 PARAMS ((bfd *, asection *, PTR));
359 static void mmo_find_sec_w_addr PARAMS ((bfd *, asection *, PTR));
360 static void mmo_find_sec_w_addr_grow PARAMS ((bfd *, asection *, PTR));
361 static asection *mmo_make_section PARAMS ((bfd *, CONST char *));
362 static void mmo_get_symbol_info PARAMS ((bfd *, asymbol *, symbol_info *));
363 static void mmo_print_symbol
364 PARAMS ((bfd *, PTR, asymbol *, bfd_print_symbol_type));
365 static void mmo_init PARAMS ((void));
366 static boolean mmo_mkobject PARAMS ((bfd *));
367 static boolean mmo_scan PARAMS ((bfd *));
368 static asection *mmo_decide_section PARAMS ((bfd *, bfd_vma));
369 static asection *mmo_get_generic_spec_data_section PARAMS ((bfd *, int));
370 static asection *mmo_get_spec_section PARAMS ((bfd *, int));
371 static INLINE bfd_byte *mmo_get_loc PARAMS ((asection *, bfd_vma, int));
372 static void mmo_xore_64 PARAMS ((asection *, bfd_vma vma, bfd_vma value));
373 static void mmo_xore_32 PARAMS ((asection *, bfd_vma vma, unsigned int));
374 static void mmo_xore_16 PARAMS ((asection *, bfd_vma vma, unsigned int));
375 static CONST bfd_target *mmo_object_p PARAMS ((bfd *));
376 static void mmo_map_set_sizes PARAMS ((bfd *, asection *, PTR));
377 static boolean mmo_get_symbols PARAMS ((bfd *));
378 static boolean mmo_create_symbol PARAMS ((bfd *, CONST char *, bfd_vma,
379 enum mmo_sym_type, unsigned int));
380 static boolean mmo_get_section_contents
381 PARAMS ((bfd *, asection *, PTR, file_ptr, bfd_size_type));
382 static long mmo_get_symtab_upper_bound PARAMS ((bfd *));
383 static long mmo_get_symtab PARAMS ((bfd *, asymbol **));
384 static void mmo_get_symbol_info PARAMS ((bfd *, asymbol *, symbol_info *));
385 static void mmo_print_symbol PARAMS ((bfd *, PTR, asymbol *,
386 bfd_print_symbol_type));
387 static boolean mmo_set_section_contents
388 PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type));
389 static int mmo_sizeof_headers PARAMS ((bfd *, boolean));
390 static long mmo_get_reloc_upper_bound PARAMS ((bfd *, asection *));
392 static boolean mmo_internal_write_header PARAMS ((bfd *));
393 static boolean mmo_internal_write_post PARAMS ((bfd *, int, asection *));
394 static boolean mmo_internal_add_3_sym
395 PARAMS ((bfd *, struct mmo_symbol_trie *, CONST struct mmo_symbol *));
396 static unsigned int mmo_internal_3_length
397 PARAMS ((bfd *, struct mmo_symbol_trie *));
398 static void mmo_internal_3_dump
399 PARAMS ((bfd *, struct mmo_symbol_trie *));
400 static void mmo_beb128_out PARAMS ((bfd *, int, int));
401 static boolean mmo_internal_write_section
402 PARAMS ((bfd *, asection *));
403 static void mmo_write_tetra PARAMS ((bfd *, unsigned int));
404 static void mmo_write_tetra_raw PARAMS ((bfd *, unsigned int));
405 static void mmo_write_octa PARAMS ((bfd *, bfd_vma));
406 static void mmo_write_octa_raw PARAMS ((bfd *, bfd_vma));
407 static boolean mmo_write_chunk
408 PARAMS ((bfd *, CONST bfd_byte *, unsigned int));
409 static boolean mmo_write_loc_chunk
410 PARAMS ((bfd *, bfd_vma, CONST bfd_byte *, unsigned int));
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 == false)
486 inited = true;
489 /* Fill in the set of valid symbol characters. */
490 strcpy (valid_mmo_symbol_character_set, letters);
491 i = strlen (letters);
493 for (j = 126; j < 256; j++)
494 valid_mmo_symbol_character_set[i++] = j;
497 /* Check whether an existing file is an mmo file. */
499 static CONST bfd_target *
500 mmo_object_p (abfd)
501 bfd *abfd;
503 struct stat statbuf;
504 bfd_byte b[4];
506 mmo_init ();
508 if (bfd_stat (abfd, &statbuf) < 0
509 || bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
510 || bfd_bread (b, 4, abfd) != 4)
511 goto bad_final;
513 /* All mmo files are a multiple of four bytes long.
514 Only recognize version one. */
515 if ((statbuf.st_size % 4) != 0
516 || b[0] != LOP || b[1] != LOP_PRE || b[2] != 1)
517 goto bad_format;
519 /* Get the last 32-bit word. */
520 if (bfd_seek (abfd, (file_ptr) statbuf.st_size - 4, SEEK_SET) != 0
521 || bfd_bread (b, 4, abfd) != 4)
522 goto bad_final;
524 /* Check if the file ends in a lop_end lopcode. */
525 if (b[0] != LOP || b[1] != LOP_END || ! mmo_mkobject (abfd))
526 goto bad_format;
528 /* Compute an upper bound on the max symbol length. Not really
529 important as all of the symbol information can only be 256k. */
530 abfd->tdata.mmo_data->max_symbol_length = (b[2] * 256 + b[3]) * 4;
531 abfd->tdata.mmo_data->lop_stab_symbol
532 = bfd_malloc (abfd->tdata.mmo_data->max_symbol_length + 1);
534 if (abfd->tdata.mmo_data->lop_stab_symbol == NULL)
536 (*_bfd_error_handler)
537 (_("%s: No core to allocate a symbol %d bytes long\n"),
538 bfd_get_filename (abfd), abfd->tdata.mmo_data->max_symbol_length);
539 goto bad_final;
542 /* Read in everything. */
543 if (! mmo_scan (abfd))
544 goto bad_format_free;
546 if (abfd->symcount > 0)
547 abfd->flags |= HAS_SYMS;
549 /* You'll have to tweak this if you want to use this format for other
550 arches (not recommended due to its small-size limitations). Look at
551 the ELF format for how to make it target-generic. */
552 if (! bfd_default_set_arch_mach (abfd, bfd_arch_mmix, 0))
553 goto bad_format_free;
555 return abfd->xvec;
557 bad_format_free:
558 free (abfd->tdata.mmo_data->lop_stab_symbol);
559 bad_format:
560 bfd_set_error (bfd_error_wrong_format);
561 bad_final:
562 return NULL;
565 /* Set up the mmo tdata information. */
567 static boolean
568 mmo_mkobject (abfd)
569 bfd *abfd;
571 mmo_init ();
573 if (abfd->tdata.mmo_data == NULL)
575 time_t created;
577 /* All fields are zero-initialized, so we don't have to explicitly
578 initialize most. */
579 tdata_type *tdata = (tdata_type *) bfd_zmalloc (sizeof (tdata_type));
580 if (tdata == NULL)
581 return false;
583 created = time (NULL);
584 bfd_put_32 (abfd, created, tdata->created);
586 abfd->tdata.mmo_data = tdata;
589 return true;
592 static boolean
593 mmo_bfd_copy_private_bfd_data (ibfd, obfd)
594 bfd *ibfd;
595 bfd *obfd;
597 if (bfd_get_flavour (ibfd) != bfd_target_mmo_flavour
598 || bfd_get_flavour (obfd) != bfd_target_mmo_flavour)
599 return true;
601 /* Copy the time the copied-from file was created. If people want the
602 time the file was last *modified*, they have that in the normal file
603 information. */
604 memcpy (obfd->tdata.mmo_data->created, ibfd->tdata.mmo_data->created,
605 sizeof (obfd->tdata.mmo_data->created));
606 return true;
609 /* Helper functions for mmo_decide_section, used through
610 bfd_map_over_sections. */
612 static void
613 mmo_find_sec_w_addr (abfd, sec, p)
614 bfd *abfd ATTRIBUTE_UNUSED;
615 asection *sec;
616 PTR p;
618 struct mmo_find_sec_info *infop = (struct mmo_find_sec_info *) p;
619 bfd_vma vma = bfd_get_section_vma (abfd, sec);
621 /* Ignore sections that aren't loaded. */
622 if ((bfd_get_section_flags (abfd, sec) & (SEC_LOAD | SEC_ALLOC))
623 != (SEC_LOAD | SEC_ALLOC))
624 return;
626 if (infop->addr >= vma && infop->addr < vma + sec->_raw_size)
627 infop->sec = sec;
630 static void
631 mmo_find_sec_w_addr_grow (abfd, sec, p)
632 bfd *abfd ATTRIBUTE_UNUSED;
633 asection *sec;
634 PTR p;
636 struct mmo_find_sec_info *infop = (struct mmo_find_sec_info *) p;
637 bfd_vma vma = bfd_get_section_vma (abfd, sec);
639 /* Ignore sections that aren't loaded. */
640 if ((bfd_get_section_flags (abfd, sec) & (SEC_LOAD | SEC_ALLOC))
641 != (SEC_LOAD | SEC_ALLOC))
642 return;
644 if (infop->addr >= vma && infop->addr < vma + MAX_ARTIFICIAL_SECTION_SIZE)
645 infop->sec = sec;
648 /* Find a section that corresponds to a VMA. Automatically create .text
649 or .data and set current section to it, depending on what vma. If we
650 can't deduce a section, make one up as ".MMIX.sec.N", where N is an
651 increasing number. */
653 static asection *
654 mmo_decide_section (abfd, vma)
655 bfd *abfd;
656 bfd_vma vma;
658 asection *sec = NULL;
659 char sec_name[sizeof (".MMIX.sec.") + 20];
660 struct mmo_find_sec_info info;
662 info.addr = vma;
663 info.sec = NULL;
665 /* First see if there's a section that would match exactly. */
666 bfd_map_over_sections (abfd, mmo_find_sec_w_addr, &info);
668 if (info.sec != NULL)
669 return info.sec;
671 /* If there's no such section, try and expand one of the existing ones,
672 up to a limit. Make sure we have .text and .data before we try that;
673 create them corresponding to expected addresses and set flags to make
674 them match the "loaded and with contents" expectation. */
675 if ((vma >> 56) == 0)
677 sec = bfd_make_section_old_way (abfd, MMO_TEXT_SECTION_NAME);
679 if (sec == NULL)
680 return NULL;
682 if (! sec->user_set_vma)
683 bfd_set_section_vma (abfd, sec, vma);
684 if (! bfd_set_section_flags (abfd, sec,
685 bfd_get_section_flags (abfd, sec)
686 | SEC_CODE | SEC_LOAD | SEC_ALLOC))
687 return NULL;
689 else if ((vma >> 56) == 0x20)
691 sec = bfd_make_section_old_way (abfd, MMO_DATA_SECTION_NAME);
693 if (sec == NULL)
694 return NULL;
696 if (! sec->user_set_vma)
697 bfd_set_section_vma (abfd, sec, vma);
698 if (! bfd_set_section_flags (abfd, sec,
699 bfd_get_section_flags (abfd, sec)
700 | SEC_LOAD | SEC_ALLOC))
701 return NULL;
704 bfd_map_over_sections (abfd, mmo_find_sec_w_addr_grow, &info);
706 if (info.sec != NULL)
707 return info.sec;
709 /* If there's still no suitable section, make a new one. */
710 sprintf (sec_name, ".MMIX.sec.%d", abfd->tdata.mmo_data->sec_no++);
711 sec = mmo_make_section (abfd, sec_name);
712 if (! sec->user_set_vma)
713 bfd_set_section_vma (abfd, sec, vma);
715 if (! bfd_set_section_flags (abfd, sec,
716 bfd_get_section_flags (abfd, sec)
717 | SEC_LOAD | SEC_ALLOC))
718 return NULL;
719 return sec;
722 /* Xor in a 64-bit value VALUE at VMA. */
724 static INLINE void
725 mmo_xore_64 (sec, vma, value)
726 asection *sec;
727 bfd_vma vma;
728 bfd_vma value;
730 bfd_byte *loc = mmo_get_loc (sec, vma, 8);
731 bfd_vma prev = bfd_get_64 (sec->owner, loc);
733 value ^= prev;
734 bfd_put_64 (sec->owner, value, loc);
737 /* Xor in a 32-bit value VALUE at VMA. */
739 static INLINE void
740 mmo_xore_32 (sec, vma, value)
741 asection *sec;
742 bfd_vma vma;
743 unsigned int value;
745 bfd_byte *loc = mmo_get_loc (sec, vma, 4);
746 unsigned int prev = bfd_get_32 (sec->owner, loc);
748 value ^= prev;
749 bfd_put_32 (sec->owner, value, loc);
752 /* Xor in a 16-bit value VALUE at VMA. */
754 static INLINE void
755 mmo_xore_16 (sec, vma, value)
756 asection *sec;
757 bfd_vma vma;
758 unsigned int value;
760 bfd_byte *loc = mmo_get_loc (sec, vma, 2);
761 unsigned int prev = bfd_get_16 (sec->owner, loc);
763 value ^= prev;
764 bfd_put_16 (sec->owner, value, loc);
767 /* Write a 32-bit word to output file, no lop_quote generated. */
769 static INLINE void
770 mmo_write_tetra_raw (abfd, value)
771 bfd *abfd;
772 unsigned int value;
774 bfd_byte buf[4];
776 bfd_put_32 (abfd, value, buf);
778 if (bfd_bwrite ((PTR) buf, 4, abfd) != 4)
779 abfd->tdata.mmo_data->have_error = true;
782 /* Write a 32-bit word to output file; lop_quote if necessary. */
784 static INLINE void
785 mmo_write_tetra (abfd, value)
786 bfd *abfd;
787 unsigned int value;
789 if (((value >> 24) & 0xff) == LOP)
790 mmo_write_tetra_raw (abfd, LOP_QUOTE_NEXT);
792 mmo_write_tetra_raw (abfd, value);
795 /* Write a 64-bit word to output file, perhaps with lop_quoting. */
797 static INLINE void
798 mmo_write_octa (abfd, value)
799 bfd *abfd;
800 bfd_vma value;
802 mmo_write_tetra (abfd, (unsigned int) (value >> 32));
803 mmo_write_tetra (abfd, (unsigned int) value);
806 /* Write a 64-bit word to output file, without lop_quoting. */
808 static INLINE void
809 mmo_write_octa_raw (abfd, value)
810 bfd *abfd;
811 bfd_vma value;
813 mmo_write_tetra_raw (abfd, (unsigned int) (value >> 32));
814 mmo_write_tetra_raw (abfd, (unsigned int) value);
817 /* Write quoted contents, zero filled. */
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;
826 bfd_byte buf[4] = {0, 0, 0, 0};
828 while (len >= 4)
830 if (loc[0] == LOP)
831 mmo_write_tetra_raw (abfd, LOP_QUOTE_NEXT);
833 retval
834 = (retval == true
835 && abfd->tdata.mmo_data->have_error == false
836 && 4 == bfd_bwrite ((PTR) loc, 4, abfd));
838 loc += 4;
839 len -= 4;
842 if (len)
844 memcpy (buf, loc, len);
845 if (buf[0] == LOP)
846 mmo_write_tetra_raw (abfd, LOP_QUOTE_NEXT);
848 retval
849 = (retval == true
850 && abfd->tdata.mmo_data->have_error == false
851 && 4 == bfd_bwrite ((PTR) buf, 4, abfd));
854 return retval;
857 /* Same, but from a list. */
859 static INLINE boolean
860 mmo_write_chunk_list (abfd, datap)
861 bfd *abfd;
862 mmo_data_list_type *datap;
864 for (; datap != NULL; datap = datap->next)
865 if (! mmo_write_chunk (abfd, datap->data, datap->size))
866 return false;
868 return true;
871 /* Write a lop_loc and some contents. */
873 static boolean
874 mmo_write_loc_chunk (abfd, vma, loc, len)
875 bfd *abfd;
876 bfd_vma vma;
877 CONST bfd_byte *loc;
878 unsigned int len;
880 /* We always write the location as 64 bits; no use saving bytes here. */
881 mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_LOC << 16) | 2);
883 /* Find an initial and trailing section of zero tetras; we don't need to
884 write out zeros. FIXME: When we do this, we should emit section size
885 and address specifiers, else objcopy can't perform a unity
886 translation. */
887 while (len >= 4 && bfd_get_32 (abfd, loc) == 0)
889 vma += 4;
890 len -= 4;
891 loc += 4;
894 while (len >= 4 && bfd_get_32 (abfd, loc + len - 4) == 0)
895 len -= 4;
897 mmo_write_octa_raw (abfd, vma);
898 return
899 abfd->tdata.mmo_data->have_error == false
900 && mmo_write_chunk (abfd, loc, len);
903 /* Same, but from a list. */
905 static INLINE boolean
906 mmo_write_loc_chunk_list (abfd, datap)
907 bfd *abfd;
908 mmo_data_list_type *datap;
910 for (; datap != NULL; datap = datap->next)
911 if (! mmo_write_loc_chunk (abfd, datap->where, datap->data, datap->size))
912 return false;
914 return true;
917 /* Make a .MMIX.spec_data.N section. */
919 static asection *
920 mmo_get_generic_spec_data_section (abfd, spec_data_number)
921 bfd *abfd;
922 int spec_data_number;
924 asection *sec;
925 char secname[sizeof (MMIX_OTHER_SPEC_SECTION_PREFIX) + 20]
926 = MMIX_OTHER_SPEC_SECTION_PREFIX;
928 sprintf (secname + strlen (MMIX_OTHER_SPEC_SECTION_PREFIX),
929 "%d", spec_data_number);
931 sec = mmo_make_section (abfd, secname);
933 return sec;
936 /* Make a special section for SPEC_DATA_NUMBER. If it is the one we use
937 ourselves, parse some of its data to get at the section name. */
939 static asection *
940 mmo_get_spec_section (abfd, spec_data_number)
941 bfd *abfd;
942 int spec_data_number;
944 bfd_byte *secname;
945 asection *sec;
946 bfd_byte buf[4];
947 unsigned int secname_length;
948 unsigned int i;
949 bfd_vma section_length;
950 bfd_vma section_vma;
951 mmo_data_list_type *loc;
952 flagword flags;
953 long orig_pos;
955 /* If this isn't the "special" special data, then make a placeholder
956 section. */
957 if (spec_data_number != SPEC_DATA_SECTION)
958 return mmo_get_generic_spec_data_section (abfd, spec_data_number);
960 /* Seek back to this position if there was a format error. */
961 orig_pos = bfd_tell (abfd);
963 /* Read the length (in 32-bit words). */
964 if (bfd_bread (buf, 4, abfd) != 4)
965 goto format_error;
967 if (buf[0] == LOP)
969 if (buf[1] != LOP_QUOTE)
970 goto format_error;
972 if (bfd_bread (buf, 4, abfd) != 4)
973 goto format_error;
976 /* We don't care to keep the name length accurate. It's
977 zero-terminated. */
978 secname_length = bfd_get_32 (abfd, buf) * 4;
980 /* Check section name length for sanity. */
981 if (secname_length > MAX_SECTION_NAME_SIZE)
982 goto format_error;
984 /* This should be free'd regardless if a section is created. */
985 secname = bfd_malloc (secname_length + 1);
986 secname[secname_length] = 0;
988 for (i = 0; i < secname_length / 4; i++)
990 if (bfd_bread (secname + i * 4, 4, abfd) != 4)
991 goto format_error_free;
993 if (secname[i * 4] == LOP)
995 /* A bit of overkill, but we handle char 0x98 in a section name,
996 and recognize misparsing. */
997 if (secname[i * 4 + 1] != LOP_QUOTE
998 || bfd_bread (secname + i * 4, 4, abfd) != 4)
999 /* Whoops. We thought this was a name, and now we found a
1000 non-lop_quote lopcode before we parsed the whole length of
1001 the name. Signal end-of-file in the same manner. */
1002 goto format_error_free;
1006 /* Get the section flags. */
1007 if (bfd_bread (buf, 4, abfd) != 4
1008 || (buf[0] == LOP
1009 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1010 goto format_error_free;
1012 flags = bfd_get_32 (abfd, buf);
1014 /* Get the section length. */
1015 if (bfd_bread (buf, 4, abfd) != 4
1016 || (buf[0] == LOP
1017 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1018 goto format_error_free;
1020 section_length = (bfd_vma) bfd_get_32 (abfd, buf) << 32;
1022 /* That's the first, high-part. Now get the low part. */
1024 if (bfd_bread (buf, 4, abfd) != 4
1025 || (buf[0] == LOP
1026 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1027 goto format_error_free;
1029 section_length |= (bfd_vma) bfd_get_32 (abfd, buf);
1031 /* Check the section length for sanity. */
1032 if (section_length > MAX_ARTIFICIAL_SECTION_SIZE)
1033 goto format_error_free;
1035 /* Get the section VMA. */
1036 if (bfd_bread (buf, 4, abfd) != 4
1037 || (buf[0] == LOP
1038 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1039 goto format_error_free;
1041 section_vma = (bfd_vma) bfd_get_32 (abfd, buf) << 32;
1043 /* That's the first, high-part. Now get the low part. */
1044 if (bfd_bread (buf, 4, abfd) != 4
1045 || (buf[0] == LOP
1046 && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
1047 goto format_error_free;
1049 section_vma |= (bfd_vma) bfd_get_32 (abfd, buf);
1051 sec = mmo_make_section (abfd, secname);
1052 free (secname);
1053 if (sec == NULL)
1054 goto format_error;
1056 /* We allocate a buffer here for the advertised size, with head room for
1057 tetrabyte alignment. */
1058 loc = bfd_zmalloc (section_length + 3
1059 + sizeof (struct mmo_data_list_struct));
1060 if (loc == NULL)
1061 goto format_error;
1063 /* Use a TETRA-rounded size for the allocated buffer; we set the
1064 "visible" section size below. */
1065 loc->size = (section_length + 3) & ~3;
1067 /* Add in the section flags we found to those bfd entered during this
1068 process and set the contents. */
1069 if (! bfd_set_section_flags (abfd, sec,
1070 bfd_sec_flags_from_mmo_flags (flags)
1071 | bfd_get_section_flags (abfd, sec)
1072 | (section_length != 0 ? SEC_HAS_CONTENTS : 0))
1073 || ! bfd_set_section_size (abfd, sec,
1074 sec->_cooked_size + section_length)
1075 /* Set VMA only for the first occurrence. */
1076 || (! sec->user_set_vma
1077 && ! bfd_set_section_vma (abfd, sec, section_vma)))
1079 /* If we get an error for any of the calls above, signal more than
1080 just a format error for the spec section. */
1081 return NULL;
1084 loc->next = NULL;
1085 if (((struct mmo_section_data_struct *) (sec->used_by_bfd))->tail != NULL)
1086 ((struct mmo_section_data_struct *) (sec->used_by_bfd))->tail->next
1087 = loc;
1088 else
1089 ((struct mmo_section_data_struct *) (sec->used_by_bfd))->head = loc;
1090 ((struct mmo_section_data_struct *) (sec->used_by_bfd))->tail = loc;
1091 loc->where = section_vma;
1093 return sec;
1095 format_error_free:
1096 free (secname);
1097 format_error:
1098 if (bfd_seek (abfd, orig_pos, SEEK_SET) != 0)
1099 return NULL;
1101 return mmo_get_generic_spec_data_section (abfd, spec_data_number);
1104 /* Read a byte, but read from file in multiples of 32-bit words. */
1106 static bfd_byte
1107 mmo_get_byte (abfd)
1108 bfd *abfd;
1110 bfd_byte retval;
1112 if (abfd->tdata.mmo_data->byte_no == 0)
1114 if (abfd->tdata.mmo_data->have_error == false
1115 && bfd_bread (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
1117 abfd->tdata.mmo_data->have_error = true;
1119 /* A value somewhat safe against tripping on some inconsistency
1120 when mopping up after this error. */
1121 return 128;
1125 retval = abfd->tdata.mmo_data->buf[abfd->tdata.mmo_data->byte_no];
1126 abfd->tdata.mmo_data->byte_no = (abfd->tdata.mmo_data->byte_no + 1) % 4;
1128 return retval;
1131 /* Write a byte, in multiples of 32-bit words. */
1133 static void
1134 mmo_write_byte (abfd, value)
1135 bfd *abfd;
1136 bfd_byte value;
1138 abfd->tdata.mmo_data->buf[(abfd->tdata.mmo_data->byte_no++ % 4)] = value;
1139 if ((abfd->tdata.mmo_data->byte_no % 4) == 0)
1141 if (abfd->tdata.mmo_data->have_error == false
1142 && bfd_bwrite (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
1143 abfd->tdata.mmo_data->have_error = true;
1147 /* Create a symbol. */
1149 static boolean
1150 mmo_create_symbol (abfd, symname, addr, sym_type, serno)
1151 bfd *abfd;
1152 CONST char *symname;
1153 bfd_vma addr;
1154 enum mmo_sym_type sym_type;
1155 unsigned int serno;
1157 struct mmo_symbol *n;
1159 n = (struct mmo_symbol *) bfd_alloc (abfd, sizeof (struct mmo_symbol));
1160 if (n == NULL)
1161 return false;
1163 n->name = bfd_alloc (abfd, strlen (symname) + 1);
1164 if (n->name == NULL)
1165 return false;
1167 strcpy ((PTR) n->name, symname);
1169 n->value = addr;
1170 n->sym_type = sym_type;
1171 n->serno = serno;
1173 if (abfd->tdata.mmo_data->symbols == NULL)
1174 abfd->tdata.mmo_data->symbols = n;
1175 else
1176 abfd->tdata.mmo_data->symtail->next = n;
1177 abfd->tdata.mmo_data->symtail = n;
1178 n->next = NULL;
1180 ++abfd->symcount;
1182 /* Check that :Main equals the last octa of the .MMIX.reg_contents
1183 section, as it's the one place we're sure to pass when reading a mmo
1184 object. For written objects, we do it while setting the symbol
1185 table. */
1186 if (strcmp (symname, MMIX_START_SYMBOL_NAME) == 0
1187 && bfd_get_start_address (abfd) != addr)
1189 (*_bfd_error_handler)
1190 (_("%s: invalid mmo file: initialization value for $255 is not `Main'\n"),
1191 bfd_get_filename (abfd));
1192 bfd_set_error (bfd_error_bad_value);
1193 return false;
1196 return true;
1199 /* Read in symbols. */
1201 static boolean
1202 mmo_get_symbols (abfd)
1203 bfd *abfd;
1206 INODE
1207 Symbol-table, mmo section mapping, File layout, mmo
1208 SUBSECTION
1209 Symbol table format
1211 From mmixal.w (or really, the generated mmixal.tex) in
1212 @url{http://www-cs-faculty.stanford.edu/~knuth/programs/mmix.tar.gz}):
1213 ``Symbols are stored and retrieved by means of a @samp{ternary
1214 search trie}, following ideas of Bentley and Sedgewick. (See
1215 ACM--SIAM Symp.@: on Discrete Algorithms @samp{8} (1997), 360--369;
1216 R.@:Sedgewick, @samp{Algorithms in C} (Reading, Mass.@:
1217 Addison--Wesley, 1998), @samp{15.4}.) Each trie node stores a
1218 character, and there are branches to subtries for the cases where
1219 a given character is less than, equal to, or greater than the
1220 character in the trie. There also is a pointer to a symbol table
1221 entry if a symbol ends at the current node.''
1223 So it's a tree encoded as a stream of bytes. The stream of bytes
1224 acts on a single virtual global symbol, adding and removing
1225 characters and signalling complete symbol points. Here, we read
1226 the stream and create symbols at the completion points.
1228 First, there's a control byte <<m>>. If any of the listed bits
1229 in <<m>> is nonzero, we execute what stands at the right, in
1230 the listed order:
1232 | (MMO3_LEFT)
1233 | 0x40 - Traverse left trie.
1234 | (Read a new command byte and recurse.)
1236 | (MMO3_SYMBITS)
1237 | 0x2f - Read the next byte as a character and store it in the
1238 | current character position; increment character position.
1239 | Test the bits of <<m>>:
1241 | (MMO3_WCHAR)
1242 | 0x80 - The character is 16-bit (so read another byte,
1243 | merge into current character.
1245 | (MMO3_TYPEBITS)
1246 | 0xf - We have a complete symbol; parse the type, value
1247 | and serial number and do what should be done
1248 | with a symbol. The type and length information
1249 | is in j = (m & 0xf).
1251 | (MMO3_REGQUAL_BITS)
1252 | j == 0xf: A register variable. The following
1253 | byte tells which register.
1254 | j <= 8: An absolute symbol. Read j bytes as the
1255 | big-endian number the symbol equals.
1256 | A j = 2 with two zero bytes denotes an
1257 | unknown symbol.
1258 | j > 8: As with j <= 8, but add (0x20 << 56)
1259 | to the value in the following j - 8
1260 | bytes.
1262 | Then comes the serial number, as a variant of
1263 | uleb128, but better named ubeb128:
1264 | Read bytes and shift the previous value left 7
1265 | (multiply by 128). Add in the new byte, repeat
1266 | until a byte has bit 7 set. The serial number
1267 | is the computed value minus 128.
1269 | (MMO3_MIDDLE)
1270 | 0x20 - Traverse middle trie. (Read a new command byte
1271 | and recurse.) Decrement character position.
1273 | (MMO3_RIGHT)
1274 | 0x10 - Traverse right trie. (Read a new command byte and
1275 | recurse.)
1277 Let's look again at the <<lop_stab>> for the trivial file
1278 (@pxref{File layout}).
1280 | 0x980b0000 - lop_stab for ":Main" = 0, serial 1.
1281 | 0x203a4040
1282 | 0x10404020
1283 | 0x4d206120
1284 | 0x69016e00
1285 | 0x81000000
1287 This forms the trivial trie (note that the path between ``:'' and
1288 ``M'' is redundant):
1290 | 203a ":"
1291 | 40 /
1292 | 40 /
1293 | 10 \
1294 | 40 /
1295 | 40 /
1296 | 204d "M"
1297 | 2061 "a"
1298 | 2069 "i"
1299 | 016e "n" is the last character in a full symbol, and
1300 | with a value represented in one byte.
1301 | 00 The value is 0.
1302 | 81 The serial number is 1. */
1304 bfd_byte m = mmo_get_byte (abfd);
1306 /* Check first if we have a bad hair day. */
1307 if (abfd->tdata.mmo_data->have_error == true)
1308 return false;
1310 if (m & MMO3_LEFT)
1311 /* Traverse left trie. */
1312 mmo_get_symbols (abfd);
1314 if (m & MMO3_SYMBITS)
1316 bfd_byte c = mmo_get_byte (abfd);
1317 bfd_byte j = m & MMO3_TYPEBITS;
1318 bfd_vma addr = 0;
1319 enum mmo_sym_type sym_type;
1320 unsigned int serno = 0;
1321 bfd_byte k;
1323 if (m & MMO3_WCHAR)
1325 bfd_byte c2 = mmo_get_byte (abfd);
1327 /* A two-byte character. We can't grok this, but neither can
1328 mmotype, for other cases than the second byte being zero. */
1330 if (c != 0)
1332 abfd->tdata.mmo_data->lop_stab_symbol
1333 [abfd->tdata.mmo_data->symbol_position] = 0;
1335 (*_bfd_error_handler)
1336 (_("%s: unsupported wide character sequence\
1337 0x%02X 0x%02X after symbol name starting with `%s'\n"),
1338 bfd_get_filename (abfd), c, c2,
1339 abfd->tdata.mmo_data->lop_stab_symbol);
1340 bfd_set_error (bfd_error_bad_value);
1341 abfd->tdata.mmo_data->have_error = true;
1342 return false;
1344 else
1345 c = c2;
1348 abfd->tdata.mmo_data->lop_stab_symbol[abfd->tdata.mmo_data->symbol_position++] = c;
1349 abfd->tdata.mmo_data->lop_stab_symbol[abfd->tdata.mmo_data->symbol_position] = 0;
1351 if (j & MMO3_REGQUAL_BITS)
1353 if (j == MMO3_REGQUAL_BITS)
1355 sym_type = mmo_reg_sym;
1356 addr = mmo_get_byte (abfd);
1358 else if (j <= 8)
1360 unsigned int i;
1362 for (i = 0; i < j; i++)
1363 addr = (addr << 8) + mmo_get_byte (abfd);
1365 if (addr == 0 && j == MMO3_UNDEF)
1366 sym_type = mmo_undef_sym;
1367 else
1368 sym_type = mmo_abs_sym;
1370 else
1372 unsigned int i;
1374 for (i = MMO3_DATA; i < j; i++)
1375 addr = (addr << 8) + mmo_get_byte (abfd);
1377 addr += (bfd_vma) 0x20 << 56;
1378 sym_type = mmo_data_sym;
1381 /* Get the serial number. */
1384 k = mmo_get_byte (abfd);
1385 serno = (serno << 7) + k;
1387 while (k < 128);
1388 serno -= 128;
1390 /* Got it. Now enter it. Skip a leading ":". */
1391 if (abfd->tdata.mmo_data->have_error == false
1392 && ! mmo_create_symbol (abfd,
1393 abfd->tdata.mmo_data->lop_stab_symbol
1394 + 1,
1395 addr, sym_type, serno))
1396 abfd->tdata.mmo_data->have_error = true;
1399 if (m & MMO3_MIDDLE)
1400 /* Traverse middle trie. */
1401 mmo_get_symbols (abfd);
1403 abfd->tdata.mmo_data->symbol_position--;
1406 if (m & MMO3_RIGHT)
1407 /* Traverse right trie. */
1408 mmo_get_symbols (abfd);
1410 return abfd->tdata.mmo_data->have_error == false;
1413 /* Get the location of memory area [VMA..VMA + SIZE - 1], which we think
1414 is in section SEC. Adjust and reallocate zero-initialized contents.
1415 If there's new contents, allocate to the next multiple of
1416 MMO_SEC_CONTENTS_CHUNK_SIZE. */
1418 static INLINE bfd_byte *
1419 mmo_get_loc (sec, vma, size)
1420 asection *sec;
1421 bfd_vma vma;
1422 int size;
1424 bfd_size_type allocated_size;
1425 struct mmo_section_data_struct *sdatap
1426 = (struct mmo_section_data_struct *) sec->used_by_bfd;
1427 struct mmo_data_list_struct *datap = sdatap->head;
1428 struct mmo_data_list_struct *entry;
1430 for (; datap != NULL; datap = datap->next)
1432 if (datap->where <= vma
1433 && datap->where + datap->size >= vma + size)
1434 return datap->data + vma - datap->where;
1435 else if (datap->where <= vma
1436 && datap->where + datap->allocated_size >= vma + size
1437 /* Only munch on the "allocated size" if it does not
1438 overlap the next chunk. */
1439 && (datap->next == NULL || datap->next->where >= vma + size))
1441 /* There was room allocated, but the size wasn't set to include
1442 it. Do that now. */
1443 datap->size += (vma + size) - (datap->where + datap->size);
1445 /* Update the section size. */
1446 if (vma + size > sec->vma + sec->_raw_size)
1447 sec->_raw_size += (vma + size) - (sec->vma + sec->_raw_size);
1449 return datap->data + vma - datap->where;
1453 /* Not found; allocate a new block. First check in case we get a
1454 request for a size split up over several blocks; we'll have to return
1455 NULL for those cases, requesting the caller to split up the request.
1456 Requests with an address aligned on MMO_SEC_CONTENTS_CHUNK_SIZE bytes and
1457 for no more than MMO_SEC_CONTENTS_CHUNK_SIZE will always get resolved. */
1459 for (datap = sdatap->head; datap != NULL; datap = datap->next)
1460 if ((datap->where <= vma && datap->where + datap->size > vma)
1461 || (datap->where < vma + size
1462 && datap->where + datap->size >= vma + size))
1463 return NULL;
1465 allocated_size
1466 = (size + MMO_SEC_CONTENTS_CHUNK_SIZE - 1) & ~(MMO_SEC_CONTENTS_CHUNK_SIZE - 1);
1467 entry = (mmo_data_list_type *)
1468 bfd_zalloc (sec->owner, sizeof (mmo_data_list_type) + allocated_size);
1469 if (entry == NULL)
1470 return false;
1471 entry->where = vma;
1472 entry->size = size;
1473 entry->allocated_size = allocated_size;
1475 datap = sdatap->head;
1477 /* Sort the records by address. Optimize for the common case of adding
1478 a record to the end of the list. */
1479 if (sdatap->tail != NULL && entry->where >= sdatap->tail->where)
1481 sdatap->tail->next = entry;
1482 entry->next = NULL;
1483 sdatap->tail = entry;
1485 else
1487 mmo_data_list_type **look;
1488 for (look = &sdatap->head;
1489 *look != NULL && (*look)->where < entry->where;
1490 look = &(*look)->next)
1492 entry->next = *look;
1493 *look = entry;
1494 if (entry->next == NULL)
1496 sdatap->tail = entry;
1498 /* We get here for the first time (at other times too) for this
1499 section. Say we have contents. */
1500 if (! bfd_set_section_flags (sec->owner, sec,
1501 bfd_get_section_flags (sec->owner, sec)
1502 | SEC_HAS_CONTENTS))
1503 return NULL;
1507 /* Update the section size. */
1508 if (vma + size > sec->vma + sec->_raw_size)
1509 sec->_raw_size += (vma + size) - (sec->vma + sec->_raw_size);
1510 return entry->data;
1513 /* Set sizes once we've read in all sections. */
1515 static void
1516 mmo_map_set_sizes (abfd, sec, ignored)
1517 bfd *abfd ATTRIBUTE_UNUSED;
1518 asection *sec;
1519 PTR ignored ATTRIBUTE_UNUSED;
1521 sec->_cooked_size = sec->_raw_size;
1522 sec->lma = sec->vma;
1525 /* Read the mmo file and turn it into sections. */
1527 static boolean
1528 mmo_scan (abfd)
1529 bfd *abfd;
1531 unsigned int i;
1532 unsigned int lineno = 1;
1533 boolean error = false;
1534 bfd_vma vma = 0;
1535 asection *sec = bfd_make_section_old_way (abfd, MMO_TEXT_SECTION_NAME);
1536 asection *non_spec_sec = NULL;
1537 bfd_vma non_spec_vma = 0;
1538 char *current_filename = NULL;
1539 bfd_size_type nbytes_read = 0;
1540 /* Buffer with room to read a 64-bit value. */
1541 bfd_byte buf[8];
1542 long stab_loc = -1;
1543 char *file_names[256];
1545 memset (file_names, 0, sizeof (file_names));
1547 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
1548 goto error_return;
1550 while ((nbytes_read = bfd_bread (buf, 4, abfd)) == 4)
1552 if (buf[0] == LOP)
1554 unsigned int y = bfd_get_8 (abfd, buf + 2);
1555 unsigned int z = bfd_get_8 (abfd, buf + 3);
1557 /* Change back to the original section for lopcodes other
1558 than LOP_QUOTE that comes after a LOP_SPEC. */
1559 if ((buf[1] != LOP_QUOTE || y != 0 || z != 1)
1560 && non_spec_sec != NULL)
1562 sec = non_spec_sec;
1563 vma = non_spec_vma;
1564 non_spec_sec = NULL;
1567 switch (buf[1])
1569 default:
1570 (*_bfd_error_handler)
1571 (_("%s: invalid mmo file: unsupported lopcode `%d'\n"),
1572 bfd_get_filename (abfd), buf[1]);
1573 bfd_set_error (bfd_error_bad_value);
1574 goto error_return;
1576 case LOP_QUOTE:
1577 /* Quote the next 32-bit word. */
1578 if (y != 0 || z != 1)
1580 (*_bfd_error_handler)
1581 (_("%s: invalid mmo file: expected YZ = 1 got YZ = %d for lop_quote\n"),
1582 bfd_get_filename (abfd), y*256+z);
1583 bfd_set_error (bfd_error_bad_value);
1584 goto error_return;
1586 if (bfd_bread (buf, 4, abfd) != 4)
1587 goto error_return;
1589 mmo_xore_32 (sec, vma, bfd_get_32 (abfd, buf));
1590 vma += 4;
1591 vma &= ~3;
1592 lineno++;
1593 break;
1595 case LOP_LOC:
1596 /* Set vma (and section). */
1597 vma = (bfd_vma) y << 56;
1598 if (z == 1)
1600 /* Get a 32-bit value. */
1601 if (bfd_bread (buf, 4, abfd) != 4)
1602 goto error_return;
1604 vma += bfd_get_32 (abfd, buf);
1606 else if (z == 2)
1608 /* Get a 64-bit value. */
1609 if (bfd_bread (buf, 8, abfd) != 8)
1610 goto error_return;
1612 vma += bfd_get_64 (abfd, buf);
1614 else
1616 (*_bfd_error_handler)
1617 (_("%s: invalid mmo file: expected z = 1 or z = 2, got z = %d for lop_loc\n"),
1618 bfd_get_filename (abfd), z);
1619 bfd_set_error (bfd_error_bad_value);
1620 goto error_return;
1623 sec = mmo_decide_section (abfd, vma);
1624 if (sec == NULL)
1625 goto error_return;
1626 break;
1628 case LOP_SKIP:
1629 /* Move forward within the same section. */
1630 vma += y * 256 + z;
1632 sec = mmo_decide_section (abfd, vma);
1633 if (sec == NULL)
1634 goto error_return;
1635 break;
1637 case LOP_FIXO:
1638 /* A fixup: Store the current vma somewhere. Position using
1639 same format as LOP_LOC. */
1641 bfd_vma p = (bfd_vma) y << 56;
1642 asection *fixosec;
1644 if (z == 1)
1646 /* Get a 32-bit value. */
1647 if (bfd_bread (buf, 4, abfd) != 4)
1648 goto error_return;
1650 p += bfd_get_32 (abfd, buf);
1652 else if (z == 2)
1654 /* Get a 64-bit value. */
1655 if (bfd_bread (buf, 8, abfd) != 8)
1656 goto error_return;
1658 p += bfd_get_64 (abfd, buf);
1660 else
1662 (*_bfd_error_handler)
1663 (_("%s: invalid mmo file: expected z = 1 or z = 2, got z = %d for lop_fixo\n"),
1664 bfd_get_filename (abfd), z);
1665 bfd_set_error (bfd_error_bad_value);
1666 goto error_return;
1669 /* The section where we store this address might be a
1670 different one than the current section. */
1671 fixosec = mmo_decide_section (abfd, p);
1672 if (fixosec == NULL)
1673 goto error_return;
1674 mmo_xore_64 (fixosec, p, vma);
1676 break;
1678 case LOP_FIXR:
1679 /* A fixup: Store YZ of this lopcode into YZ at vma - 4 * yz. */
1681 unsigned int yz = (y * 256 + z);
1682 bfd_vma p = vma + 2 - 4 * yz;
1683 asection *fixrsec = mmo_decide_section (abfd, p);
1684 if (fixrsec == NULL)
1685 goto error_return;
1686 mmo_xore_16 (fixrsec, p, yz);
1688 break;
1690 case LOP_FIXRX:
1691 /* A fixup, similar to lop_fixr, but taking larger numbers
1692 and can change branches into the opposite direction
1693 (gasp!). */
1695 bfd_vma delta;
1696 bfd_vma p;
1697 asection *fixrsec;
1699 if (y != 0)
1701 (*_bfd_error_handler)
1702 (_("%s: invalid mmo file: expected y = 0, got y = %d for lop_fixrx\n"),
1703 bfd_get_filename (abfd), y);
1704 bfd_set_error (bfd_error_bad_value);
1705 goto error_return;
1708 if (z != 16 && z != 24)
1710 (*_bfd_error_handler)
1711 (_("%s: invalid mmo file: expected z = 16 or z = 24, got z = %d for lop_fixrx\n"),
1712 bfd_get_filename (abfd), z);
1713 bfd_set_error (bfd_error_bad_value);
1714 goto error_return;
1717 /* Get the next 32-bit value. */
1718 if (bfd_bread (buf, 4, abfd) != 4)
1719 goto error_return;
1721 delta = bfd_get_32 (abfd, buf);
1723 /* Do an, ehm, involved calculation for the location of
1724 the fixup. See mmixal documentation for a verbose
1725 explanation. We follow it verbosely here for the
1726 readers delight. */
1727 if (buf[0] == 0)
1728 p = vma - 4 * delta;
1729 else if (buf[0] == 1)
1730 p = vma - 4 * ((delta & 0xffffff) - (1 << z));
1731 else
1733 (*_bfd_error_handler)
1734 (_("%s: invalid mmo file: leading byte of operand word must be 0 or 1, got %d for lop_fixrx\n"),
1735 bfd_get_filename (abfd), buf[0]);
1736 bfd_set_error (bfd_error_bad_value);
1737 goto error_return;
1740 fixrsec = mmo_decide_section (abfd, vma);
1741 if (fixrsec == NULL)
1742 goto error_return;
1743 mmo_xore_32 (fixrsec, p, delta);
1745 break;
1747 case LOP_FILE:
1748 /* Set current file and perhaps the file name. Reset line
1749 number. */
1750 if (z != 0)
1752 char *fname = bfd_malloc (z * 4 + 1);
1754 if (fname == NULL)
1756 (*_bfd_error_handler)
1757 (_("%s: cannot allocate file name for file number %d, %d bytes\n"),
1758 bfd_get_filename (abfd), y, z * 4 + 1);
1759 bfd_set_error (bfd_error_system_call);
1760 goto error_return;
1763 fname[z * 4] = 0;
1765 for (i = 0; i < z; i++)
1767 if (bfd_bread (fname + i * 4, 4, abfd) != 4)
1769 free (fname);
1770 goto error_return;
1774 if (file_names[y] != NULL)
1776 (*_bfd_error_handler)
1777 (_("%s: invalid mmo file: file number %d `%s',\
1778 was already entered as `%s'\n"),
1779 bfd_get_filename (abfd), y, fname, file_names[y]);
1780 bfd_set_error (bfd_error_bad_value);
1781 goto error_return;
1784 file_names[y] = fname;
1787 if (file_names[y] == NULL)
1789 (*_bfd_error_handler)
1790 (_("%s: invalid mmo file: file name for number %d\
1791 was not specified before use\n"),
1792 bfd_get_filename (abfd), y);
1793 bfd_set_error (bfd_error_bad_value);
1794 goto error_return;
1797 current_filename = file_names[y];
1798 lineno = 0;
1799 break;
1801 case LOP_LINE:
1802 /* Set line number. */
1803 lineno = y * 256 + z;
1804 /* FIXME: Create a sequence of mmo-specific line number
1805 entries for each section, then translate into canonical
1806 format. */
1807 break;
1809 case LOP_SPEC:
1810 /* Special data follows until the next non-lop_quote
1811 lopcode. */
1812 non_spec_sec = sec;
1813 non_spec_vma = vma;
1814 sec = mmo_get_spec_section (abfd, y * 256 + z);
1815 if (sec == NULL)
1816 goto error_return;
1818 vma = sec->vma;
1819 break;
1821 case LOP_PRE:
1823 /* We ignore header information, except we read in the
1824 creation time from the first 32-bit word with the time
1825 in seconds since era. */
1826 if (z >= 1
1827 && bfd_bread (abfd->tdata.mmo_data->created, 4,
1828 abfd) != 4)
1829 goto error_return;
1831 for (i = 1; i < z; i++)
1832 if (bfd_bread (buf, 4, abfd) != 4)
1833 goto error_return;
1835 break;
1837 case LOP_POST:
1838 /* This tells of the contents of registers $Z..$255 at
1839 startup. We make a section out of it, with VMA = Z * 8,
1840 but only if Z != 255 or the contents is non-zero. */
1842 asection *rsec;
1843 bfd_byte *loc;
1844 bfd_vma first_octa;
1845 bfd_vma startaddr_octa;
1847 /* Read first octaword outside loop to simplify logic when
1848 excluding the Z == 255, octa == 0 case. */
1849 if (bfd_bread (buf, 8, abfd) != 8)
1850 goto error_return;
1852 first_octa = bfd_get_64 (abfd, buf);
1854 /* Don't emit contents for the trivial case which is
1855 always present; $255 pointing to Main. */
1856 if (z != 255)
1858 rsec
1859 = bfd_make_section_old_way (abfd,
1860 MMIX_REG_CONTENTS_SECTION_NAME);
1861 rsec->vma = z * 8;
1862 loc = mmo_get_loc (rsec, z * 8, (255 - z) * 8);
1863 bfd_put_64 (abfd, first_octa, loc);
1865 for (i = z + 1; i < 255; i++)
1867 if (bfd_bread (loc + (i - z) * 8, 8, abfd) != 8)
1868 goto error_return;
1871 /* Read out the last octabyte, and use it to set the
1872 start address. */
1873 if (bfd_bread (buf, 8, abfd) != 8)
1874 goto error_return;
1876 startaddr_octa = bfd_get_64 (abfd, buf);
1878 else
1879 startaddr_octa = first_octa;
1881 if (! bfd_set_start_address (abfd, startaddr_octa))
1883 /* Currently this can't fail, but this should handle
1884 future failures. */
1885 bfd_set_error (bfd_error_bad_value);
1886 goto error_return;
1889 break;
1891 case LOP_STAB:
1892 /* We read in the symbols now, not later. */
1893 if (y != 0 || z != 0)
1895 (*_bfd_error_handler)
1896 (_("%s: invalid mmo file: fields y and z of lop_stab\
1897 non-zero, y: %d, z: %d\n"),
1898 bfd_get_filename (abfd), y, z);
1899 bfd_set_error (bfd_error_bad_value);
1900 goto error_return;
1903 /* Save the location, so we can check that YZ in the LOP_END
1904 is correct. */
1905 stab_loc = bfd_tell (abfd);
1907 /* It's not said that an MMO can be without symbols (though
1908 mmixal will refuse to assemble files without Main), but
1909 it seems it would still be a valid mmo-file, so allow it.
1910 We detect the absence of a symbol area in that the upper
1911 limit is computed (from the lop_end YZ field) as 0.
1912 Don't call mmo_get_symbols; it can only detect the end of
1913 a valid symbol trie, not the absence of one. */
1914 if (abfd->tdata.mmo_data->max_symbol_length != 0
1915 && ! mmo_get_symbols (abfd))
1916 goto error_return;
1917 break;
1919 case LOP_END:
1921 /* This must be the last 32-bit word in an mmo file.
1922 Let's find out. */
1923 struct stat statbuf;
1924 long curpos = bfd_tell (abfd);
1926 if (bfd_stat (abfd, &statbuf) < 0)
1927 goto error_return;
1929 if (statbuf.st_size != curpos)
1931 (*_bfd_error_handler)
1932 (_("%s: invalid mmo file: lop_end not last item in\
1933 file\n"),
1934 bfd_get_filename (abfd));
1935 bfd_set_error (bfd_error_bad_value);
1936 goto error_return;
1939 /* Check that the YZ field is right. Subtract the size of
1940 this LOP_END in the calculation; YZ does not include
1941 it. */
1942 if ((long) (y * 256 + z) * 4 != (curpos - stab_loc) - 4)
1944 (*_bfd_error_handler)
1945 (_("%s: invalid mmo file: YZ of lop_end (%ld)\
1946 not equal to the number of tetras to the preceding lop_stab (%ld)\n"),
1947 bfd_get_filename (abfd), (long) (y * 256 + z),
1948 (curpos - stab_loc - 4)/4);
1949 bfd_set_error (bfd_error_bad_value);
1950 goto error_return;
1953 bfd_map_over_sections (abfd, mmo_map_set_sizes, NULL);
1954 goto done;
1958 else
1960 /* This wasn't a lopcode, so store it in the current section. */
1961 mmo_xore_32 (sec, vma & ~3, bfd_get_32 (abfd, buf));
1962 vma += 4;
1963 vma &= ~3;
1964 lineno++;
1968 /* We know this file is a multiple of four bytes (checked in
1969 mmo_object_p), so if we got something other than 0, this was a bad
1970 file (although it's more likely we'll get 0 in that case too).
1971 If we got end-of-file, then there was no lop_stab, so the file has
1972 invalid format. */
1974 if (nbytes_read != 0)
1975 bfd_set_error (bfd_error_system_call);
1976 else
1977 bfd_set_error (bfd_error_bad_value);
1979 error_return:
1980 error = true;
1981 done:
1982 /* Mark the .text and .data section with their normal attribute if they
1983 contain anything. This is not redundant wrt. mmo_decide_section,
1984 since that code might never execute, and conversely the alloc+code
1985 section flags must be set then. */
1986 sec = bfd_get_section_by_name (abfd, MMO_TEXT_SECTION_NAME);
1987 if (sec != NULL
1988 && (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
1989 && ! bfd_set_section_flags (abfd, sec,
1990 bfd_get_section_flags (abfd, sec)
1991 | SEC_ALLOC | SEC_LOAD | SEC_CODE))
1992 error = true;
1994 sec = bfd_get_section_by_name (abfd, MMO_DATA_SECTION_NAME);
1995 if (sec != NULL
1996 && (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
1997 && ! bfd_set_section_flags (abfd, sec,
1998 bfd_get_section_flags (abfd, sec)
1999 | SEC_ALLOC | SEC_LOAD))
2000 error = true;
2002 /* Free whatever resources we took. */
2003 for (i = 0; i < sizeof (file_names) / sizeof (file_names[0]); i++)
2004 if (file_names[i])
2005 free (file_names[i]);
2006 return error ? false : true;
2009 /* A hook to set up object file dependent section information. For mmo,
2010 we point out the shape of allocated section contents. */
2012 static boolean
2013 mmo_new_section_hook (abfd, newsect)
2014 bfd *abfd ATTRIBUTE_UNUSED;
2015 asection *newsect;
2017 /* We zero-fill all fields and assume NULL is represented by an all
2018 zero-bit pattern. */
2019 newsect->used_by_bfd =
2020 (PTR) bfd_zalloc (abfd, sizeof (struct mmo_section_data_struct));
2022 if (!newsect->used_by_bfd)
2023 return false;
2025 /* Always align to at least 32-bit words. */
2026 newsect->alignment_power = 2;
2027 return true;
2030 /* We already have section contents loaded for sections that have
2031 contents. */
2033 static boolean
2034 mmo_get_section_contents (abfd, sec, location, offset, bytes_to_do)
2035 bfd *abfd ATTRIBUTE_UNUSED;
2036 asection *sec ATTRIBUTE_UNUSED;
2037 PTR location ATTRIBUTE_UNUSED;
2038 file_ptr offset ATTRIBUTE_UNUSED;
2039 bfd_size_type bytes_to_do ATTRIBUTE_UNUSED;
2041 /* Iterate over diminishing chunk sizes, copying contents, like
2042 mmo_set_section_contents. */
2043 while (bytes_to_do)
2045 /* A minor song-and-dance to make sure we're not bitten by the
2046 distant possibility of the cast from bfd_vma to int making the
2047 chunk zero-sized. */
2048 int chunk_size
2049 = (int) bytes_to_do != 0 ? bytes_to_do : MMO_SEC_CONTENTS_CHUNK_SIZE;
2050 bfd_byte *loc;
2053 loc = mmo_get_loc (sec, sec->vma + offset, chunk_size);
2054 while (loc == NULL && (chunk_size /= 2) != 0);
2056 if (chunk_size == 0)
2057 return false;
2059 memcpy (location, loc, chunk_size);
2061 location += chunk_size;
2062 bytes_to_do -= chunk_size;
2063 offset += chunk_size;
2065 return true;
2068 /* Return the amount of memory needed to read the symbol table. */
2070 static long
2071 mmo_get_symtab_upper_bound (abfd)
2072 bfd *abfd ATTRIBUTE_UNUSED;
2074 return (abfd->symcount + 1) * sizeof (asymbol *);
2077 /* Sort mmo symbols by serial number. */
2079 static int
2080 mmo_sort_mmo_symbols (arg1, arg2)
2081 CONST PTR arg1;
2082 CONST PTR arg2;
2084 CONST struct mmo_symbol *sym1 = *(CONST struct mmo_symbol **) arg1;
2085 CONST struct mmo_symbol *sym2 = *(CONST struct mmo_symbol **) arg2;
2087 /* Sort by serial number first. */
2088 if (sym1->serno < sym2->serno)
2089 return -1;
2090 else if (sym1->serno > sym2->serno)
2091 return 1;
2093 /* Then sort by address of the table entries. */
2094 return ((CONST char *) arg1 - (CONST char *) arg2);
2097 /* Translate the symbol table. */
2099 static long
2100 mmo_get_symtab (abfd, alocation)
2101 bfd *abfd;
2102 asymbol **alocation;
2104 unsigned int symcount = bfd_get_symcount (abfd);
2105 asymbol *csymbols;
2106 unsigned int i;
2108 csymbols = abfd->tdata.mmo_data->csymbols;
2109 if (csymbols == NULL)
2111 asymbol *c;
2112 struct mmo_symbol *s;
2113 struct mmo_symbol **msp;
2115 /* First we store the symbols into the table we'll return, then we
2116 qsort it on the serial number, with secondary on the address of
2117 the symbol, to preserve order if there would be non-unique serial
2118 numbers. */
2119 for (s = abfd->tdata.mmo_data->symbols,
2120 msp = (struct mmo_symbol **) alocation;
2121 s != NULL;
2122 s = s->next, ++msp)
2123 *msp = s;
2125 *msp = NULL;
2127 qsort (alocation, symcount, sizeof (struct mmo_symbol *),
2128 mmo_sort_mmo_symbols);
2130 csymbols = (asymbol *) bfd_alloc (abfd, symcount * sizeof (asymbol));
2131 if (csymbols == NULL && symcount != 0)
2132 return false;
2133 abfd->tdata.mmo_data->csymbols = csymbols;
2135 for (msp = (struct mmo_symbol **) alocation, c = csymbols;
2136 *msp != NULL;
2137 msp++, ++c)
2139 s = *msp;
2140 c->the_bfd = abfd;
2141 c->name = s->name;
2142 c->value = s->value;
2143 c->flags = BSF_GLOBAL;
2145 if (s->sym_type == mmo_data_sym)
2147 c->section
2148 = bfd_get_section_by_name (abfd, MMO_DATA_SECTION_NAME);
2150 if (c->section == NULL)
2151 c->section = bfd_abs_section_ptr;
2152 else
2153 c->value -= c->section->vma;
2155 else if (s->sym_type == mmo_undef_sym)
2156 c->section = bfd_und_section_ptr;
2157 else if (s->sym_type == mmo_reg_sym)
2159 c->section
2160 = bfd_make_section_old_way (abfd, MMIX_REG_SECTION_NAME);
2162 else
2164 asection *textsec
2165 = bfd_get_section_by_name (abfd, MMO_TEXT_SECTION_NAME);
2167 if (textsec != NULL
2168 && c->value >= textsec->vma
2169 && c->value <= textsec->vma + textsec->_cooked_size)
2171 c->section = textsec;
2172 c->value -= c->section->vma;
2174 else
2175 c->section = bfd_abs_section_ptr;
2178 c->udata.p = NULL;
2182 /* Last, overwrite the incoming table with the right-type entries. */
2183 for (i = 0; i < symcount; i++)
2184 *alocation++ = csymbols++;
2185 *alocation = NULL;
2187 return symcount;
2190 /* Get information about a symbol. */
2192 static void
2193 mmo_get_symbol_info (ignore_abfd, symbol, ret)
2194 bfd *ignore_abfd ATTRIBUTE_UNUSED;
2195 asymbol *symbol;
2196 symbol_info *ret;
2198 bfd_symbol_info (symbol, ret);
2201 static void
2202 mmo_print_symbol (abfd, afile, symbol, how)
2203 bfd *abfd;
2204 PTR afile;
2205 asymbol *symbol;
2206 bfd_print_symbol_type how;
2208 FILE *file = (FILE *) afile;
2210 switch (how)
2212 case bfd_print_symbol_name:
2213 fprintf (file, "%s", symbol->name);
2214 break;
2215 default:
2216 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
2218 fprintf (file, " %-5s %s",
2219 symbol->section->name,
2220 symbol->name);
2224 /* We can't map a file directly into executable code, so the
2225 size of header information is irrelevant. */
2227 static int
2228 mmo_sizeof_headers (abfd, exec)
2229 bfd *abfd ATTRIBUTE_UNUSED;
2230 boolean exec ATTRIBUTE_UNUSED;
2232 return 0;
2235 /* Write the (section-neutral) file preamble. */
2237 static boolean
2238 mmo_internal_write_header (abfd)
2239 bfd *abfd;
2241 CONST char lop_pre_bfd[] = { LOP, LOP_PRE, 1, 1};
2243 if (bfd_bwrite (lop_pre_bfd, 4, abfd) != 4)
2244 return false;
2246 /* Copy creation time of original file. */
2247 if (bfd_bwrite (abfd->tdata.mmo_data->created, 4, abfd) != 4)
2248 return false;
2250 return true;
2253 /* Write the LOP_POST record, with global register initializations.
2254 Z is the Z field of the LOP_POST, corresponding to 255 - number of
2255 registers at DATA. The Z = 255 field is filled in with the
2256 start-address. */
2258 static boolean
2259 mmo_internal_write_post (abfd, z, sec)
2260 bfd *abfd;
2261 int z;
2262 asection *sec;
2264 int i;
2265 bfd_byte buf[8];
2266 mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_POST << 16) | z);
2268 for (i = z; i < 255; i++)
2270 bfd_byte *data = mmo_get_loc (sec, i * 8, 8);
2272 if (bfd_bwrite (data, 8, abfd) != 8)
2273 return false;
2276 /* For Z == $255, we always emit the start location; supposedly Main,
2277 but we have it handy at bfd_get_start_address. If we're called with
2278 Z == 255, don't assume DATA is valid. */
2279 bfd_put_64 (abfd, bfd_get_start_address (abfd), buf);
2281 return
2282 abfd->tdata.mmo_data->have_error == false
2283 && bfd_bwrite (buf, 8, abfd) == 8;
2286 /* Translate to and from BFD flags. This is to make sure that we don't
2287 get bitten by BFD flag number changes. */
2289 static flagword
2290 mmo_sec_flags_from_bfd_flags (flags)
2291 flagword flags;
2293 flagword oflags = 0;
2295 if (flags & SEC_ALLOC)
2296 oflags |= MMO_SEC_ALLOC;
2297 if (flags & SEC_LOAD)
2298 oflags |= MMO_SEC_LOAD;
2299 if (flags & SEC_RELOC)
2300 oflags |= MMO_SEC_RELOC;
2301 if (flags & SEC_READONLY)
2302 oflags |= MMO_SEC_READONLY;
2303 if (flags & SEC_CODE)
2304 oflags |= MMO_SEC_CODE;
2305 if (flags & SEC_DATA)
2306 oflags |= MMO_SEC_DATA;
2307 if (flags & SEC_NEVER_LOAD)
2308 oflags |= MMO_SEC_NEVER_LOAD;
2309 if (flags & SEC_IS_COMMON)
2310 oflags |= MMO_SEC_IS_COMMON;
2311 if (flags & SEC_DEBUGGING)
2312 oflags |= MMO_SEC_DEBUGGING;
2314 return oflags;
2317 static flagword
2318 bfd_sec_flags_from_mmo_flags (flags)
2319 flagword flags;
2321 flagword oflags = 0;
2323 if (flags & MMO_SEC_ALLOC)
2324 oflags |= SEC_ALLOC;
2325 if (flags & MMO_SEC_LOAD)
2326 oflags |= SEC_LOAD;
2327 if (flags & MMO_SEC_RELOC)
2328 oflags |= SEC_RELOC;
2329 if (flags & MMO_SEC_READONLY)
2330 oflags |= SEC_READONLY;
2331 if (flags & MMO_SEC_CODE)
2332 oflags |= SEC_CODE;
2333 if (flags & MMO_SEC_DATA)
2334 oflags |= SEC_DATA;
2335 if (flags & MMO_SEC_NEVER_LOAD)
2336 oflags |= SEC_NEVER_LOAD;
2337 if (flags & MMO_SEC_IS_COMMON)
2338 oflags |= SEC_IS_COMMON;
2339 if (flags & MMO_SEC_DEBUGGING)
2340 oflags |= SEC_DEBUGGING;
2342 return oflags;
2345 /* Write a section. */
2347 static boolean
2348 mmo_internal_write_section (abfd, sec)
2349 bfd *abfd;
2350 asection *sec;
2352 /* We do it differently depending on what section this is:
2354 ".text": Output, prepended by information about the first source file
2355 (not yet implemented.)
2357 ".data": Output.
2359 (".MMIX.reg_contents": Not handled here.)
2361 Anything else: Output inside a lop_spec 80, in the format described
2362 above. */
2364 if (strcmp (sec->name, MMO_TEXT_SECTION_NAME) == 0)
2365 /* FIXME: Output source file name and line number. */
2366 return
2367 mmo_write_loc_chunk_list (abfd,
2368 ((struct mmo_section_data_struct *)
2369 (sec->used_by_bfd))->head);
2370 else if (strcmp (sec->name, MMO_DATA_SECTION_NAME) == 0)
2371 return
2372 mmo_write_loc_chunk_list (abfd,
2373 ((struct mmo_section_data_struct *)
2374 (sec->used_by_bfd))->head);
2375 else if (strcmp (sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
2376 /* Not handled here. */
2378 /* This would normally be an abort call since this can't happen, but
2379 we don't do that. */
2380 bfd_set_error (bfd_error_bad_value);
2381 return false;
2383 else if (strncmp (sec->name, MMIX_OTHER_SPEC_SECTION_PREFIX,
2384 strlen (MMIX_OTHER_SPEC_SECTION_PREFIX)) == 0)
2386 int n = atoi (sec->name + strlen (MMIX_OTHER_SPEC_SECTION_PREFIX));
2387 mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_SPEC << 16) | n);
2388 return
2389 abfd->tdata.mmo_data->have_error == false
2390 && mmo_write_chunk_list (abfd,
2391 ((struct mmo_section_data_struct *)
2392 (sec->used_by_bfd))->head);
2394 /* Ignore sections that are just allocated or empty; we write out
2395 _contents_ here. */
2396 else if ((bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS) != 0
2397 && sec->_raw_size != 0)
2399 /* Keep the document-comment formatted the way it is. */
2401 INODE
2402 mmo section mapping, , Symbol-table, mmo
2403 SUBSECTION
2404 mmo section mapping
2406 The implementation in BFD uses special data type 80 (decimal) to
2407 encapsulate and describe named sections, containing e.g.@: debug
2408 information. If needed, any datum in the encapsulation will be
2409 quoted using lop_quote. First comes a 32-bit word holding the
2410 number of 32-bit words containing the zero-terminated zero-padded
2411 segment name. After the name there's a 32-bit word holding flags
2412 describing the section type. Then comes a 64-bit big-endian word
2413 with the section length (in bytes), then another with the section
2414 start address. Depending on the type of section, the contents
2415 might follow, zero-padded to 32-bit boundary. For a loadable
2416 section (such as data or code), the contents might follow at some
2417 later point, not necessarily immediately, as a lop_loc with the
2418 same start address as in the section description, followed by the
2419 contents. This in effect forms a descriptor that must be emitted
2420 before the actual contents. Sections described this way must not
2421 overlap.
2423 For areas that don't have such descriptors, synthetic sections are
2424 formed by BFD. Consecutive contents in the two memory areas
2425 @samp{0x0000@dots{}00} to @samp{0x01ff@dots{}ff} and
2426 @samp{0x2000@dots{}00} to @samp{0x20ff@dots{}ff} are entered in
2427 sections named <<.text>> and <<.data>> respectively. If an area
2428 is not otherwise described, but would together with a neighboring
2429 lower area be less than @samp{0x40000000} bytes long, it is joined
2430 with the lower area and the gap is zero-filled. For other cases,
2431 a new section is formed, named <<.MMIX.sec.@var{n}>>. Here,
2432 @var{n} is a number, a running count through the mmo file,
2433 starting at 0.
2435 EXAMPLE
2436 A loadable section specified as:
2438 | .section secname,"ax"
2439 | TETRA 1,2,3,4,-1,-2009
2440 | BYTE 80
2442 and linked to address @samp{0x4}, is represented by the sequence:
2444 | 0x98080050 - lop_spec 80
2445 | 0x00000002 - two 32-bit words for the section name
2446 | 0x7365636e - "secn"
2447 | 0x616d6500 - "ame\0"
2448 | 0x00000033 - flags CODE, READONLY, LOAD, ALLOC
2449 | 0x00000000 - high 32 bits of section length
2450 | 0x0000001c - section length is 28 bytes; 6 * 4 + 1 + alignment to 32 bits
2451 | 0x00000000 - high 32 bits of section address
2452 | 0x00000004 - section address is 4
2453 | 0x98010002 - 64 bits with address of following data
2454 | 0x00000000 - high 64 bits of address
2455 | 0x00000004 - data starts at address 4
2456 | 0x00000001 - 1
2457 | 0x00000002 - 2
2458 | 0x00000003 - 3
2459 | 0x00000004 - 4
2460 | 0xffffffff - -1
2461 | 0xfffff827 - -2009
2462 | 0x50000000 - 80 as a byte, padded with zeros.
2464 Note that the lop_spec wrapping does not include the section
2465 contents. Compare this to a non-loaded section specified as:
2467 | .section thirdsec
2468 | TETRA 200001,100002
2469 | BYTE 38,40
2471 This, when linked to address @samp{0x200000000000001c}, is
2472 represented by:
2474 | 0x98080050 - lop_spec 80
2475 | 0x00000002 - two 32-bit words for the section name
2476 | 0x7365636e - "thir"
2477 | 0x616d6500 - "dsec"
2478 | 0x00000010 - flag READONLY
2479 | 0x00000000 - high 32 bits of section length
2480 | 0x0000000c - section length is 12 bytes; 2 * 4 + 2 + alignment to 32 bits
2481 | 0x20000000 - high 64 bits of address
2482 | 0x0000001c - low 64 bits of address 0x200000000000001c
2483 | 0x00030d41 - 200001
2484 | 0x000186a2 - 100002
2485 | 0x26280000 - 38, 40 as bytes, padded with zeros
2487 For the latter example, the section contents must not to appear
2488 loaded in memory, and is therefore specified as part of the
2489 special data. The address is usually unimportant but might
2490 provide information for e.g.@: the DWARF 2 debugging format. */
2492 mmo_write_tetra_raw (abfd, LOP_SPEC_SECTION);
2493 mmo_write_tetra (abfd, (strlen (sec->name) + 3) / 4);
2494 mmo_write_chunk (abfd, sec->name, strlen (sec->name));
2495 /* FIXME: We can get debug sections (.debug_line & Co.) with a
2496 section flag still having SEC_RELOC set. Investigate. This
2497 might be true for all alien sections; perhaps mmo.em should clear
2498 that flag. Might be related to weak references. */
2499 mmo_write_tetra (abfd,
2500 mmo_sec_flags_from_bfd_flags
2501 (bfd_get_section_flags (abfd, sec)));
2502 mmo_write_octa (abfd, sec->_raw_size);
2503 mmo_write_octa (abfd, bfd_get_section_vma (abfd, sec));
2505 /* Writing a LOP_LOC ends the LOP_SPEC data, and makes data actually
2506 loaded. */
2507 if (bfd_get_section_flags (abfd, sec) & SEC_LOAD)
2508 return
2509 abfd->tdata.mmo_data->have_error == false
2510 && mmo_write_loc_chunk_list (abfd,
2511 ((struct mmo_section_data_struct *)
2512 (sec->used_by_bfd))->head);
2513 return
2514 abfd->tdata.mmo_data->have_error == false
2515 && mmo_write_chunk_list (abfd,
2516 ((struct mmo_section_data_struct *)
2517 (sec->used_by_bfd))->head);
2519 return true;
2522 /* We save up all data before output. */
2524 static boolean
2525 mmo_set_section_contents (abfd, sec, location, offset, bytes_to_do)
2526 bfd *abfd ATTRIBUTE_UNUSED;
2527 sec_ptr sec;
2528 PTR location;
2529 file_ptr offset;
2530 bfd_size_type bytes_to_do;
2532 /* Iterate over diminishing chunk sizes, copying contents. */
2533 while (bytes_to_do)
2535 /* A minor song-and-dance to make sure we're not bitten by the
2536 distant possibility of the cast from bfd_vma to int making the
2537 chunk zero-sized. */
2538 int chunk_size
2539 = (int) bytes_to_do != 0 ? bytes_to_do : MMO_SEC_CONTENTS_CHUNK_SIZE;
2540 bfd_byte *loc;
2543 loc = mmo_get_loc (sec, sec->vma + offset, chunk_size);
2544 while (loc == NULL && (chunk_size /= 2) != 0);
2546 if (chunk_size == 0)
2547 return false;
2549 memcpy (loc, location, chunk_size);
2551 location += chunk_size;
2552 bytes_to_do -= chunk_size;
2553 offset += chunk_size;
2555 return true;
2558 /* Add a symbol to a trie-tree. */
2560 static boolean
2561 mmo_internal_add_3_sym (abfd, rootp, symp)
2562 bfd *abfd;
2563 struct mmo_symbol_trie *rootp;
2564 CONST struct mmo_symbol *symp;
2566 CONST char *name = symp->name;
2567 struct mmo_symbol_trie *trie = rootp;
2568 struct mmo_symbol_trie **triep = NULL;
2570 while (*name && trie != NULL)
2572 if (*name < trie->symchar)
2574 triep = &trie->left;
2575 trie = trie->left;
2577 else if (*name > trie->symchar)
2579 triep = &trie->right;
2580 trie = trie->right;
2582 else if (*name == trie->symchar)
2584 triep = &trie->middle;
2585 name++;
2587 /* Make sure "trie" points to where we should fill in the
2588 current symbol whenever we've iterated through "name". We
2589 would lose the right position if we encounter "foobar" then
2590 "foo". */
2591 if (*name)
2592 trie = trie->middle;
2596 while (*name != 0)
2598 /* Create middle branches for the rest of the characters. */
2599 trie = bfd_zalloc (abfd, sizeof (struct mmo_symbol_trie));
2600 *triep = trie;
2601 trie->symchar = *name++;
2602 triep = &trie->middle;
2605 /* We discover a duplicate symbol rather late in the process, but still;
2606 we discover it and bail out. */
2607 if (trie->sym.name != NULL)
2609 (*_bfd_error_handler)
2610 (_("%s: invalid symbol table: duplicate symbol `%s'\n"),
2611 bfd_get_filename (abfd), trie->sym.name);
2612 bfd_set_error (bfd_error_bad_value);
2613 return false;
2616 memcpy (&trie->sym, symp, sizeof *symp);
2617 return true;
2620 /* Find out the length of the serialized version of a trie in bytes. */
2622 static unsigned int
2623 mmo_internal_3_length (abfd, trie)
2624 bfd *abfd;
2625 struct mmo_symbol_trie *trie;
2627 /* First, one for the control byte. */
2628 unsigned int length = 1;
2630 if (trie == NULL)
2631 return 0;
2633 /* Add in the recursion to the left. */
2634 length += mmo_internal_3_length (abfd, trie->left);
2636 /* Add in the middle trie and the character. */
2637 length += 1 + mmo_internal_3_length (abfd, trie->middle);
2639 /* Add in the recursion to the right. */
2640 length += mmo_internal_3_length (abfd, trie->right);
2642 /* Add in bytes for the symbol (if this is an endnode). */
2643 if (trie->sym.name != NULL)
2645 unsigned int serno = trie->sym.serno;
2647 /* First what it takes to encode the value. */
2648 if (trie->sym.sym_type == mmo_reg_sym)
2649 length++;
2650 else if (trie->sym.sym_type == mmo_undef_sym)
2651 length += 2;
2652 else
2654 bfd_vma value = trie->sym.value;
2656 /* Coded in one to eight following bytes. */
2657 if (trie->sym.sym_type == mmo_data_sym)
2658 value -= (bfd_vma) 0x20 << 56;
2662 value >>= 8;
2663 length++;
2665 while (value != 0);
2668 /* Find out what it takes to encode the serial number. */
2671 serno >>= 7;
2672 length++;
2674 while (serno != 0);
2677 return length;
2680 /* Helper function for outputting the serial number of a symbol, output as
2681 a variant of leb128 (see dwarf2 documentation) which could be called
2682 beb128. Using a helper function and recursion simplifies debugging. */
2684 static void
2685 mmo_beb128_out (abfd, serno, marker)
2686 bfd *abfd;
2687 int serno;
2688 int marker;
2690 if (serno & ~0x7f)
2691 mmo_beb128_out (abfd, serno >> 7, 0);
2692 mmo_write_byte (abfd, marker | (serno & 0x7f));
2695 /* Serialize a trie. */
2697 static void
2698 mmo_internal_3_dump (abfd, trie)
2699 bfd *abfd;
2700 struct mmo_symbol_trie *trie;
2702 bfd_byte control = 0;
2704 if (trie == NULL)
2705 return;
2707 if (trie->left)
2708 control |= MMO3_LEFT;
2710 if (trie->middle)
2711 control |= MMO3_MIDDLE;
2713 if (trie->right)
2714 control |= MMO3_RIGHT;
2716 if (trie->sym.name != NULL)
2718 /* Encode the symbol type and length of value bytes. */
2719 if (trie->sym.sym_type == mmo_reg_sym)
2720 control |= MMO3_REGQUAL_BITS;
2721 else if (trie->sym.sym_type == mmo_undef_sym)
2722 control |= MMO3_UNDEF;
2723 else
2725 bfd_vma value = trie->sym.value;
2727 /* Coded in 1..8 following bytes. */
2728 if (trie->sym.sym_type == mmo_data_sym)
2730 control |= MMO3_DATA;
2731 value -= (bfd_vma) 0x20 << 56;
2736 value >>= 8;
2737 control++;
2739 while (value != 0);
2743 /* The control byte is output before recursing. */
2744 mmo_write_byte (abfd, control);
2746 mmo_internal_3_dump (abfd, trie->left);
2748 if (control & MMO3_SYMBITS)
2750 mmo_write_byte (abfd, trie->symchar);
2752 if (trie->sym.name != NULL)
2754 if (trie->sym.sym_type == mmo_reg_sym)
2755 mmo_write_byte (abfd, trie->sym.value);
2756 else if (trie->sym.sym_type == mmo_undef_sym)
2758 mmo_write_byte (abfd, 0);
2759 mmo_write_byte (abfd, 0);
2761 else
2763 bfd_vma value = trie->sym.value;
2765 bfd_byte byte_n = control & 15;
2767 /* Coded in 1..8 following bytes. Note that the value is
2768 shifted out big-endian. */
2769 if (trie->sym.sym_type == mmo_data_sym)
2771 value -= (bfd_vma) 0x20 << 56;
2772 byte_n -= 8;
2777 mmo_write_byte (abfd, (value >> ((byte_n - 1) * 8)) & 0xff);
2778 byte_n--;
2780 while (byte_n != 0);
2783 mmo_beb128_out (abfd, trie->sym.serno, 128);
2785 mmo_internal_3_dump (abfd, trie->middle);
2787 mmo_internal_3_dump (abfd, trie->right);
2790 /* Write symbols, either in mmo format or hidden in a lop_spec 80 section.
2791 Write the lop_end terminator also. */
2793 static boolean
2794 mmo_write_symbols_and_terminator (abfd)
2795 bfd *abfd;
2797 int count = bfd_get_symcount (abfd);
2798 asymbol *fakemain[2];
2799 asymbol **table;
2800 int serno = 2;
2801 struct mmo_symbol_trie root;
2802 int trie_len;
2803 int i;
2804 bfd_byte buf[4];
2806 /* Create a symbol for "Main". */
2807 asymbol *mainsym = bfd_make_empty_symbol (abfd);
2809 mainsym->flags = BSF_GLOBAL;
2810 mainsym->value = bfd_get_start_address (abfd);
2811 mainsym->name = MMIX_START_SYMBOL_NAME;
2812 mainsym->section = bfd_abs_section_ptr;
2813 fakemain[0] = mainsym;
2814 fakemain[1] = NULL;
2816 memset (&root, 0, sizeof (root));
2818 /* Make all symbols take a left turn. */
2819 root.symchar = 0xff;
2821 /* There must always be a ":Main", so we'll add one
2822 if there are no symbols. */
2823 if (count == 0)
2825 table = fakemain;
2826 count = 1;
2828 else
2829 table = bfd_get_outsymbols (abfd);
2831 for (i = 0; i < count && table[i] != NULL; i++)
2833 asymbol *s = table[i];
2835 /* It's not enough to consult bfd_is_local_label, since it does not
2836 mean "local" in the sense of linkable-and-observable-after-link.
2837 Let's just check the BSF_GLOBAL flag.
2839 Also, don't export symbols with characters not in the allowed set. */
2840 if ((s->flags & (BSF_DEBUGGING|BSF_GLOBAL)) == BSF_GLOBAL
2841 && strspn (s->name,
2842 valid_mmo_symbol_character_set) == strlen (s->name))
2844 struct mmo_symbol sym;
2845 memset (&sym, 0, sizeof (sym));
2847 sym.name = s->name;
2848 sym.value =
2849 s->value
2850 + s->section->output_section->vma
2851 + s->section->output_offset;
2853 if (bfd_is_und_section (s->section))
2854 sym.sym_type = mmo_undef_sym;
2855 else if (strcmp (s->section->name, MMO_DATA_SECTION_NAME) == 0
2856 /* The encoding of data symbols require that the "rest"
2857 of the value fits in 6 bytes, so the upper two bytes
2858 must be 0x2000. All other symbols get to be the
2859 absolute type. */
2860 && (sym.value >> 48) == 0x2000)
2861 sym.sym_type = mmo_data_sym;
2862 else if (strcmp (s->section->name, MMIX_REG_SECTION_NAME) == 0)
2863 sym.sym_type = mmo_reg_sym;
2864 else if (strcmp (s->section->name,
2865 MMIX_REG_CONTENTS_SECTION_NAME) == 0)
2867 sym.sym_type = mmo_reg_sym;
2868 sym.value /= 8;
2870 else
2871 sym.sym_type = mmo_abs_sym;
2873 /* FIXME: We assume the order of the received symbols is an
2874 ordered mapping of the serial numbers. This is not
2875 necessarily true if we e.g. objcopy a mmo file to another and
2876 there are gaps in the numbering. Note sure if this can
2877 happen. Not sure what to do. */
2878 /* Make sure Main has serial number 1; others start at 2. */
2879 if (strcmp (s->name, MMIX_START_SYMBOL_NAME) == 0)
2881 sym.serno = 1;
2883 /* Check that the value assigned to :Main is the same as the
2884 entry address. The default linker script asserts this.
2885 This is as good a place as any to check this consistency. */
2886 if (sym.value != bfd_get_start_address (abfd))
2888 /* Arbitrary buffer to hold the printable representation
2889 of a vma. */
2890 char vmas_main[40];
2891 char vmas_start[40];
2892 bfd_vma vma_start = bfd_get_start_address (abfd);
2894 sprintf_vma (vmas_main, s->value);
2895 sprintf_vma (vmas_start, vma_start);
2897 (*_bfd_error_handler)
2898 (_("%s: Bad symbol definition: `Main' set to %s rather\
2899 than the start address %s\n"),
2900 bfd_get_filename (abfd), vmas_main, vmas_start);
2901 bfd_set_error (bfd_error_bad_value);
2902 return false;
2905 else
2906 sym.serno = serno++;
2908 if (! mmo_internal_add_3_sym (abfd, &root, &sym))
2909 return false;
2913 /* Change the root node to be a ":"-prefix. */
2914 root.symchar = ':';
2915 root.middle = root.left;
2916 root.right = NULL;
2917 root.left = NULL;
2919 /* We have to find out if we can fit the whole symbol table in the mmo
2920 symtab. It would be bad to assume we can always fit it in 262144
2921 bytes. If we can't, just leave the Main symbol. */
2922 trie_len = (mmo_internal_3_length (abfd, &root) + 3)/4;
2924 if (trie_len > 0xffff)
2926 /* Test this code by using a lower limit in the test above and check
2927 that the single "Main" symbol is emitted and handled properly.
2928 There's no specific test-case. */
2929 struct mmo_symbol sym;
2931 (*_bfd_error_handler)
2932 (_("%s: warning: symbol table too large for mmo, larger than 65535\
2933 32-bit words: %d. Only `Main' will be emitted.\n"),
2934 bfd_get_filename (abfd), trie_len);
2936 memset (&sym, 0, sizeof (sym));
2937 sym.sym_type = mmo_abs_sym;
2938 sym.name = MMIX_START_SYMBOL_NAME;
2939 sym.serno = 1;
2940 sym.value = bfd_get_start_address (abfd);
2942 /* Then patch up a symbol table to be just the ":Main" symbol. */
2943 memset (&root, 0, sizeof (root));
2944 root.left = root.middle;
2945 root.symchar = 0xff;
2946 root.middle = NULL;
2947 root.right = NULL;
2949 if (! mmo_internal_add_3_sym (abfd, &root, &sym))
2950 return false;
2952 root.symchar = ':';
2953 root.middle = root.left;
2954 root.right = NULL;
2955 root.left = NULL;
2957 trie_len = (mmo_internal_3_length (abfd, &root) + 3)/4;
2960 /* Reset the written-bytes counter. */
2961 abfd->tdata.mmo_data->byte_no = 0;
2963 /* Put out the lop_stab mark. */
2964 bfd_put_32 (abfd, (LOP << 24) | (LOP_STAB << 16), buf);
2965 if (bfd_bwrite (buf, 4, abfd) != 4)
2966 return false;
2968 /* Dump out symbols. */
2969 mmo_internal_3_dump (abfd, &root);
2971 if (trie_len != (abfd->tdata.mmo_data->byte_no + 3)/4)
2973 /* I haven't seen this trig. It seems no use claiming this case
2974 isn't debugged and abort if we get here. Instead emit a
2975 diagnostic and fail "normally". */
2976 (*_bfd_error_handler)
2977 (_("%s: internal error, symbol table changed size from %d to %d\
2978 words\n"),
2979 bfd_get_filename (abfd), trie_len,
2980 (abfd->tdata.mmo_data->byte_no + 3)/4);
2981 bfd_set_error (bfd_error_bad_value);
2982 return false;
2985 /* Dump out remaining bytes in the buffer and handle I/O errors by
2986 propagating errors. */
2987 if ((abfd->tdata.mmo_data->byte_no % 4) != 0
2988 || abfd->tdata.mmo_data->have_error)
2990 memset (abfd->tdata.mmo_data->buf + (abfd->tdata.mmo_data->byte_no % 4),
2991 0, 4 - (abfd->tdata.mmo_data->byte_no % 4));
2993 if (abfd->tdata.mmo_data->have_error
2994 || bfd_bwrite (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
2995 return false;
2998 bfd_put_32 (abfd, (LOP << 24) | (LOP_END << 16) | trie_len, buf);
2999 return bfd_bwrite (buf, 4, abfd) == 4;
3002 /* Write section unless it is the register contents section. For that, we
3003 instead store the section in the supplied pointer. This function is
3004 used through bfd_map_over_sections. */
3006 static void
3007 mmo_write_section_unless_reg_contents (abfd, sec, p)
3008 bfd *abfd;
3009 asection *sec;
3010 PTR p;
3012 struct mmo_write_sec_info *infop = (struct mmo_write_sec_info *) p;
3014 if (infop->retval == false)
3015 return;
3017 if (strcmp (sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
3019 infop->reg_section = sec;
3020 return;
3023 /* Exclude the convenience register section. */
3024 if (strcmp (sec->name, MMIX_REG_SECTION_NAME) == 0)
3026 if (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
3028 /* Make sure it hasn't got contents. It seems impossible to
3029 make it carry contents, so we don't have a test-case for
3030 this. */
3031 (*_bfd_error_handler)
3032 (_("%s: internal error, internal register section %s had\
3033 contents\n"),
3034 bfd_get_filename (abfd), sec->name);
3035 bfd_set_error (bfd_error_bad_value);
3036 infop->retval = false;
3037 return;
3040 return;
3043 infop->retval = mmo_internal_write_section (abfd, sec);
3046 /* Do the actual output of a file. Assumes mmo_set_section_contents is
3047 already called. */
3049 static boolean
3050 mmo_write_object_contents (abfd)
3051 bfd *abfd;
3053 struct mmo_write_sec_info wsecinfo;
3055 /* First, there are a few words of preamble. */
3056 if (! mmo_internal_write_header (abfd))
3057 return false;
3059 wsecinfo.reg_section = NULL;
3060 wsecinfo.retval = true;
3062 bfd_map_over_sections (abfd, mmo_write_section_unless_reg_contents,
3063 (PTR) &wsecinfo);
3065 if (wsecinfo.retval == false)
3066 return false;
3068 if (wsecinfo.reg_section != NULL)
3070 asection *sec = wsecinfo.reg_section;
3071 unsigned int z = (unsigned int) (sec->vma / 8);
3073 /* Registers 0..31 must not be global. Do sanity check on the "vma"
3074 of the register contents section and check that it corresponds to
3075 the length of the section. */
3076 if (z < 32 || z >= 255 || (sec->vma & 7) != 0
3077 || sec->vma != 256 * 8 - sec->_raw_size - 8)
3079 bfd_set_error (bfd_error_bad_value);
3081 if (sec->_raw_size == 0)
3082 /* There must always be at least one such register. */
3083 (*_bfd_error_handler)
3084 (_("%s: no initialized registers; section length 0\n"),
3085 bfd_get_filename (abfd));
3086 else if (sec->vma > (256 - 32) * 8)
3087 /* Provide better error message for the case of too many
3088 global registers. */
3089 (*_bfd_error_handler)
3090 (_("%s: too many initialized registers; section length %ld\n"),
3091 bfd_get_filename (abfd),
3092 (long) sec->_raw_size);
3093 else
3094 (*_bfd_error_handler)
3095 (_("%s: invalid start address for initialized registers of\
3096 length %ld: 0x%lx%08lx\n"),
3097 bfd_get_filename (abfd),
3098 (long) sec->_raw_size,
3099 (unsigned long) (sec->vma >> 32), (unsigned long) (sec->vma));
3101 return false;
3104 if (! mmo_internal_write_post (abfd, z, sec))
3105 return false;
3107 else
3108 if (! mmo_internal_write_post (abfd, 255, NULL))
3109 return false;
3111 return mmo_write_symbols_and_terminator (abfd);
3114 /* Return the size of a NULL pointer, so we support linking in an mmo
3115 object. */
3117 static long
3118 mmo_get_reloc_upper_bound (abfd, sec)
3119 bfd *abfd ATTRIBUTE_UNUSED;
3120 asection *sec ATTRIBUTE_UNUSED;
3122 return sizeof (PTR);
3125 /* Similarly canonicalize relocs to empty, filling in the terminating NULL
3126 pointer. */
3128 long
3129 mmo_canonicalize_reloc (abfd, section, relptr, symbols)
3130 bfd *abfd ATTRIBUTE_UNUSED;
3131 sec_ptr section ATTRIBUTE_UNUSED;
3132 arelent **relptr;
3133 asymbol **symbols ATTRIBUTE_UNUSED;
3135 *relptr = NULL;
3136 return 0;
3139 /* If there's anything in particular in a mmo bfd that we want to free,
3140 make this a real function. Only do this if you see major memory
3141 thrashing; zealous free:ing will cause unwanted behavior, especially if
3142 you "free" memory allocated with "bfd_alloc", or even "bfd_release" a
3143 block allocated with "bfd_alloc"; they're really allocated from an
3144 obstack, and we don't know what was allocated there since this
3145 particular allocation. */
3147 #define mmo_close_and_cleanup _bfd_generic_close_and_cleanup
3148 #define mmo_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
3150 /* Perhaps we need to adjust this one; mmo labels (originally) without a
3151 leading ':' might more appropriately be called local. */
3152 #define mmo_bfd_is_local_label_name bfd_generic_is_local_label_name
3154 /* Is this one really used or defined by anyone? */
3155 #define mmo_get_lineno _bfd_nosymbols_get_lineno
3157 /* FIXME: We can do better on this one, if we have a dwarf2 .debug_line
3158 section or if MMO line numbers are implemented. */
3159 #define mmo_find_nearest_line _bfd_nosymbols_find_nearest_line
3160 #define mmo_make_empty_symbol _bfd_generic_make_empty_symbol
3161 #define mmo_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
3162 #define mmo_read_minisymbols _bfd_generic_read_minisymbols
3163 #define mmo_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
3165 #define mmo_get_section_contents_in_window \
3166 _bfd_generic_get_section_contents_in_window
3167 #define mmo_bfd_get_relocated_section_contents \
3168 bfd_generic_get_relocated_section_contents
3169 #define mmo_bfd_gc_sections bfd_generic_gc_sections
3170 #define mmo_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
3171 #define mmo_bfd_link_add_symbols _bfd_generic_link_add_symbols
3172 #define mmo_bfd_final_link _bfd_generic_final_link
3173 #define mmo_bfd_link_split_section _bfd_generic_link_split_section
3175 /* Strictly speaking, only MMIX uses this restricted format, but let's not
3176 stop anybody from shooting themselves in the foot. */
3177 #define mmo_set_arch_mach bfd_default_set_arch_mach
3178 #define mmo_bfd_relax_section bfd_generic_relax_section
3179 #define mmo_bfd_merge_sections bfd_generic_merge_sections
3181 /* objcopy will be upset if we return -1 from bfd_get_reloc_upper_bound by
3182 using BFD_JUMP_TABLE_RELOCS (_bfd_norelocs) rather than 0. FIXME: Most
3183 likely a bug in the _bfd_norelocs definition.
3185 On the other hand, we smuggle in an mmo object (because setting up ELF
3186 is too cumbersome) when linking (from other formats, presumably ELF) to
3187 represent the g255 entry. We need to link that object, so need to say
3188 it has no relocs. Upper bound for the size of the relocation table is
3189 the size of a NULL pointer, and we support "canonicalization" for that
3190 pointer. */
3191 #define mmo_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
3193 /* We want to copy time of creation, otherwise we'd use
3194 BFD_JUMP_TABLE_COPY (_bfd_generic). */
3195 #define mmo_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
3196 #define mmo_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
3197 #define mmo_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
3198 #define mmo_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
3199 #define mmo_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
3201 CONST bfd_target bfd_mmo_vec =
3203 "mmo", /* name */
3204 bfd_target_mmo_flavour,
3205 BFD_ENDIAN_BIG, /* target byte order */
3206 BFD_ENDIAN_BIG, /* target headers byte order */
3208 /* FIXME: Might need adjustments. */
3209 (HAS_RELOC | EXEC_P | /* object flags */
3210 HAS_LINENO | HAS_DEBUG |
3211 HAS_SYMS | HAS_LOCALS | WP_TEXT),
3213 /* FIXME: Might need adjustments. */
3214 (SEC_CODE | SEC_DATA | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
3215 | SEC_READONLY | SEC_EXCLUDE | SEC_DEBUGGING | SEC_IN_MEMORY),
3216 /* section flags */
3217 0, /* leading underscore */
3218 ' ', /* ar_pad_char */
3219 16, /* ar_max_namelen */
3220 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3221 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3222 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
3223 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3224 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3225 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
3228 _bfd_dummy_target,
3229 mmo_object_p, /* bfd_check_format */
3230 _bfd_dummy_target,
3231 _bfd_dummy_target,
3234 bfd_false,
3235 mmo_mkobject,
3236 bfd_false,
3237 bfd_false,
3239 { /* bfd_write_contents */
3240 bfd_false,
3241 mmo_write_object_contents,
3242 bfd_false,
3243 bfd_false,
3246 BFD_JUMP_TABLE_GENERIC (mmo),
3247 BFD_JUMP_TABLE_COPY (mmo),
3248 BFD_JUMP_TABLE_CORE (_bfd_nocore),
3249 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
3250 BFD_JUMP_TABLE_SYMBOLS (mmo),
3251 /* We have to provide a valid method for getting relocs, returning zero,
3252 so we can't say BFD_JUMP_TABLE_RELOCS (_bfd_norelocs). */
3253 BFD_JUMP_TABLE_RELOCS (mmo),
3254 BFD_JUMP_TABLE_WRITE (mmo),
3255 BFD_JUMP_TABLE_LINK (mmo),
3256 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
3258 NULL,
3260 NULL