1 /* BFD back-end for mmo objects (MMIX-specific object-format).
2 Copyright (C) 2001-2014 Free Software Foundation, Inc.
3 Written by Hans-Peter Nilsson (hp@bitrange.com).
4 Infrastructure and other bits originally copied from srec.c and
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
29 The mmo object format is used exclusively together with Professor
30 Donald E.@: Knuth's educational 64-bit processor MMIX. The simulator
31 @command{mmix} which is available at
32 @url{http://mmix.cs.hm.edu/src/index.html}
33 understands this format. That package also includes a combined
34 assembler and linker called @command{mmixal}. The mmo format has
35 no advantages feature-wise compared to e.g. ELF. It is a simple
36 non-relocatable object format with no support for archives or
37 debugging information, except for symbol value information and
38 line numbers (which is not yet implemented in BFD). See
39 @url{http://mmix.cs.hm.edu/} for more
40 information about MMIX. The ELF format is used for intermediate
41 object files in the BFD implementation.
43 @c We want to xref the symbol table node. A feature in "chew"
44 @c requires that "commands" do not contain spaces in the
45 @c arguments. Hence the hyphen in "Symbol-table".
49 @* mmo section mapping::
53 File layout, Symbol-table, mmo, mmo
57 The mmo file contents is not partitioned into named sections as
58 with e.g.@: ELF. Memory areas is formed by specifying the
59 location of the data that follows. Only the memory area
60 @samp{0x0000@dots{}00} to @samp{0x01ff@dots{}ff} is executable, so
61 it is used for code (and constants) and the area
62 @samp{0x2000@dots{}00} to @samp{0x20ff@dots{}ff} is used for
63 writable data. @xref{mmo section mapping}.
65 There is provision for specifying ``special data'' of 65536
66 different types. We use type 80 (decimal), arbitrarily chosen the
67 same as the ELF <<e_machine>> number for MMIX, filling it with
68 section information normally found in ELF objects. @xref{mmo
71 Contents is entered as 32-bit words, xor:ed over previous
72 contents, always zero-initialized. A word that starts with the
73 byte @samp{0x98} forms a command called a @samp{lopcode}, where
74 the next byte distinguished between the thirteen lopcodes. The
75 two remaining bytes, called the @samp{Y} and @samp{Z} fields, or
76 the @samp{YZ} field (a 16-bit big-endian number), are used for
77 various purposes different for each lopcode. As documented in
78 @url{http://mmix.cs.hm.edu/doc/mmixal.pdf},
83 0x98000001. The next word is contents, regardless of whether it
84 starts with 0x98 or not.
87 0x9801YYZZ, where @samp{Z} is 1 or 2. This is a location
88 directive, setting the location for the next data to the next
89 32-bit word (for @math{Z = 1}) or 64-bit word (for @math{Z = 2}),
90 plus @math{Y * 2^56}. Normally @samp{Y} is 0 for the text segment
91 and 2 for the data segment. Beware that the low bits of non-
92 tetrabyte-aligned values are silently discarded when being
93 automatically incremented and when storing contents (in contrast
94 to e.g. its use as current location when followed by lop_fixo
95 et al before the next possibly-quoted tetrabyte contents).
98 0x9802YYZZ. Increase the current location by @samp{YZ} bytes.
101 0x9803YYZZ, where @samp{Z} is 1 or 2. Store the current location
102 as 64 bits into the location pointed to by the next 32-bit
103 (@math{Z = 1}) or 64-bit (@math{Z = 2}) word, plus @math{Y *
107 0x9804YYZZ. @samp{YZ} is stored into the current location plus
111 0x980500ZZ. @samp{Z} is 16 or 24. A value @samp{L} derived from
112 the following 32-bit word are used in a manner similar to
113 @samp{YZ} in lop_fixr: it is xor:ed into the current location
114 minus @math{4 * L}. The first byte of the word is 0 or 1. If it
115 is 1, then @math{L = (@var{lowest 24 bits of word}) - 2^Z}, if 0,
116 then @math{L = (@var{lowest 24 bits of word})}.
119 0x9806YYZZ. @samp{Y} is the file number, @samp{Z} is count of
120 32-bit words. Set the file number to @samp{Y} and the line
121 counter to 0. The next @math{Z * 4} bytes contain the file name,
122 padded with zeros if the count is not a multiple of four. The
123 same @samp{Y} may occur multiple times, but @samp{Z} must be 0 for
124 all but the first occurrence.
127 0x9807YYZZ. @samp{YZ} is the line number. Together with
128 lop_file, it forms the source location for the next 32-bit word.
129 Note that for each non-lopcode 32-bit word, line numbers are
130 assumed incremented by one.
133 0x9808YYZZ. @samp{YZ} is the type number. Data until the next
134 lopcode other than lop_quote forms special data of type @samp{YZ}.
135 @xref{mmo section mapping}.
137 Other types than 80, (or type 80 with a content that does not
138 parse) is stored in sections named <<.MMIX.spec_data.@var{n}>>
139 where @var{n} is the @samp{YZ}-type. The flags for such a
140 sections say not to allocate or load the data. The vma is 0.
141 Contents of multiple occurrences of special data @var{n} is
142 concatenated to the data of the previous lop_spec @var{n}s. The
143 location in data or code at which the lop_spec occurred is lost.
146 0x980901ZZ. The first lopcode in a file. The @samp{Z} field forms the
147 length of header information in 32-bit words, where the first word
148 tells the time in seconds since @samp{00:00:00 GMT Jan 1 1970}.
151 0x980a00ZZ. @math{Z > 32}. This lopcode follows after all
152 content-generating lopcodes in a program. The @samp{Z} field
153 denotes the value of @samp{rG} at the beginning of the program.
154 The following @math{256 - Z} big-endian 64-bit words are loaded
155 into global registers @samp{$G} @dots{} @samp{$255}.
158 0x980b0000. The next-to-last lopcode in a program. Must follow
159 immediately after the lop_post lopcode and its data. After this
160 lopcode follows all symbols in a compressed format
161 (@pxref{Symbol-table}).
164 0x980cYYZZ. The last lopcode in a program. It must follow the
165 lop_stab lopcode and its data. The @samp{YZ} field contains the
166 number of 32-bit words of symbol table information after the
167 preceding lop_stab lopcode.
170 Note that the lopcode "fixups"; <<lop_fixr>>, <<lop_fixrx>> and
171 <<lop_fixo>> are not generated by BFD, but are handled. They are
172 generated by <<mmixal>>.
175 This trivial one-label, one-instruction file:
179 can be represented this way in mmo:
181 | 0x98090101 - lop_pre, one 32-bit word with timestamp.
183 | 0x98010002 - lop_loc, text segment, using a 64-bit address.
184 | Note that mmixal does not emit this for the file above.
185 | 0x00000000 - Address, high 32 bits.
186 | 0x00000000 - Address, low 32 bits.
187 | 0x98060002 - lop_file, 2 32-bit words for file-name.
188 | 0x74657374 - "test"
189 | 0x2e730000 - ".s\0\0"
190 | 0x98070001 - lop_line, line 1.
191 | 0x00010203 - TRAP 1,2,3
192 | 0x980a00ff - lop_post, setting $255 to 0.
195 | 0x980b0000 - lop_stab for ":Main" = 0, serial 1.
196 | 0x203a4040 @xref{Symbol-table}.
201 | 0x980c0005 - lop_end; symbol table contained five 32-bit words. */
206 #include "libiberty.h"
207 #include "elf/mmix.h"
208 #include "opcode/mmix.h"
225 #define LOP_QUOTE_NEXT ((LOP << 24) | (LOP_QUOTE << 16) | 1)
226 #define SPEC_DATA_SECTION 80
227 #define LOP_SPEC_SECTION \
228 ((LOP << 24) | (LOP_SPEC << 16) | SPEC_DATA_SECTION)
230 /* Must be a power of two. If you change this to be >= 64k, you need a
231 new test-case; the ld test b-loc64k.d touches chunk-size problem areas. */
232 #define MMO_SEC_CONTENTS_CHUNK_SIZE (1 << 15)
234 /* An arbitrary number for the maximum length section name size. */
235 #define MAX_SECTION_NAME_SIZE (1024 * 1024)
237 /* A quite arbitrary number for the maximum length section size. */
238 #define MAX_ARTIFICIAL_SECTION_SIZE (1024 * 1024 * 1024)
240 #define MMO3_WCHAR 0x80
241 #define MMO3_LEFT 0x40
242 #define MMO3_MIDDLE 0x20
243 #define MMO3_RIGHT 0x10
244 #define MMO3_TYPEBITS 0xf
245 #define MMO3_REGQUAL_BITS 0xf
248 #define MMO3_SYMBITS 0x2f
250 /* Put these everywhere in new code. */
251 #define FATAL_DEBUG \
252 _bfd_abort (__FILE__, __LINE__, \
253 "Internal: Non-debugged code (test-case missing)")
255 #define BAD_CASE(x) \
256 _bfd_abort (__FILE__, __LINE__, \
259 enum mmo_sym_type
{ mmo_reg_sym
, mmo_undef_sym
, mmo_data_sym
, mmo_abs_sym
};
261 /* When scanning the mmo file, a linked list of mmo_symbol
262 structures is built to represent the symbol table (if there is
267 struct mmo_symbol
*next
;
270 enum mmo_sym_type sym_type
;
274 struct mmo_data_list_struct
276 struct mmo_data_list_struct
*next
;
279 bfd_size_type allocated_size
;
283 typedef struct mmo_data_list_struct mmo_data_list_type
;
285 struct mmo_symbol_trie
287 struct mmo_symbol_trie
*left
;
288 struct mmo_symbol_trie
*right
;
289 struct mmo_symbol_trie
*middle
;
293 /* A zero name means there's nothing here. */
294 struct mmo_symbol sym
;
297 /* The mmo tdata information. */
299 struct mmo_data_struct
301 struct mmo_symbol
*symbols
;
302 struct mmo_symbol
*symtail
;
305 /* File representation of time (NULL) when this file was created. */
308 /* When we're reading bytes recursively, check this occasionally.
309 Also holds write errors. */
310 bfd_boolean have_error
;
312 /* Max symbol length that may appear in the lop_stab table. Note that
313 this table might just hold a subset of symbols for not-really large
314 programs, as it can only be 65536 * 4 bytes large. */
315 int max_symbol_length
;
317 /* Here's the symbol we build in lop_stab. */
318 char *lop_stab_symbol
;
320 /* Index into lop_stab_symbol for the next character when parsing the
321 symbol information. */
324 /* When creating arbitrary sections, we need to count section numbers. */
327 /* When writing or reading byte-wise, we need to count the bytes
328 within a 32-bit word. */
331 /* We also need a buffer to hold the bytes we count reading or writing. */
335 typedef struct mmo_data_struct tdata_type
;
337 struct mmo_section_data_struct
339 mmo_data_list_type
*head
;
340 mmo_data_list_type
*tail
;
343 #define mmo_section_data(sec) \
344 ((struct mmo_section_data_struct *) (sec)->used_by_bfd)
346 /* These structures are used in bfd_map_over_sections constructs. */
348 /* Used when writing out sections; all but the register contents section
349 which is stored in reg_section. */
350 struct mmo_write_sec_info
352 asection
*reg_section
;
356 /* Used when trying to find a section corresponding to addr. */
357 struct mmo_find_sec_info
363 static bfd_boolean
mmo_bfd_copy_private_bfd_data (bfd
*, bfd
*);
364 static void mmo_write_section_unless_reg_contents (bfd
*, asection
*, void *);
365 static void mmo_find_sec_w_addr (bfd
*, asection
*, void *);
366 static void mmo_find_sec_w_addr_grow (bfd
*, asection
*, void *);
367 static asection
*mmo_make_section (bfd
*, const char *);
368 static void mmo_get_symbol_info (bfd
*, asymbol
*, symbol_info
*);
369 static void mmo_print_symbol (bfd
*, void *, asymbol
*,
370 bfd_print_symbol_type
);
371 static void mmo_init (void);
372 static bfd_boolean
mmo_mkobject (bfd
*);
373 static bfd_boolean
mmo_scan (bfd
*);
374 static asection
*mmo_decide_section (bfd
*, bfd_vma
);
375 static asection
*mmo_get_generic_spec_data_section (bfd
*, int);
376 static asection
*mmo_get_spec_section (bfd
*, int);
377 static INLINE bfd_byte
*mmo_get_loc (asection
*, bfd_vma
, int);
378 static void mmo_xore_64 (asection
*, bfd_vma vma
, bfd_vma value
);
379 static void mmo_xore_32 (asection
*, bfd_vma vma
, unsigned int);
380 static void mmo_xore_16 (asection
*, bfd_vma vma
, unsigned int);
381 static const bfd_target
*mmo_object_p (bfd
*);
382 static void mmo_map_set_sizes (bfd
*, asection
*, void *);
383 static bfd_boolean
mmo_get_symbols (bfd
*);
384 static bfd_boolean
mmo_create_symbol (bfd
*, const char *, bfd_vma
,
385 enum mmo_sym_type
, unsigned int);
386 static bfd_boolean
mmo_get_section_contents (bfd
*, asection
*, void *,
387 file_ptr
, bfd_size_type
);
388 static long mmo_get_symtab_upper_bound (bfd
*);
389 static long mmo_canonicalize_symtab (bfd
*, asymbol
**);
390 static void mmo_get_symbol_info (bfd
*, asymbol
*, symbol_info
*);
391 static void mmo_print_symbol (bfd
*, void *, asymbol
*,
392 bfd_print_symbol_type
);
393 static bfd_boolean
mmo_set_section_contents (bfd
*, sec_ptr
, const void *,
394 file_ptr
, bfd_size_type
);
395 static int mmo_sizeof_headers (bfd
*, struct bfd_link_info
*);
396 static bfd_boolean
mmo_internal_write_header (bfd
*);
397 static bfd_boolean
mmo_internal_write_post (bfd
*, int, asection
*);
398 static bfd_boolean
mmo_internal_add_3_sym (bfd
*, struct mmo_symbol_trie
*,
399 const struct mmo_symbol
*);
400 static unsigned int mmo_internal_3_length (bfd
*, struct mmo_symbol_trie
*);
401 static void mmo_internal_3_dump (bfd
*, struct mmo_symbol_trie
*);
402 static void mmo_beb128_out (bfd
*, int, int);
403 static bfd_boolean
mmo_internal_write_section (bfd
*, asection
*);
404 static void mmo_write_tetra (bfd
*, unsigned int);
405 static void mmo_write_tetra_raw (bfd
*, unsigned int);
406 static void mmo_write_octa (bfd
*, bfd_vma
);
407 static void mmo_write_octa_raw (bfd
*, bfd_vma
);
408 static bfd_boolean
mmo_write_chunk (bfd
*, const bfd_byte
*, unsigned int);
409 static bfd_boolean
mmo_flush_chunk (bfd
*);
410 static bfd_boolean
mmo_write_loc_chunk (bfd
*, bfd_vma
, const bfd_byte
*,
411 unsigned int, bfd_vma
*);
412 static bfd_boolean
mmo_write_chunk_list (bfd
*, mmo_data_list_type
*);
413 static bfd_boolean
mmo_write_loc_chunk_list (bfd
*, mmo_data_list_type
*);
414 static bfd_boolean
mmo_write_symbols_and_terminator (bfd
*);
415 static flagword
mmo_sec_flags_from_bfd_flags (flagword
);
416 static flagword
bfd_sec_flags_from_mmo_flags (flagword
);
417 static bfd_byte
mmo_get_byte (bfd
*);
418 static void mmo_write_byte (bfd
*, bfd_byte
);
419 static bfd_boolean
mmo_new_section_hook (bfd
*, asection
*);
420 static int mmo_sort_mmo_symbols (const void *, const void *);
421 static bfd_boolean
mmo_write_object_contents (bfd
*);
422 static bfd_boolean
mmo_write_section_description (bfd
*, asection
*);
423 static bfd_boolean
mmo_has_leading_or_trailing_zero_tetra_p (bfd
*,
426 /* Global "const" variables initialized once. Must not depend on
427 particular input or caller; put such things into the bfd or elsewhere.
428 Look ma, no static per-invocation data! */
431 char valid_mmo_symbol_character_set
[/* A-Z a-z (we assume consecutive
432 codes; sorry EBCDIC:ers!). */
433 + 'Z' - 'A' + 1 + 'z' - 'a' + 1
438 /* Codes higher than 126. */
444 /* Get section SECNAME or create one if it doesn't exist. When creating
445 one, new memory for the name is allocated. */
448 mmo_make_section (bfd
*abfd
, const char *secname
)
450 asection
*sec
= bfd_get_section_by_name (abfd
, secname
);
454 char *newsecname
= strdup (secname
);
456 if (newsecname
== NULL
)
458 (*_bfd_error_handler
)
459 (_("%s: No core to allocate section name %s\n"),
460 bfd_get_filename (abfd
), secname
);
461 bfd_set_error (bfd_error_system_call
);
464 sec
= bfd_make_section (abfd
, newsecname
);
470 /* Nothing to do, but keep as a placeholder if we need it.
471 Note that state that might differ between bfd:s must not be initialized
472 here, nor must it be static. Add it to tdata information instead. */
477 static bfd_boolean inited
= FALSE
;
480 static const char letters
[]
481 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:_";
487 /* Fill in the set of valid symbol characters. */
488 strcpy (valid_mmo_symbol_character_set
, letters
);
489 i
= strlen (letters
);
491 for (j
= 126; j
< 256; j
++)
492 valid_mmo_symbol_character_set
[i
++] = j
;
495 /* Check whether an existing file is an mmo file. */
497 static const bfd_target
*
498 mmo_object_p (bfd
*abfd
)
505 if (bfd_stat (abfd
, &statbuf
) < 0
506 || bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0
507 || bfd_bread (b
, 4, abfd
) != 4)
510 /* All mmo files are a multiple of four bytes long.
511 Only recognize version one. */
512 if ((statbuf
.st_size
% 4) != 0
513 || b
[0] != LOP
|| b
[1] != LOP_PRE
|| b
[2] != 1)
516 /* Get the last 32-bit word. */
517 if (bfd_seek (abfd
, (file_ptr
) statbuf
.st_size
- 4, SEEK_SET
) != 0
518 || bfd_bread (b
, 4, abfd
) != 4)
521 /* Check if the file ends in a lop_end lopcode. */
522 if (b
[0] != LOP
|| b
[1] != LOP_END
|| ! mmo_mkobject (abfd
))
525 /* Compute an upper bound on the max symbol length. Not really
526 important as all of the symbol information can only be 256k. */
527 abfd
->tdata
.mmo_data
->max_symbol_length
= (b
[2] * 256 + b
[3]) * 4;
528 abfd
->tdata
.mmo_data
->lop_stab_symbol
529 = bfd_malloc (abfd
->tdata
.mmo_data
->max_symbol_length
+ 1);
531 if (abfd
->tdata
.mmo_data
->lop_stab_symbol
== NULL
)
533 (*_bfd_error_handler
)
534 (_("%s: No core to allocate a symbol %d bytes long\n"),
535 bfd_get_filename (abfd
), abfd
->tdata
.mmo_data
->max_symbol_length
);
539 /* Read in everything. */
540 if (! mmo_scan (abfd
))
541 goto bad_format_free
;
543 if (abfd
->symcount
> 0)
544 abfd
->flags
|= HAS_SYMS
;
546 /* You'll have to tweak this if you want to use this format for other
547 arches (not recommended due to its small-size limitations). Look at
548 the ELF format for how to make it target-generic. */
549 if (! bfd_default_set_arch_mach (abfd
, bfd_arch_mmix
, 0))
550 goto bad_format_free
;
555 free (abfd
->tdata
.mmo_data
->lop_stab_symbol
);
557 bfd_set_error (bfd_error_wrong_format
);
562 /* Set up the mmo tdata information. */
565 mmo_mkobject (bfd
*abfd
)
569 if (abfd
->tdata
.mmo_data
== NULL
)
573 /* All fields are zero-initialized, so we don't have to explicitly
575 tdata_type
*tdata
= (tdata_type
*) bfd_zmalloc (sizeof (tdata_type
));
579 created
= time (NULL
);
580 bfd_put_32 (abfd
, created
, tdata
->created
);
582 abfd
->tdata
.mmo_data
= tdata
;
589 mmo_bfd_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
591 if (bfd_get_flavour (ibfd
) != bfd_target_mmo_flavour
592 || bfd_get_flavour (obfd
) != bfd_target_mmo_flavour
)
595 /* Copy the time the copied-from file was created. If people want the
596 time the file was last *modified*, they have that in the normal file
598 memcpy (obfd
->tdata
.mmo_data
->created
, ibfd
->tdata
.mmo_data
->created
,
599 sizeof (obfd
->tdata
.mmo_data
->created
));
603 /* Helper functions for mmo_decide_section, used through
604 bfd_map_over_sections. */
607 mmo_find_sec_w_addr (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *p
)
609 struct mmo_find_sec_info
*infop
= (struct mmo_find_sec_info
*) p
;
610 bfd_vma vma
= bfd_get_section_vma (abfd
, sec
);
612 /* Ignore sections that aren't loaded. */
613 if ((bfd_get_section_flags (abfd
, sec
) & (SEC_LOAD
| SEC_ALLOC
))
614 != (SEC_LOAD
| SEC_ALLOC
))
617 if (infop
->addr
>= vma
&& infop
->addr
< vma
+ sec
->size
)
622 mmo_find_sec_w_addr_grow (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *p
)
624 struct mmo_find_sec_info
*infop
= (struct mmo_find_sec_info
*) p
;
625 bfd_vma vma
= bfd_get_section_vma (abfd
, sec
);
627 /* Ignore sections that aren't loaded. */
628 if ((bfd_get_section_flags (abfd
, sec
) & (SEC_LOAD
| SEC_ALLOC
))
629 != (SEC_LOAD
| SEC_ALLOC
))
632 if (infop
->addr
>= vma
&& infop
->addr
< vma
+ MAX_ARTIFICIAL_SECTION_SIZE
)
636 /* Find a section that corresponds to a VMA. Automatically create .text
637 or .data and set current section to it, depending on what vma. If we
638 can't deduce a section, make one up as ".MMIX.sec.N", where N is an
639 increasing number. */
642 mmo_decide_section (bfd
*abfd
, bfd_vma vma
)
644 asection
*sec
= NULL
;
645 char sec_name
[sizeof (".MMIX.sec.") + 20];
646 struct mmo_find_sec_info info
;
651 /* First see if there's a section that would match exactly. */
652 bfd_map_over_sections (abfd
, mmo_find_sec_w_addr
, &info
);
654 if (info
.sec
!= NULL
)
657 /* If there's no such section, try and expand one of the existing ones,
658 up to a limit. Make sure we have .text and .data before we try that;
659 create them corresponding to expected addresses and set flags to make
660 them match the "loaded and with contents" expectation. */
661 if ((vma
>> 56) == 0)
663 sec
= bfd_make_section_old_way (abfd
, MMO_TEXT_SECTION_NAME
);
668 if (! sec
->user_set_vma
&& ! bfd_set_section_vma (abfd
, sec
, vma
))
671 if (! bfd_set_section_flags (abfd
, sec
,
672 bfd_get_section_flags (abfd
, sec
)
673 | SEC_CODE
| SEC_LOAD
| SEC_ALLOC
))
676 else if ((vma
>> 56) == 0x20)
678 sec
= bfd_make_section_old_way (abfd
, MMO_DATA_SECTION_NAME
);
683 if (! sec
->user_set_vma
&& ! bfd_set_section_vma (abfd
, sec
, vma
))
686 if (! bfd_set_section_flags (abfd
, sec
,
687 bfd_get_section_flags (abfd
, sec
)
688 | SEC_LOAD
| SEC_ALLOC
))
692 bfd_map_over_sections (abfd
, mmo_find_sec_w_addr_grow
, &info
);
694 if (info
.sec
!= NULL
)
697 /* If there's still no suitable section, make a new one. */
698 sprintf (sec_name
, ".MMIX.sec.%d", abfd
->tdata
.mmo_data
->sec_no
++);
699 sec
= mmo_make_section (abfd
, sec_name
);
701 if (! sec
->user_set_vma
&& ! bfd_set_section_vma (abfd
, sec
, vma
))
704 if (! bfd_set_section_flags (abfd
, sec
,
705 bfd_get_section_flags (abfd
, sec
)
706 | SEC_LOAD
| SEC_ALLOC
))
711 /* Xor in a 64-bit value VALUE at VMA. */
714 mmo_xore_64 (asection
*sec
, bfd_vma vma
, bfd_vma value
)
716 bfd_byte
*loc
= mmo_get_loc (sec
, vma
, 8);
717 bfd_vma prev
= bfd_get_64 (sec
->owner
, loc
);
720 bfd_put_64 (sec
->owner
, value
, loc
);
723 /* Xor in a 32-bit value VALUE at VMA. */
726 mmo_xore_32 (asection
*sec
, bfd_vma vma
, unsigned int value
)
728 bfd_byte
*loc
= mmo_get_loc (sec
, vma
, 4);
729 unsigned int prev
= bfd_get_32 (sec
->owner
, loc
);
732 bfd_put_32 (sec
->owner
, value
, loc
);
735 /* Xor in a 16-bit value VALUE at VMA. */
738 mmo_xore_16 (asection
*sec
, bfd_vma vma
, unsigned int value
)
740 bfd_byte
*loc
= mmo_get_loc (sec
, vma
, 2);
741 unsigned int prev
= bfd_get_16 (sec
->owner
, loc
);
744 bfd_put_16 (sec
->owner
, value
, loc
);
747 /* Write a 32-bit word to output file, no lop_quote generated. */
750 mmo_write_tetra_raw (bfd
*abfd
, unsigned int value
)
754 bfd_put_32 (abfd
, value
, buf
);
756 if (bfd_bwrite (buf
, 4, abfd
) != 4)
757 abfd
->tdata
.mmo_data
->have_error
= TRUE
;
760 /* Write a 32-bit word to output file; lop_quote if necessary. */
763 mmo_write_tetra (bfd
*abfd
, unsigned int value
)
765 if (((value
>> 24) & 0xff) == LOP
)
766 mmo_write_tetra_raw (abfd
, LOP_QUOTE_NEXT
);
768 mmo_write_tetra_raw (abfd
, value
);
771 /* Write a 64-bit word to output file, perhaps with lop_quoting. */
774 mmo_write_octa (bfd
*abfd
, bfd_vma value
)
776 mmo_write_tetra (abfd
, (unsigned int) (value
>> 32));
777 mmo_write_tetra (abfd
, (unsigned int) value
);
780 /* Write a 64-bit word to output file, without lop_quoting. */
783 mmo_write_octa_raw (bfd
*abfd
, bfd_vma value
)
785 mmo_write_tetra_raw (abfd
, (unsigned int) (value
>> 32));
786 mmo_write_tetra_raw (abfd
, (unsigned int) value
);
789 /* Write quoted contents. Intended to be called multiple times in
790 sequence, followed by a call to mmo_flush_chunk. */
792 static INLINE bfd_boolean
793 mmo_write_chunk (bfd
*abfd
, const bfd_byte
*loc
, unsigned int len
)
795 bfd_boolean retval
= TRUE
;
796 struct mmo_data_struct
*mmop
= abfd
->tdata
.mmo_data
;
798 /* Fill up a tetra from bytes remaining from a previous chunk. */
799 if (mmop
->byte_no
!= 0)
801 while (mmop
->byte_no
< 4 && len
!= 0)
803 mmop
->buf
[mmop
->byte_no
++] = *loc
++;
807 if (mmop
->byte_no
== 4)
809 mmo_write_tetra (abfd
, bfd_get_32 (abfd
, mmop
->buf
));
817 mmo_write_tetra_raw (abfd
, LOP_QUOTE_NEXT
);
820 && ! mmop
->have_error
821 && 4 == bfd_bwrite (loc
, 4, abfd
));
829 /* We must have flushed a previous remainder if we get one from
831 BFD_ASSERT (mmop
->byte_no
== 0);
832 memcpy (mmop
->buf
, loc
, len
);
837 mmop
->have_error
= TRUE
;
841 /* Flush remaining bytes, from a previous mmo_write_chunk, zero-padded to
844 static INLINE bfd_boolean
845 mmo_flush_chunk (bfd
*abfd
)
847 if (abfd
->tdata
.mmo_data
->byte_no
!= 0)
849 memset (abfd
->tdata
.mmo_data
->buf
+ abfd
->tdata
.mmo_data
->byte_no
,
850 0, 4 - abfd
->tdata
.mmo_data
->byte_no
);
851 mmo_write_tetra (abfd
,
852 bfd_get_32 (abfd
, abfd
->tdata
.mmo_data
->buf
));
853 abfd
->tdata
.mmo_data
->byte_no
= 0;
856 return ! abfd
->tdata
.mmo_data
->have_error
;
859 /* Same, but from a list. */
861 static INLINE bfd_boolean
862 mmo_write_chunk_list (bfd
*abfd
, mmo_data_list_type
*datap
)
864 for (; datap
!= NULL
; datap
= datap
->next
)
865 if (! mmo_write_chunk (abfd
, datap
->data
, datap
->size
))
868 return mmo_flush_chunk (abfd
);
871 /* Write a lop_loc and some contents. A caller needs to call
872 mmo_flush_chunk after calling this function. The location is only
873 output if different than *LAST_VMAP, which is updated after this call. */
876 mmo_write_loc_chunk (bfd
*abfd
, bfd_vma vma
, const bfd_byte
*loc
,
877 unsigned int len
, bfd_vma
*last_vmap
)
879 /* Find an initial and trailing section of zero (aligned) tetras; we don't
880 need to write out zeros. FIXME: When we do this, we should emit
881 section size and address specifiers, else objcopy can't always perform
882 an identity translation. Only do this if we *don't* have left-over
883 data from a previous write (and will not add any) or else the vma of
884 this chunk is *not* the next address, because then data isn't
885 tetrabyte-aligned and we're concatenating to that left-over data. */
888 && (abfd
->tdata
.mmo_data
->byte_no
== 0 || vma
!= *last_vmap
))
890 while (len
> 4 && bfd_get_32 (abfd
, loc
) == 0)
898 while (len
> 4 && bfd_get_32 (abfd
, loc
+ len
- 4) == 0)
902 /* Only write out the location if it's different than the one the caller
903 (supposedly) previously handled, accounting for omitted leading zeros. */
904 if (vma
!= *last_vmap
)
906 /* We might be in the middle of a sequence. */
907 mmo_flush_chunk (abfd
);
909 /* This should not happen during normal usage, but can presumably
910 happen with an erroneous linker-script, so handle gracefully.
911 Avoid Knuth-specific terms in the message, such as "tetrabyte".
912 Note that this function will get non-4-multiple lengths and
913 unaligned vmas but those come in tuples (mostly pairs) and are
914 continuous (i.e. the if-condition above false) and they are
915 group-wise aligned. */
918 (*_bfd_error_handler
)
919 (_("%s: attempt to emit contents at non-multiple-of-4 address 0x%lx\n"),
920 bfd_get_filename (abfd
), (unsigned long) vma
);
921 bfd_set_error (bfd_error_bad_value
);
925 /* We always write the location as 64 bits; no use saving bytes
927 mmo_write_tetra_raw (abfd
, (LOP
<< 24) | (LOP_LOC
<< 16) | 2);
928 mmo_write_octa_raw (abfd
, vma
);
931 /* Update to reflect end of this chunk, with trailing zeros omitted. */
932 *last_vmap
= vma
+ len
;
934 return (! abfd
->tdata
.mmo_data
->have_error
935 && mmo_write_chunk (abfd
, loc
, len
));
938 /* Same, but from a list. */
940 static INLINE bfd_boolean
941 mmo_write_loc_chunk_list (bfd
*abfd
, mmo_data_list_type
*datap
)
943 /* Get an address different than the address of the first chunk. */
944 bfd_vma last_vma
= datap
? datap
->where
- 1 : 0;
946 for (; datap
!= NULL
; datap
= datap
->next
)
947 if (! mmo_write_loc_chunk (abfd
, datap
->where
, datap
->data
, datap
->size
,
951 return mmo_flush_chunk (abfd
);
954 /* Make a .MMIX.spec_data.N section. */
957 mmo_get_generic_spec_data_section (bfd
*abfd
, int spec_data_number
)
960 char secname
[sizeof (MMIX_OTHER_SPEC_SECTION_PREFIX
) + 20]
961 = MMIX_OTHER_SPEC_SECTION_PREFIX
;
963 sprintf (secname
+ strlen (MMIX_OTHER_SPEC_SECTION_PREFIX
),
964 "%d", spec_data_number
);
966 sec
= mmo_make_section (abfd
, secname
);
971 /* Make a special section for SPEC_DATA_NUMBER. If it is the one we use
972 ourselves, parse some of its data to get at the section name. */
975 mmo_get_spec_section (bfd
*abfd
, int spec_data_number
)
980 unsigned int secname_length
;
982 bfd_vma section_length
;
984 mmo_data_list_type
*loc
;
988 /* If this isn't the "special" special data, then make a placeholder
990 if (spec_data_number
!= SPEC_DATA_SECTION
)
991 return mmo_get_generic_spec_data_section (abfd
, spec_data_number
);
993 /* Seek back to this position if there was a format error. */
994 orig_pos
= bfd_tell (abfd
);
996 /* Read the length (in 32-bit words). */
997 if (bfd_bread (buf
, 4, abfd
) != 4)
1002 if (buf
[1] != LOP_QUOTE
)
1005 if (bfd_bread (buf
, 4, abfd
) != 4)
1009 /* We don't care to keep the name length accurate. It's
1011 secname_length
= bfd_get_32 (abfd
, buf
) * 4;
1013 /* Check section name length for sanity. */
1014 if (secname_length
> MAX_SECTION_NAME_SIZE
)
1017 /* This should be free'd regardless if a section is created. */
1018 secname
= bfd_malloc (secname_length
+ 1);
1019 secname
[secname_length
] = 0;
1021 for (i
= 0; i
< secname_length
/ 4; i
++)
1023 if (bfd_bread (secname
+ i
* 4, 4, abfd
) != 4)
1024 goto format_error_free
;
1026 if (secname
[i
* 4] == (char) LOP
)
1028 /* A bit of overkill, but we handle char 0x98 in a section name,
1029 and recognize misparsing. */
1030 if (secname
[i
* 4 + 1] != LOP_QUOTE
1031 || bfd_bread (secname
+ i
* 4, 4, abfd
) != 4)
1032 /* Whoops. We thought this was a name, and now we found a
1033 non-lop_quote lopcode before we parsed the whole length of
1034 the name. Signal end-of-file in the same manner. */
1035 goto format_error_free
;
1039 /* Get the section flags. */
1040 if (bfd_bread (buf
, 4, abfd
) != 4
1042 && (buf
[1] != LOP_QUOTE
|| bfd_bread (buf
, 4, abfd
) != 4)))
1043 goto format_error_free
;
1045 flags
= bfd_get_32 (abfd
, buf
);
1047 /* Get the section length. */
1048 if (bfd_bread (buf
, 4, abfd
) != 4
1050 && (buf
[1] != LOP_QUOTE
|| bfd_bread (buf
, 4, abfd
) != 4)))
1051 goto format_error_free
;
1053 section_length
= (bfd_vma
) bfd_get_32 (abfd
, buf
) << 32;
1055 /* That's the first, high-part. Now get the low part. */
1057 if (bfd_bread (buf
, 4, abfd
) != 4
1059 && (buf
[1] != LOP_QUOTE
|| bfd_bread (buf
, 4, abfd
) != 4)))
1060 goto format_error_free
;
1062 section_length
|= (bfd_vma
) bfd_get_32 (abfd
, buf
);
1064 /* Check the section length for sanity. */
1065 if (section_length
> MAX_ARTIFICIAL_SECTION_SIZE
)
1066 goto format_error_free
;
1068 /* Get the section VMA. */
1069 if (bfd_bread (buf
, 4, abfd
) != 4
1071 && (buf
[1] != LOP_QUOTE
|| bfd_bread (buf
, 4, abfd
) != 4)))
1072 goto format_error_free
;
1074 section_vma
= (bfd_vma
) bfd_get_32 (abfd
, buf
) << 32;
1076 /* That's the first, high-part. Now get the low part. */
1077 if (bfd_bread (buf
, 4, abfd
) != 4
1079 && (buf
[1] != LOP_QUOTE
|| bfd_bread (buf
, 4, abfd
) != 4)))
1080 goto format_error_free
;
1082 section_vma
|= (bfd_vma
) bfd_get_32 (abfd
, buf
);
1084 sec
= mmo_make_section (abfd
, secname
);
1089 /* We allocate a buffer here for the advertised size, with head room for
1090 tetrabyte alignment. */
1091 loc
= bfd_zmalloc (section_length
+ 3
1092 + sizeof (struct mmo_data_list_struct
));
1096 /* Use a TETRA-rounded size for the allocated buffer; we set the
1097 "visible" section size below. */
1098 loc
->size
= (section_length
+ 3) & ~3;
1100 /* Add in the section flags we found to those bfd entered during this
1101 process and set the contents. */
1102 if (! bfd_set_section_flags (abfd
, sec
,
1103 bfd_sec_flags_from_mmo_flags (flags
)
1104 | bfd_get_section_flags (abfd
, sec
)
1105 | (section_length
!= 0 ? SEC_HAS_CONTENTS
: 0))
1106 || ! bfd_set_section_size (abfd
, sec
, sec
->size
+ section_length
)
1107 /* Set VMA only for the first occurrence. */
1108 || (! sec
->user_set_vma
1109 && ! bfd_set_section_vma (abfd
, sec
, section_vma
)))
1111 /* If we get an error for any of the calls above, signal more than
1112 just a format error for the spec section. */
1117 if (mmo_section_data (sec
)->tail
!= NULL
)
1118 mmo_section_data (sec
)->tail
->next
= loc
;
1120 mmo_section_data (sec
)->head
= loc
;
1121 mmo_section_data (sec
)->tail
= loc
;
1122 loc
->where
= section_vma
;
1129 if (bfd_seek (abfd
, orig_pos
, SEEK_SET
) != 0)
1132 return mmo_get_generic_spec_data_section (abfd
, spec_data_number
);
1135 /* Read a byte, but read from file in multiples of 32-bit words. */
1138 mmo_get_byte (bfd
*abfd
)
1142 if (abfd
->tdata
.mmo_data
->byte_no
== 0)
1144 if (! abfd
->tdata
.mmo_data
->have_error
1145 && bfd_bread (abfd
->tdata
.mmo_data
->buf
, 4, abfd
) != 4)
1147 abfd
->tdata
.mmo_data
->have_error
= TRUE
;
1149 /* A value somewhat safe against tripping on some inconsistency
1150 when mopping up after this error. */
1155 retval
= abfd
->tdata
.mmo_data
->buf
[abfd
->tdata
.mmo_data
->byte_no
];
1156 abfd
->tdata
.mmo_data
->byte_no
= (abfd
->tdata
.mmo_data
->byte_no
+ 1) % 4;
1161 /* Write a byte, in multiples of 32-bit words. */
1164 mmo_write_byte (bfd
*abfd
, bfd_byte value
)
1166 abfd
->tdata
.mmo_data
->buf
[(abfd
->tdata
.mmo_data
->byte_no
++ % 4)] = value
;
1167 if ((abfd
->tdata
.mmo_data
->byte_no
% 4) == 0)
1169 if (! abfd
->tdata
.mmo_data
->have_error
1170 && bfd_bwrite (abfd
->tdata
.mmo_data
->buf
, 4, abfd
) != 4)
1171 abfd
->tdata
.mmo_data
->have_error
= TRUE
;
1175 /* Create a symbol. */
1178 mmo_create_symbol (bfd
*abfd
, const char *symname
, bfd_vma addr
, enum
1179 mmo_sym_type sym_type
, unsigned int serno
)
1181 struct mmo_symbol
*n
;
1183 n
= (struct mmo_symbol
*) bfd_alloc (abfd
, sizeof (struct mmo_symbol
));
1187 n
->name
= bfd_alloc (abfd
, strlen (symname
) + 1);
1188 if (n
->name
== NULL
)
1191 strcpy (n
->name
, symname
);
1194 n
->sym_type
= sym_type
;
1197 if (abfd
->tdata
.mmo_data
->symbols
== NULL
)
1198 abfd
->tdata
.mmo_data
->symbols
= n
;
1200 abfd
->tdata
.mmo_data
->symtail
->next
= n
;
1201 abfd
->tdata
.mmo_data
->symtail
= n
;
1206 /* Check that :Main equals the last octa of the .MMIX.reg_contents
1207 section, as it's the one place we're sure to pass when reading a mmo
1208 object. For written objects, we do it while setting the symbol
1210 if (strcmp (symname
, MMIX_START_SYMBOL_NAME
) == 0
1211 && bfd_get_start_address (abfd
) != addr
)
1213 (*_bfd_error_handler
)
1214 (_("%s: invalid mmo file: initialization value for $255 is not `Main'\n"),
1215 bfd_get_filename (abfd
));
1216 bfd_set_error (bfd_error_bad_value
);
1223 /* Read in symbols. */
1226 mmo_get_symbols (bfd
*abfd
)
1230 Symbol-table, mmo section mapping, File layout, mmo
1234 From mmixal.w (or really, the generated mmixal.tex) in the
1235 MMIXware package which also contains the @command{mmix} simulator:
1236 ``Symbols are stored and retrieved by means of a @samp{ternary
1237 search trie}, following ideas of Bentley and Sedgewick. (See
1238 ACM--SIAM Symp.@: on Discrete Algorithms @samp{8} (1997), 360--369;
1239 R.@:Sedgewick, @samp{Algorithms in C} (Reading, Mass.@:
1240 Addison--Wesley, 1998), @samp{15.4}.) Each trie node stores a
1241 character, and there are branches to subtries for the cases where
1242 a given character is less than, equal to, or greater than the
1243 character in the trie. There also is a pointer to a symbol table
1244 entry if a symbol ends at the current node.''
1246 So it's a tree encoded as a stream of bytes. The stream of bytes
1247 acts on a single virtual global symbol, adding and removing
1248 characters and signalling complete symbol points. Here, we read
1249 the stream and create symbols at the completion points.
1251 First, there's a control byte <<m>>. If any of the listed bits
1252 in <<m>> is nonzero, we execute what stands at the right, in
1256 | 0x40 - Traverse left trie.
1257 | (Read a new command byte and recurse.)
1260 | 0x2f - Read the next byte as a character and store it in the
1261 | current character position; increment character position.
1262 | Test the bits of <<m>>:
1265 | 0x80 - The character is 16-bit (so read another byte,
1266 | merge into current character.
1269 | 0xf - We have a complete symbol; parse the type, value
1270 | and serial number and do what should be done
1271 | with a symbol. The type and length information
1272 | is in j = (m & 0xf).
1274 | (MMO3_REGQUAL_BITS)
1275 | j == 0xf: A register variable. The following
1276 | byte tells which register.
1277 | j <= 8: An absolute symbol. Read j bytes as the
1278 | big-endian number the symbol equals.
1279 | A j = 2 with two zero bytes denotes an
1281 | j > 8: As with j <= 8, but add (0x20 << 56)
1282 | to the value in the following j - 8
1285 | Then comes the serial number, as a variant of
1286 | uleb128, but better named ubeb128:
1287 | Read bytes and shift the previous value left 7
1288 | (multiply by 128). Add in the new byte, repeat
1289 | until a byte has bit 7 set. The serial number
1290 | is the computed value minus 128.
1293 | 0x20 - Traverse middle trie. (Read a new command byte
1294 | and recurse.) Decrement character position.
1297 | 0x10 - Traverse right trie. (Read a new command byte and
1300 Let's look again at the <<lop_stab>> for the trivial file
1301 (@pxref{File layout}).
1303 | 0x980b0000 - lop_stab for ":Main" = 0, serial 1.
1310 This forms the trivial trie (note that the path between ``:'' and
1311 ``M'' is redundant):
1322 | 016e "n" is the last character in a full symbol, and
1323 | with a value represented in one byte.
1324 | 00 The value is 0.
1325 | 81 The serial number is 1. */
1327 bfd_byte m
= mmo_get_byte (abfd
);
1329 /* Check first if we have a bad hair day. */
1330 if (abfd
->tdata
.mmo_data
->have_error
)
1334 /* Traverse left trie. */
1335 mmo_get_symbols (abfd
);
1337 if (m
& MMO3_SYMBITS
)
1339 bfd_byte c
= mmo_get_byte (abfd
);
1340 bfd_byte j
= m
& MMO3_TYPEBITS
;
1342 enum mmo_sym_type sym_type
;
1343 unsigned int serno
= 0;
1348 bfd_byte c2
= mmo_get_byte (abfd
);
1350 /* A two-byte character. We can't grok this, but neither can
1351 mmotype, for other cases than the second byte being zero. */
1355 abfd
->tdata
.mmo_data
->lop_stab_symbol
1356 [abfd
->tdata
.mmo_data
->symbol_position
] = 0;
1358 (*_bfd_error_handler
)
1359 (_("%s: unsupported wide character sequence"
1360 " 0x%02X 0x%02X after symbol name starting with `%s'\n"),
1361 bfd_get_filename (abfd
), c
, c2
,
1362 abfd
->tdata
.mmo_data
->lop_stab_symbol
);
1363 bfd_set_error (bfd_error_bad_value
);
1364 abfd
->tdata
.mmo_data
->have_error
= TRUE
;
1371 abfd
->tdata
.mmo_data
->lop_stab_symbol
[abfd
->tdata
.mmo_data
->symbol_position
++] = c
;
1372 abfd
->tdata
.mmo_data
->lop_stab_symbol
[abfd
->tdata
.mmo_data
->symbol_position
] = 0;
1374 if (j
& MMO3_REGQUAL_BITS
)
1376 if (j
== MMO3_REGQUAL_BITS
)
1378 sym_type
= mmo_reg_sym
;
1379 addr
= mmo_get_byte (abfd
);
1385 for (i
= 0; i
< j
; i
++)
1386 addr
= (addr
<< 8) + mmo_get_byte (abfd
);
1388 if (addr
== 0 && j
== MMO3_UNDEF
)
1389 sym_type
= mmo_undef_sym
;
1391 sym_type
= mmo_abs_sym
;
1397 for (i
= MMO3_DATA
; i
< j
; i
++)
1398 addr
= (addr
<< 8) + mmo_get_byte (abfd
);
1400 addr
+= (bfd_vma
) 0x20 << 56;
1401 sym_type
= mmo_data_sym
;
1404 /* Get the serial number. */
1407 k
= mmo_get_byte (abfd
);
1408 serno
= (serno
<< 7) + k
;
1413 /* Got it. Now enter it. Skip a leading ":". */
1414 if (! abfd
->tdata
.mmo_data
->have_error
1415 && ! mmo_create_symbol (abfd
,
1416 abfd
->tdata
.mmo_data
->lop_stab_symbol
1418 addr
, sym_type
, serno
))
1419 abfd
->tdata
.mmo_data
->have_error
= TRUE
;
1422 if (m
& MMO3_MIDDLE
)
1423 /* Traverse middle trie. */
1424 mmo_get_symbols (abfd
);
1426 abfd
->tdata
.mmo_data
->symbol_position
--;
1430 /* Traverse right trie. */
1431 mmo_get_symbols (abfd
);
1433 return ! abfd
->tdata
.mmo_data
->have_error
;
1436 /* Get the location of memory area [VMA..VMA + SIZE - 1], which we think
1437 is in section SEC. Adjust and reallocate zero-initialized contents.
1438 If there's new contents, allocate to the next multiple of
1439 MMO_SEC_CONTENTS_CHUNK_SIZE. */
1441 static INLINE bfd_byte
*
1442 mmo_get_loc (asection
*sec
, bfd_vma vma
, int size
)
1444 bfd_size_type allocated_size
;
1445 struct mmo_section_data_struct
*sdatap
= mmo_section_data (sec
);
1446 struct mmo_data_list_struct
*datap
= sdatap
->head
;
1447 struct mmo_data_list_struct
*entry
;
1449 /* First search the list to see if we have the requested chunk in one
1450 piece, or perhaps if we have a suitable chunk with room to fit. */
1451 for (; datap
!= NULL
; datap
= datap
->next
)
1453 if (datap
->where
<= vma
1454 && datap
->where
+ datap
->size
>= vma
+ size
)
1455 return datap
->data
+ vma
- datap
->where
;
1456 else if (datap
->where
<= vma
1457 && datap
->where
+ datap
->allocated_size
>= vma
+ size
1458 /* Only munch on the "allocated size" if it does not
1459 overlap the next chunk. */
1460 && (datap
->next
== NULL
|| datap
->next
->where
>= vma
+ size
))
1462 /* There was room allocated, but the size wasn't set to include
1464 datap
->size
+= (vma
+ size
) - (datap
->where
+ datap
->size
);
1466 /* Update the section size. This happens only if we update the
1467 32-bit-aligned chunk size. Callers that have
1468 non-32-bit-aligned sections should do all allocation and
1469 size-setting by themselves or at least set the section size
1470 after the last allocating call to this function. */
1471 if (vma
+ size
> sec
->vma
+ sec
->size
)
1472 sec
->size
+= (vma
+ size
) - (sec
->vma
+ sec
->size
);
1474 return datap
->data
+ vma
- datap
->where
;
1478 /* Not found; allocate a new block. First check in case we get a
1479 request for a size split up over several blocks; we'll have to return
1480 NULL for those cases, requesting the caller to split up the request.
1481 Requests with an address aligned on MMO_SEC_CONTENTS_CHUNK_SIZE bytes and
1482 for no more than MMO_SEC_CONTENTS_CHUNK_SIZE will always get resolved. */
1484 for (datap
= sdatap
->head
; datap
!= NULL
; datap
= datap
->next
)
1485 if ((datap
->where
<= vma
&& datap
->where
+ datap
->size
> vma
)
1486 || (datap
->where
< vma
+ size
1487 && datap
->where
+ datap
->size
>= vma
+ size
))
1491 = (size
+ MMO_SEC_CONTENTS_CHUNK_SIZE
- 1) & ~(MMO_SEC_CONTENTS_CHUNK_SIZE
- 1);
1492 entry
= (mmo_data_list_type
*)
1493 bfd_zalloc (sec
->owner
, sizeof (mmo_data_list_type
) + allocated_size
);
1498 entry
->allocated_size
= allocated_size
;
1500 datap
= sdatap
->head
;
1502 /* Sort the records by address. Optimize for the common case of adding
1503 a record to the end of the list. */
1504 if (sdatap
->tail
!= NULL
&& entry
->where
>= sdatap
->tail
->where
)
1506 sdatap
->tail
->next
= entry
;
1508 sdatap
->tail
= entry
;
1512 mmo_data_list_type
**look
;
1513 for (look
= &sdatap
->head
;
1514 *look
!= NULL
&& (*look
)->where
< entry
->where
;
1515 look
= &(*look
)->next
)
1517 entry
->next
= *look
;
1519 if (entry
->next
== NULL
)
1521 sdatap
->tail
= entry
;
1523 /* We get here for the first time (at other times too) for this
1524 section. Say we have contents. */
1525 if (! bfd_set_section_flags (sec
->owner
, sec
,
1526 bfd_get_section_flags (sec
->owner
, sec
)
1527 | SEC_HAS_CONTENTS
))
1532 /* Update the section size. This happens only when we add contents and
1533 re-size as we go. The section size will then be aligned to 32 bits. */
1534 if (vma
+ size
> sec
->vma
+ sec
->size
)
1535 sec
->size
+= (vma
+ size
) - (sec
->vma
+ sec
->size
);
1539 /* Set sizes once we've read in all sections. */
1542 mmo_map_set_sizes (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
,
1543 void *ignored ATTRIBUTE_UNUSED
)
1545 sec
->lma
= sec
->vma
;
1548 /* Read the mmo file and turn it into sections. */
1551 mmo_scan (bfd
*abfd
)
1554 unsigned int lineno
= 1;
1555 bfd_boolean error
= FALSE
;
1557 asection
*sec
= bfd_make_section_old_way (abfd
, MMO_TEXT_SECTION_NAME
);
1558 asection
*non_spec_sec
= NULL
;
1559 bfd_vma non_spec_vma
= 0;
1560 bfd_size_type nbytes_read
= 0;
1561 /* Buffer with room to read a 64-bit value. */
1564 char *file_names
[256];
1567 memset (file_names
, 0, sizeof (file_names
));
1569 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0)
1572 while ((nbytes_read
= bfd_bread (buf
, 4, abfd
)) == 4)
1576 unsigned int y
= bfd_get_8 (abfd
, buf
+ 2);
1577 unsigned int z
= bfd_get_8 (abfd
, buf
+ 3);
1579 /* Change back to the original section for lopcodes other
1580 than LOP_QUOTE that comes after a LOP_SPEC. */
1581 if ((buf
[1] != LOP_QUOTE
|| y
!= 0 || z
!= 1)
1582 && non_spec_sec
!= NULL
)
1586 non_spec_sec
= NULL
;
1592 (*_bfd_error_handler
)
1593 (_("%s: invalid mmo file: unsupported lopcode `%d'\n"),
1594 bfd_get_filename (abfd
), buf
[1]);
1595 bfd_set_error (bfd_error_bad_value
);
1599 /* Quote the next 32-bit word. */
1600 if (y
!= 0 || z
!= 1)
1602 (*_bfd_error_handler
)
1603 (_("%s: invalid mmo file: expected YZ = 1 got YZ = %d for lop_quote\n"),
1604 bfd_get_filename (abfd
), y
*256+z
);
1605 bfd_set_error (bfd_error_bad_value
);
1608 if (bfd_bread (buf
, 4, abfd
) != 4)
1612 mmo_xore_32 (sec
, vma
, bfd_get_32 (abfd
, buf
));
1618 /* Set vma (and section). */
1619 vma
= (bfd_vma
) y
<< 56;
1622 /* Get a 32-bit value. */
1623 if (bfd_bread (buf
, 4, abfd
) != 4)
1626 vma
+= bfd_get_32 (abfd
, buf
);
1630 /* Get a 64-bit value. */
1631 if (bfd_bread (buf
, 8, abfd
) != 8)
1634 vma
+= bfd_get_64 (abfd
, buf
);
1638 (*_bfd_error_handler
)
1639 (_("%s: invalid mmo file: expected z = 1 or z = 2, got z = %d for lop_loc\n"),
1640 bfd_get_filename (abfd
), z
);
1641 bfd_set_error (bfd_error_bad_value
);
1645 /* When we decide which section the data goes into, we might
1646 create the section. If that happens, make sure the VMA at
1647 creation time is tetra-aligned. */
1648 sec
= mmo_decide_section (abfd
, vma
& ~3);
1654 /* Move forward within the same section. */
1657 sec
= mmo_decide_section (abfd
, vma
);
1663 /* A fixup: Store the current vma somewhere. Position using
1664 same format as LOP_LOC. */
1666 bfd_vma p
= (bfd_vma
) y
<< 56;
1671 /* Get a 32-bit value. */
1672 if (bfd_bread (buf
, 4, abfd
) != 4)
1675 p
+= bfd_get_32 (abfd
, buf
);
1679 /* Get a 64-bit value. */
1680 if (bfd_bread (buf
, 8, abfd
) != 8)
1683 p
+= bfd_get_64 (abfd
, buf
);
1687 (*_bfd_error_handler
)
1688 (_("%s: invalid mmo file: expected z = 1 or z = 2, got z = %d for lop_fixo\n"),
1689 bfd_get_filename (abfd
), z
);
1690 bfd_set_error (bfd_error_bad_value
);
1694 /* The section where we store this address might be a
1695 different one than the current section. */
1696 fixosec
= mmo_decide_section (abfd
, p
);
1697 if (fixosec
== NULL
)
1699 mmo_xore_64 (fixosec
, p
, vma
);
1704 /* A fixup: Store YZ of this lopcode into YZ at vma - 4 * yz. */
1706 unsigned int yz
= (y
* 256 + z
);
1707 bfd_vma p
= vma
+ 2 - 4 * yz
;
1708 asection
*fixrsec
= mmo_decide_section (abfd
, p
);
1709 if (fixrsec
== NULL
)
1711 mmo_xore_16 (fixrsec
, p
, yz
);
1716 /* A fixup, similar to lop_fixr, but taking larger numbers
1717 and can change branches into the opposite direction
1726 (*_bfd_error_handler
)
1727 (_("%s: invalid mmo file: expected y = 0, got y = %d for lop_fixrx\n"),
1728 bfd_get_filename (abfd
), y
);
1729 bfd_set_error (bfd_error_bad_value
);
1733 if (z
!= 16 && z
!= 24)
1735 (*_bfd_error_handler
)
1736 (_("%s: invalid mmo file: expected z = 16 or z = 24, got z = %d for lop_fixrx\n"),
1737 bfd_get_filename (abfd
), z
);
1738 bfd_set_error (bfd_error_bad_value
);
1742 /* Get the next 32-bit value. */
1743 if (bfd_bread (buf
, 4, abfd
) != 4)
1746 delta
= bfd_get_32 (abfd
, buf
);
1748 /* Do an, ehm, involved calculation for the location of
1749 the fixup. See mmixal documentation for a verbose
1750 explanation. We follow it verbosely here for the
1753 p
= vma
- 4 * delta
;
1754 else if (buf
[0] == 1)
1755 p
= vma
- 4 * ((delta
& 0xffffff) - (1 << z
));
1758 (*_bfd_error_handler
)
1759 (_("%s: invalid mmo file: leading byte of operand word must be 0 or 1, got %d for lop_fixrx\n"),
1760 bfd_get_filename (abfd
), buf
[0]);
1761 bfd_set_error (bfd_error_bad_value
);
1765 fixrsec
= mmo_decide_section (abfd
, vma
);
1766 if (fixrsec
== NULL
)
1768 mmo_xore_32 (fixrsec
, p
, delta
);
1773 /* Set current file and perhaps the file name. Reset line
1777 char *fname
= bfd_malloc (z
* 4 + 1);
1781 (*_bfd_error_handler
)
1782 (_("%s: cannot allocate file name for file number %d, %d bytes\n"),
1783 bfd_get_filename (abfd
), y
, z
* 4 + 1);
1784 bfd_set_error (bfd_error_system_call
);
1790 for (i
= 0; i
< z
; i
++)
1792 if (bfd_bread (fname
+ i
* 4, 4, abfd
) != 4)
1799 if (file_names
[y
] != NULL
)
1801 (*_bfd_error_handler
)
1802 (_("%s: invalid mmo file: file number %d `%s',"
1803 " was already entered as `%s'\n"),
1804 bfd_get_filename (abfd
), y
, fname
, file_names
[y
]);
1805 bfd_set_error (bfd_error_bad_value
);
1809 file_names
[y
] = fname
;
1812 if (file_names
[y
] == NULL
)
1814 (*_bfd_error_handler
)
1815 (_("%s: invalid mmo file: file name for number %d"
1816 " was not specified before use\n"),
1817 bfd_get_filename (abfd
), y
);
1818 bfd_set_error (bfd_error_bad_value
);
1826 /* Set line number. */
1827 lineno
= y
* 256 + z
;
1828 /* FIXME: Create a sequence of mmo-specific line number
1829 entries for each section, then translate into canonical
1834 /* Special data follows until the next non-lop_quote
1838 sec
= mmo_get_spec_section (abfd
, y
* 256 + z
);
1847 /* We ignore header information, except we read in the
1848 creation time from the first 32-bit word with the time
1849 in seconds since era. */
1851 && bfd_bread (abfd
->tdata
.mmo_data
->created
, 4,
1855 for (i
= 1; i
< z
; i
++)
1856 if (bfd_bread (buf
, 4, abfd
) != 4)
1862 /* This tells of the contents of registers $Z..$255 at
1863 startup. We make a section out of it, with VMA = Z * 8,
1864 but only if Z != 255 or the contents is non-zero. */
1869 bfd_vma startaddr_octa
;
1871 /* Read first octaword outside loop to simplify logic when
1872 excluding the Z == 255, octa == 0 case. */
1873 if (bfd_bread (buf
, 8, abfd
) != 8)
1876 first_octa
= bfd_get_64 (abfd
, buf
);
1878 /* Don't emit contents for the trivial case which is
1879 always present; $255 pointing to Main. */
1883 = bfd_make_section_old_way (abfd
,
1884 MMIX_REG_CONTENTS_SECTION_NAME
);
1885 rsec
->flags
|= SEC_LINKER_CREATED
;
1887 loc
= mmo_get_loc (rsec
, z
* 8, (255 - z
) * 8);
1888 bfd_put_64 (abfd
, first_octa
, loc
);
1890 for (i
= z
+ 1; i
< 255; i
++)
1892 if (bfd_bread (loc
+ (i
- z
) * 8, 8, abfd
) != 8)
1896 /* Read out the last octabyte, and use it to set the
1898 if (bfd_bread (buf
, 8, abfd
) != 8)
1901 startaddr_octa
= bfd_get_64 (abfd
, buf
);
1904 startaddr_octa
= first_octa
;
1906 if (! bfd_set_start_address (abfd
, startaddr_octa
))
1908 /* Currently this can't fail, but this should handle
1910 bfd_set_error (bfd_error_bad_value
);
1917 /* We read in the symbols now, not later. */
1918 if (y
!= 0 || z
!= 0)
1920 (*_bfd_error_handler
)
1921 (_("%s: invalid mmo file: fields y and z of lop_stab"
1922 " non-zero, y: %d, z: %d\n"),
1923 bfd_get_filename (abfd
), y
, z
);
1924 bfd_set_error (bfd_error_bad_value
);
1928 /* Save the location, so we can check that YZ in the LOP_END
1930 stab_loc
= bfd_tell (abfd
);
1932 /* It's not said that an MMO can be without symbols (though
1933 mmixal will refuse to assemble files without Main), but
1934 it seems it would still be a valid mmo-file, so allow it.
1935 We detect the absence of a symbol area in that the upper
1936 limit is computed (from the lop_end YZ field) as 0.
1937 Don't call mmo_get_symbols; it can only detect the end of
1938 a valid symbol trie, not the absence of one. */
1939 if (abfd
->tdata
.mmo_data
->max_symbol_length
!= 0
1940 && ! mmo_get_symbols (abfd
))
1946 /* This must be the last 32-bit word in an mmo file.
1948 struct stat statbuf
;
1949 file_ptr curpos
= bfd_tell (abfd
);
1951 if (bfd_stat (abfd
, &statbuf
) < 0)
1954 if (statbuf
.st_size
!= curpos
)
1956 (*_bfd_error_handler
)
1957 (_("%s: invalid mmo file: lop_end not last item in"
1959 bfd_get_filename (abfd
));
1960 bfd_set_error (bfd_error_bad_value
);
1964 /* Check that the YZ field is right. Subtract the size of
1965 this LOP_END in the calculation; YZ does not include
1967 if ((long) (y
* 256 + z
) * 4 != (curpos
- stab_loc
) - 4)
1969 (*_bfd_error_handler
)
1970 (_("%s: invalid mmo file: YZ of lop_end (%ld)"
1971 " not equal to the number of tetras to the preceding"
1972 " lop_stab (%ld)\n"),
1973 bfd_get_filename (abfd
), (long) (y
* 256 + z
),
1974 (curpos
- stab_loc
- 4)/4);
1975 bfd_set_error (bfd_error_bad_value
);
1979 bfd_map_over_sections (abfd
, mmo_map_set_sizes
, NULL
);
1986 /* This wasn't a lopcode, so store it in the current section. */
1987 mmo_xore_32 (sec
, vma
& ~3, bfd_get_32 (abfd
, buf
));
1994 /* We know this file is a multiple of four bytes (checked in
1995 mmo_object_p), so if we got something other than 0, this was a bad
1996 file (although it's more likely we'll get 0 in that case too).
1997 If we got end-of-file, then there was no lop_stab, so the file has
2000 if (nbytes_read
!= 0)
2001 bfd_set_error (bfd_error_system_call
);
2003 bfd_set_error (bfd_error_bad_value
);
2008 /* Mark the .text and .data section with their normal attribute if they
2009 contain anything. This is not redundant wrt. mmo_decide_section,
2010 since that code might never execute, and conversely the alloc+code
2011 section flags must be set then. */
2012 sec
= bfd_get_section_by_name (abfd
, MMO_TEXT_SECTION_NAME
);
2014 && (bfd_get_section_flags (abfd
, sec
) & SEC_HAS_CONTENTS
)
2015 && ! bfd_set_section_flags (abfd
, sec
,
2016 bfd_get_section_flags (abfd
, sec
)
2017 | SEC_ALLOC
| SEC_LOAD
| SEC_CODE
))
2020 sec
= bfd_get_section_by_name (abfd
, MMO_DATA_SECTION_NAME
);
2022 && (bfd_get_section_flags (abfd
, sec
) & SEC_HAS_CONTENTS
)
2023 && ! bfd_set_section_flags (abfd
, sec
,
2024 bfd_get_section_flags (abfd
, sec
)
2025 | SEC_ALLOC
| SEC_LOAD
))
2028 /* Free whatever resources we took. */
2029 for (i
= 0; i
< sizeof (file_names
) / sizeof (file_names
[0]); i
++)
2031 free (file_names
[i
]);
2035 /* A hook to set up object file dependent section information. For mmo,
2036 we point out the shape of allocated section contents. */
2039 mmo_new_section_hook (bfd
*abfd
, asection
*newsect
)
2041 if (!newsect
->used_by_bfd
)
2043 /* We zero-fill all fields and assume NULL is represented by an all
2044 zero-bit pattern. */
2045 newsect
->used_by_bfd
2046 = bfd_zalloc (abfd
, sizeof (struct mmo_section_data_struct
));
2047 if (!newsect
->used_by_bfd
)
2051 /* Always align to at least 32-bit words. */
2052 newsect
->alignment_power
= 2;
2053 return _bfd_generic_new_section_hook (abfd
, newsect
);
2056 /* We already have section contents loaded for sections that have
2060 mmo_get_section_contents (bfd
*abfd ATTRIBUTE_UNUSED
,
2064 bfd_size_type bytes_to_do
)
2066 /* Iterate over diminishing chunk sizes, copying contents, like
2067 mmo_set_section_contents. */
2070 /* A minor song-and-dance to make sure we're not bitten by the
2071 distant possibility of the cast from bfd_vma to int making the
2072 chunk zero-sized. */
2074 = (int) bytes_to_do
!= 0 ? bytes_to_do
: MMO_SEC_CONTENTS_CHUNK_SIZE
;
2078 loc
= mmo_get_loc (sec
, sec
->vma
+ offset
, chunk_size
);
2079 while (loc
== NULL
&& (chunk_size
/= 2) != 0);
2081 if (chunk_size
== 0)
2084 memcpy (location
, loc
, chunk_size
);
2086 location
+= chunk_size
;
2087 bytes_to_do
-= chunk_size
;
2088 offset
+= chunk_size
;
2093 /* Return the amount of memory needed to read the symbol table. */
2096 mmo_get_symtab_upper_bound (bfd
*abfd
)
2098 return (abfd
->symcount
+ 1) * sizeof (asymbol
*);
2101 /* Sort mmo symbols by serial number. */
2104 mmo_sort_mmo_symbols (const void *arg1
, const void *arg2
)
2106 const struct mmo_symbol
*sym1
= *(const struct mmo_symbol
**) arg1
;
2107 const struct mmo_symbol
*sym2
= *(const struct mmo_symbol
**) arg2
;
2109 /* Sort by serial number first. */
2110 if (sym1
->serno
< sym2
->serno
)
2112 else if (sym1
->serno
> sym2
->serno
)
2115 /* Then sort by address of the table entries. */
2116 return ((const char *) arg1
- (const char *) arg2
);
2119 /* Translate the symbol table. */
2122 mmo_canonicalize_symtab (bfd
*abfd
, asymbol
**alocation
)
2124 unsigned int symcount
= bfd_get_symcount (abfd
);
2128 csymbols
= abfd
->tdata
.mmo_data
->csymbols
;
2129 if (csymbols
== NULL
&& symcount
!= 0)
2132 struct mmo_symbol
*s
;
2133 struct mmo_symbol
**msp
;
2135 /* First we store the symbols into the table we'll return, then we
2136 qsort it on the serial number, with secondary on the address of
2137 the symbol, to preserve order if there would be non-unique serial
2139 for (s
= abfd
->tdata
.mmo_data
->symbols
,
2140 msp
= (struct mmo_symbol
**) alocation
;
2147 qsort (alocation
, symcount
, sizeof (struct mmo_symbol
*),
2148 mmo_sort_mmo_symbols
);
2150 csymbols
= (asymbol
*) bfd_alloc (abfd
, symcount
* sizeof (asymbol
));
2151 if (csymbols
== NULL
)
2153 abfd
->tdata
.mmo_data
->csymbols
= csymbols
;
2155 for (msp
= (struct mmo_symbol
**) alocation
, c
= csymbols
;
2162 c
->value
= s
->value
;
2163 c
->flags
= BSF_GLOBAL
;
2165 if (s
->sym_type
== mmo_data_sym
)
2168 = bfd_get_section_by_name (abfd
, MMO_DATA_SECTION_NAME
);
2170 if (c
->section
== NULL
)
2171 c
->section
= bfd_abs_section_ptr
;
2173 c
->value
-= c
->section
->vma
;
2175 else if (s
->sym_type
== mmo_undef_sym
)
2176 c
->section
= bfd_und_section_ptr
;
2177 else if (s
->sym_type
== mmo_reg_sym
)
2180 = bfd_make_section_old_way (abfd
, MMIX_REG_SECTION_NAME
);
2181 c
->section
->flags
|= SEC_LINKER_CREATED
;
2186 = bfd_get_section_by_name (abfd
, MMO_TEXT_SECTION_NAME
);
2190 && c
->value
>= textsec
->vma
2191 && c
->value
<= textsec
->vma
+ textsec
->size
)
2193 c
->section
= textsec
;
2194 c
->value
-= c
->section
->vma
;
2196 /* In mmo, symbol types depend on the VMA. Therefore, if
2197 the data section isn't within the usual bounds, its
2198 symbols are marked as absolute. Correct that. This
2199 means we can't have absolute symbols with values matching
2200 data section addresses, but we also can't have with
2201 absolute symbols with values matching text section
2202 addresses. For such needs, use the ELF format. */
2204 = bfd_get_section_by_name (abfd
,
2205 MMO_DATA_SECTION_NAME
))
2207 && c
->value
>= datasec
->vma
2208 && c
->value
<= datasec
->vma
+ datasec
->size
)
2210 c
->section
= datasec
;
2211 c
->value
-= c
->section
->vma
;
2214 c
->section
= bfd_abs_section_ptr
;
2221 /* Last, overwrite the incoming table with the right-type entries. */
2222 for (i
= 0; i
< symcount
; i
++)
2223 *alocation
++ = csymbols
++;
2229 /* Get information about a symbol. */
2232 mmo_get_symbol_info (bfd
*ignore_abfd ATTRIBUTE_UNUSED
,
2233 asymbol
*symbol
, symbol_info
*ret
)
2235 bfd_symbol_info (symbol
, ret
);
2239 mmo_print_symbol (bfd
*abfd
, void *afile
, asymbol
*symbol
,
2240 bfd_print_symbol_type how
)
2242 FILE *file
= (FILE *) afile
;
2246 case bfd_print_symbol_name
:
2247 fprintf (file
, "%s", symbol
->name
);
2250 bfd_print_symbol_vandf (abfd
, file
, symbol
);
2252 fprintf (file
, " %-5s %s",
2253 symbol
->section
->name
,
2258 /* We can't map a file directly into executable code, so the
2259 size of header information is irrelevant. */
2262 mmo_sizeof_headers (bfd
*abfd ATTRIBUTE_UNUSED
,
2263 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
2268 /* Write the (section-neutral) file preamble. */
2271 mmo_internal_write_header (bfd
*abfd
)
2273 const char lop_pre_bfd
[] = { LOP
, LOP_PRE
, 1, 1};
2275 if (bfd_bwrite (lop_pre_bfd
, 4, abfd
) != 4)
2278 /* Copy creation time of original file. */
2279 if (bfd_bwrite (abfd
->tdata
.mmo_data
->created
, 4, abfd
) != 4)
2285 /* Write the LOP_POST record, with global register initializations.
2286 Z is the Z field of the LOP_POST, corresponding to 255 - number of
2287 registers at DATA. The Z = 255 field is filled in with the
2291 mmo_internal_write_post (bfd
*abfd
, int z
, asection
*sec
)
2295 mmo_write_tetra_raw (abfd
, (LOP
<< 24) | (LOP_POST
<< 16) | z
);
2297 for (i
= z
; i
< 255; i
++)
2299 bfd_byte
*data
= mmo_get_loc (sec
, i
* 8, 8);
2301 if (bfd_bwrite (data
, 8, abfd
) != 8)
2305 /* For Z == $255, we always emit the start location; supposedly Main,
2306 but we have it handy at bfd_get_start_address. If we're called with
2307 Z == 255, don't assume DATA is valid. */
2308 bfd_put_64 (abfd
, bfd_get_start_address (abfd
), buf
);
2310 return ! abfd
->tdata
.mmo_data
->have_error
&& bfd_bwrite (buf
, 8, abfd
) == 8;
2313 /* Translate to and from BFD flags. This is to make sure that we don't
2314 get bitten by BFD flag number changes. */
2317 mmo_sec_flags_from_bfd_flags (flagword flags
)
2319 flagword oflags
= 0;
2321 if (flags
& SEC_ALLOC
)
2322 oflags
|= MMO_SEC_ALLOC
;
2323 if (flags
& SEC_LOAD
)
2324 oflags
|= MMO_SEC_LOAD
;
2325 if (flags
& SEC_RELOC
)
2326 oflags
|= MMO_SEC_RELOC
;
2327 if (flags
& SEC_READONLY
)
2328 oflags
|= MMO_SEC_READONLY
;
2329 if (flags
& SEC_CODE
)
2330 oflags
|= MMO_SEC_CODE
;
2331 if (flags
& SEC_DATA
)
2332 oflags
|= MMO_SEC_DATA
;
2333 if (flags
& SEC_NEVER_LOAD
)
2334 oflags
|= MMO_SEC_NEVER_LOAD
;
2335 if (flags
& SEC_IS_COMMON
)
2336 oflags
|= MMO_SEC_IS_COMMON
;
2337 if (flags
& SEC_DEBUGGING
)
2338 oflags
|= MMO_SEC_DEBUGGING
;
2344 bfd_sec_flags_from_mmo_flags (flagword flags
)
2346 flagword oflags
= 0;
2348 if (flags
& MMO_SEC_ALLOC
)
2349 oflags
|= SEC_ALLOC
;
2350 if (flags
& MMO_SEC_LOAD
)
2352 if (flags
& MMO_SEC_RELOC
)
2353 oflags
|= SEC_RELOC
;
2354 if (flags
& MMO_SEC_READONLY
)
2355 oflags
|= SEC_READONLY
;
2356 if (flags
& MMO_SEC_CODE
)
2358 if (flags
& MMO_SEC_DATA
)
2360 if (flags
& MMO_SEC_NEVER_LOAD
)
2361 oflags
|= SEC_NEVER_LOAD
;
2362 if (flags
& MMO_SEC_IS_COMMON
)
2363 oflags
|= SEC_IS_COMMON
;
2364 if (flags
& MMO_SEC_DEBUGGING
)
2365 oflags
|= SEC_DEBUGGING
;
2370 /* Return TRUE iff the leading or trailing tetrabyte in SEC is defined and
2374 mmo_has_leading_or_trailing_zero_tetra_p (bfd
*abfd
, asection
*sec
)
2376 bfd_vma secaddr
= bfd_get_section_vma (abfd
, sec
);
2381 if (bfd_get_32 (abfd
, mmo_get_loc (sec
, secaddr
, 4)) == 0
2382 && bfd_get_32 (abfd
,
2383 mmo_get_loc (sec
, secaddr
+ sec
->size
- 4, 4)) == 0)
2389 /* Write a section. */
2392 mmo_internal_write_section (bfd
*abfd
, asection
*sec
)
2394 /* We do it differently depending on what section this is:
2396 ".text": Output, prepended by information about the first source file
2397 (not yet implemented.)
2401 (".MMIX.reg_contents": Not handled here.)
2403 Anything else: Output inside a lop_spec 80, in the format described
2406 if (strcmp (sec
->name
, MMO_TEXT_SECTION_NAME
) == 0)
2408 bfd_vma secaddr
= bfd_get_section_vma (abfd
, sec
);
2410 /* Because leading and trailing zeros are omitted in output, we need to
2411 specify the section boundaries so they're correct when the file
2412 is read in again. That's also the case if this section is
2413 specified as not within its usual boundaries or alignments. */
2415 && (secaddr
+ sec
->size
>= (bfd_vma
) 1 << 56
2416 || (secaddr
& 3) != 0
2417 || (sec
->size
& 3) != 0
2418 || mmo_has_leading_or_trailing_zero_tetra_p (abfd
, sec
)))
2420 if (!mmo_write_section_description (abfd
, sec
))
2424 /* FIXME: Output source file name and line number. */
2425 return mmo_write_loc_chunk_list (abfd
, mmo_section_data (sec
)->head
);
2427 else if (strcmp (sec
->name
, MMO_DATA_SECTION_NAME
) == 0)
2429 bfd_vma secaddr
= bfd_get_section_vma (abfd
, sec
);
2431 /* Same goes as for MMO_TEXT_SECTION_NAME above. */
2433 && (secaddr
< (bfd_vma
) 0x20 << 56
2434 || secaddr
+ sec
->size
>= (bfd_vma
) 0x21 << 56
2435 || (secaddr
& 3) != 0
2436 || (sec
->size
& 3) != 0
2437 || mmo_has_leading_or_trailing_zero_tetra_p (abfd
, sec
)))
2439 if (!mmo_write_section_description (abfd
, sec
))
2443 return mmo_write_loc_chunk_list (abfd
, mmo_section_data (sec
)->head
);
2445 else if (strcmp (sec
->name
, MMIX_REG_CONTENTS_SECTION_NAME
) == 0)
2446 /* Not handled here. */
2448 /* This would normally be an abort call since this can't happen, but
2449 we don't do that. */
2450 bfd_set_error (bfd_error_bad_value
);
2453 else if (CONST_STRNEQ (sec
->name
, MMIX_OTHER_SPEC_SECTION_PREFIX
))
2455 int n
= atoi (sec
->name
+ strlen (MMIX_OTHER_SPEC_SECTION_PREFIX
));
2457 mmo_write_tetra_raw (abfd
, (LOP
<< 24) | (LOP_SPEC
<< 16) | n
);
2458 return (! abfd
->tdata
.mmo_data
->have_error
2459 && mmo_write_chunk_list (abfd
, mmo_section_data (sec
)->head
));
2461 /* Ignore sections that are just allocated or empty; we write out
2463 else if ((bfd_get_section_flags (abfd
, sec
) & SEC_HAS_CONTENTS
) != 0
2466 if (!mmo_write_section_description (abfd
, sec
))
2469 /* Writing a LOP_LOC ends the LOP_SPEC data, and makes data actually
2471 if (bfd_get_section_flags (abfd
, sec
) & SEC_LOAD
)
2472 return (! abfd
->tdata
.mmo_data
->have_error
2473 && mmo_write_loc_chunk_list (abfd
,
2474 mmo_section_data (sec
)->head
));
2475 return (! abfd
->tdata
.mmo_data
->have_error
2476 && mmo_write_chunk_list (abfd
, mmo_section_data (sec
)->head
));
2479 /* Some section without contents. */
2483 /* Write the description of a section, extended-mmo-style. */
2486 mmo_write_section_description (bfd
*abfd
, asection
*sec
)
2488 /* Keep the following document-comment formatted the way it is. */
2491 mmo section mapping, , Symbol-table, mmo
2495 The implementation in BFD uses special data type 80 (decimal) to
2496 encapsulate and describe named sections, containing e.g.@: debug
2497 information. If needed, any datum in the encapsulation will be
2498 quoted using lop_quote. First comes a 32-bit word holding the
2499 number of 32-bit words containing the zero-terminated zero-padded
2500 segment name. After the name there's a 32-bit word holding flags
2501 describing the section type. Then comes a 64-bit big-endian word
2502 with the section length (in bytes), then another with the section
2503 start address. Depending on the type of section, the contents
2504 might follow, zero-padded to 32-bit boundary. For a loadable
2505 section (such as data or code), the contents might follow at some
2506 later point, not necessarily immediately, as a lop_loc with the
2507 same start address as in the section description, followed by the
2508 contents. This in effect forms a descriptor that must be emitted
2509 before the actual contents. Sections described this way must not
2512 For areas that don't have such descriptors, synthetic sections are
2513 formed by BFD. Consecutive contents in the two memory areas
2514 @samp{0x0000@dots{}00} to @samp{0x01ff@dots{}ff} and
2515 @samp{0x2000@dots{}00} to @samp{0x20ff@dots{}ff} are entered in
2516 sections named <<.text>> and <<.data>> respectively. If an area
2517 is not otherwise described, but would together with a neighboring
2518 lower area be less than @samp{0x40000000} bytes long, it is joined
2519 with the lower area and the gap is zero-filled. For other cases,
2520 a new section is formed, named <<.MMIX.sec.@var{n}>>. Here,
2521 @var{n} is a number, a running count through the mmo file,
2525 A loadable section specified as:
2527 | .section secname,"ax"
2528 | TETRA 1,2,3,4,-1,-2009
2531 and linked to address @samp{0x4}, is represented by the sequence:
2533 | 0x98080050 - lop_spec 80
2534 | 0x00000002 - two 32-bit words for the section name
2535 | 0x7365636e - "secn"
2536 | 0x616d6500 - "ame\0"
2537 | 0x00000033 - flags CODE, READONLY, LOAD, ALLOC
2538 | 0x00000000 - high 32 bits of section length
2539 | 0x0000001c - section length is 28 bytes; 6 * 4 + 1 + alignment to 32 bits
2540 | 0x00000000 - high 32 bits of section address
2541 | 0x00000004 - section address is 4
2542 | 0x98010002 - 64 bits with address of following data
2543 | 0x00000000 - high 32 bits of address
2544 | 0x00000004 - low 32 bits: data starts at address 4
2550 | 0xfffff827 - -2009
2551 | 0x50000000 - 80 as a byte, padded with zeros.
2553 Note that the lop_spec wrapping does not include the section
2554 contents. Compare this to a non-loaded section specified as:
2557 | TETRA 200001,100002
2560 This, when linked to address @samp{0x200000000000001c}, is
2563 | 0x98080050 - lop_spec 80
2564 | 0x00000002 - two 32-bit words for the section name
2565 | 0x7365636e - "thir"
2566 | 0x616d6500 - "dsec"
2567 | 0x00000010 - flag READONLY
2568 | 0x00000000 - high 32 bits of section length
2569 | 0x0000000c - section length is 12 bytes; 2 * 4 + 2 + alignment to 32 bits
2570 | 0x20000000 - high 32 bits of address
2571 | 0x0000001c - low 32 bits of address 0x200000000000001c
2572 | 0x00030d41 - 200001
2573 | 0x000186a2 - 100002
2574 | 0x26280000 - 38, 40 as bytes, padded with zeros
2576 For the latter example, the section contents must not be
2577 loaded in memory, and is therefore specified as part of the
2578 special data. The address is usually unimportant but might
2579 provide information for e.g.@: the DWARF 2 debugging format. */
2581 mmo_write_tetra_raw (abfd
, LOP_SPEC_SECTION
);
2582 mmo_write_tetra (abfd
, (strlen (sec
->name
) + 3) / 4);
2583 mmo_write_chunk (abfd
, (bfd_byte
*) sec
->name
, strlen (sec
->name
));
2584 mmo_flush_chunk (abfd
);
2585 /* FIXME: We can get debug sections (.debug_line & Co.) with a section
2586 flag still having SEC_RELOC set. Investigate. This might be true
2587 for all alien sections; perhaps mmo.em should clear that flag. Might
2588 be related to weak references. */
2589 mmo_write_tetra (abfd
,
2590 mmo_sec_flags_from_bfd_flags
2591 (bfd_get_section_flags (abfd
, sec
)));
2592 mmo_write_octa (abfd
, sec
->size
);
2593 mmo_write_octa (abfd
, bfd_get_section_vma (abfd
, sec
));
2597 /* We save up all data before output. */
2600 mmo_set_section_contents (bfd
*abfd ATTRIBUTE_UNUSED
, sec_ptr sec
,
2601 const void *location
, file_ptr offset
,
2602 bfd_size_type bytes_to_do
)
2604 /* Iterate over diminishing chunk sizes, copying contents. */
2607 /* A minor song-and-dance to make sure we're not bitten by the
2608 distant possibility of the cast from bfd_vma to int making the
2609 chunk zero-sized. */
2611 = (int) bytes_to_do
!= 0 ? bytes_to_do
: MMO_SEC_CONTENTS_CHUNK_SIZE
;
2615 loc
= mmo_get_loc (sec
, sec
->vma
+ offset
, chunk_size
);
2616 while (loc
== NULL
&& (chunk_size
/= 2) != 0);
2618 if (chunk_size
== 0)
2621 memcpy (loc
, location
, chunk_size
);
2623 location
+= chunk_size
;
2624 bytes_to_do
-= chunk_size
;
2625 offset
+= chunk_size
;
2630 /* Add a symbol to a trie-tree. */
2633 mmo_internal_add_3_sym (bfd
*abfd
, struct mmo_symbol_trie
*rootp
,
2634 const struct mmo_symbol
*symp
)
2636 const char *name
= symp
->name
;
2637 struct mmo_symbol_trie
*trie
= rootp
;
2638 struct mmo_symbol_trie
**triep
= NULL
;
2640 while (*name
&& trie
!= NULL
)
2642 if (*name
< trie
->symchar
)
2644 triep
= &trie
->left
;
2647 else if (*name
> trie
->symchar
)
2649 triep
= &trie
->right
;
2652 else if (*name
== trie
->symchar
)
2654 triep
= &trie
->middle
;
2657 /* Make sure "trie" points to where we should fill in the
2658 current symbol whenever we've iterated through "name". We
2659 would lose the right position if we encounter "foobar" then
2662 trie
= trie
->middle
;
2668 /* Create middle branches for the rest of the characters. */
2669 trie
= bfd_zalloc (abfd
, sizeof (struct mmo_symbol_trie
));
2671 trie
->symchar
= *name
++;
2672 triep
= &trie
->middle
;
2675 /* We discover a duplicate symbol rather late in the process, but still;
2676 we discover it and bail out. */
2677 if (trie
->sym
.name
!= NULL
)
2679 (*_bfd_error_handler
)
2680 (_("%s: invalid symbol table: duplicate symbol `%s'\n"),
2681 bfd_get_filename (abfd
), trie
->sym
.name
);
2682 bfd_set_error (bfd_error_bad_value
);
2686 memcpy (&trie
->sym
, symp
, sizeof *symp
);
2690 /* Find out the length of the serialized version of a trie in bytes. */
2693 mmo_internal_3_length (bfd
*abfd
, struct mmo_symbol_trie
*trie
)
2695 /* First, one for the control byte. */
2696 unsigned int length
= 1;
2701 /* Add in the recursion to the left. */
2702 length
+= mmo_internal_3_length (abfd
, trie
->left
);
2704 /* Add in the middle trie and the character. */
2705 length
+= 1 + mmo_internal_3_length (abfd
, trie
->middle
);
2707 /* Add in the recursion to the right. */
2708 length
+= mmo_internal_3_length (abfd
, trie
->right
);
2710 /* Add in bytes for the symbol (if this is an endnode). */
2711 if (trie
->sym
.name
!= NULL
)
2713 unsigned int serno
= trie
->sym
.serno
;
2715 /* First what it takes to encode the value. */
2716 if (trie
->sym
.sym_type
== mmo_reg_sym
)
2718 else if (trie
->sym
.sym_type
== mmo_undef_sym
)
2722 bfd_vma value
= trie
->sym
.value
;
2724 /* Coded in one to eight following bytes. */
2725 if (trie
->sym
.sym_type
== mmo_data_sym
)
2726 value
-= (bfd_vma
) 0x20 << 56;
2736 /* Find out what it takes to encode the serial number. */
2748 /* Helper function for outputting the serial number of a symbol, output as
2749 a variant of leb128 (see dwarf2 documentation) which could be called
2750 beb128. Using a helper function and recursion simplifies debugging. */
2753 mmo_beb128_out (bfd
*abfd
, int serno
, int marker
)
2756 mmo_beb128_out (abfd
, serno
>> 7, 0);
2757 mmo_write_byte (abfd
, marker
| (serno
& 0x7f));
2760 /* Serialize a trie. */
2763 mmo_internal_3_dump (bfd
*abfd
, struct mmo_symbol_trie
*trie
)
2765 bfd_byte control
= 0;
2771 control
|= MMO3_LEFT
;
2774 control
|= MMO3_MIDDLE
;
2777 control
|= MMO3_RIGHT
;
2779 if (trie
->sym
.name
!= NULL
)
2781 /* Encode the symbol type and length of value bytes. */
2782 if (trie
->sym
.sym_type
== mmo_reg_sym
)
2783 control
|= MMO3_REGQUAL_BITS
;
2784 else if (trie
->sym
.sym_type
== mmo_undef_sym
)
2785 control
|= MMO3_UNDEF
;
2788 bfd_vma value
= trie
->sym
.value
;
2790 /* Coded in 1..8 following bytes. */
2791 if (trie
->sym
.sym_type
== mmo_data_sym
)
2793 control
|= MMO3_DATA
;
2794 value
-= (bfd_vma
) 0x20 << 56;
2806 /* The control byte is output before recursing. */
2807 mmo_write_byte (abfd
, control
);
2809 mmo_internal_3_dump (abfd
, trie
->left
);
2811 if (control
& MMO3_SYMBITS
)
2813 mmo_write_byte (abfd
, trie
->symchar
);
2815 if (trie
->sym
.name
!= NULL
)
2817 if (trie
->sym
.sym_type
== mmo_reg_sym
)
2818 mmo_write_byte (abfd
, trie
->sym
.value
);
2819 else if (trie
->sym
.sym_type
== mmo_undef_sym
)
2821 mmo_write_byte (abfd
, 0);
2822 mmo_write_byte (abfd
, 0);
2826 bfd_vma value
= trie
->sym
.value
;
2828 bfd_byte byte_n
= control
& 15;
2830 /* Coded in 1..8 following bytes. Note that the value is
2831 shifted out big-endian. */
2832 if (trie
->sym
.sym_type
== mmo_data_sym
)
2834 value
-= (bfd_vma
) 0x20 << 56;
2840 mmo_write_byte (abfd
, (value
>> ((byte_n
- 1) * 8)) & 0xff);
2843 while (byte_n
!= 0);
2846 mmo_beb128_out (abfd
, trie
->sym
.serno
, 128);
2848 mmo_internal_3_dump (abfd
, trie
->middle
);
2850 mmo_internal_3_dump (abfd
, trie
->right
);
2853 /* Write symbols in mmo format. Also write the lop_end terminator. */
2856 mmo_write_symbols_and_terminator (bfd
*abfd
)
2858 int count
= bfd_get_symcount (abfd
);
2860 asymbol
**orig_table
= bfd_get_outsymbols (abfd
);
2862 struct mmo_symbol_trie root
;
2867 /* Create a symbol for "Main". */
2868 asymbol
*fakemain
= bfd_make_empty_symbol (abfd
);
2870 fakemain
->flags
= BSF_GLOBAL
;
2871 fakemain
->value
= bfd_get_start_address (abfd
);
2872 fakemain
->name
= MMIX_START_SYMBOL_NAME
;
2873 fakemain
->section
= bfd_abs_section_ptr
;
2875 memset (&root
, 0, sizeof (root
));
2877 /* Make all symbols take a left turn. */
2878 root
.symchar
= 0xff;
2880 /* There must always be a ":Main", so we'll add one if there are no
2881 symbols. Make sure we have room for it. */
2882 table
= bfd_alloc (abfd
, (count
+ 1) * sizeof (asymbol
*));
2886 memcpy (table
, orig_table
, count
* sizeof (asymbol
*));
2888 /* Move :Main (if there is one) to the first position. This is
2889 necessary to get the same layout of the trie-tree when linking as
2890 when objcopying the result as in the objcopy.exp test "simple objcopy
2891 of executable". It also automatically takes care of assigning serial
2892 number 1 to :Main (as is mandatory). */
2893 for (i
= 0; i
< count
; i
++)
2894 if (table
[i
] != NULL
2895 && strcmp (table
[i
]->name
, MMIX_START_SYMBOL_NAME
) == 0
2896 && (table
[i
]->flags
& (BSF_DEBUGGING
|BSF_GLOBAL
)) == BSF_GLOBAL
)
2898 asymbol
*mainsym
= table
[i
];
2899 memcpy (table
+ 1, orig_table
, i
* sizeof (asymbol
*));
2902 /* Check that the value assigned to :Main is the same as the entry
2903 address. The default linker script asserts this. This is as
2904 good a place as any to check this consistency. */
2906 + mainsym
->section
->output_section
->vma
2907 + mainsym
->section
->output_offset
)
2908 != bfd_get_start_address (abfd
))
2910 /* Arbitrary buffer to hold the printable representation of a
2913 char vmas_start
[40];
2914 bfd_vma vma_start
= bfd_get_start_address (abfd
);
2916 sprintf_vma (vmas_main
, mainsym
->value
);
2917 sprintf_vma (vmas_start
, vma_start
);
2919 (*_bfd_error_handler
)
2920 (_("%s: Bad symbol definition: `Main' set to %s rather"
2921 " than the start address %s\n"),
2922 bfd_get_filename (abfd
), vmas_main
, vmas_start
);
2923 bfd_set_error (bfd_error_bad_value
);
2928 if (i
== count
&& count
!= 0)
2930 /* When there are symbols, there must be a :Main. There was no
2931 :Main, so we need to add it manually. */
2932 memcpy (table
+ 1, orig_table
, count
* sizeof (asymbol
*));
2933 table
[0] = fakemain
;
2937 for (i
= 0, serno
= 1; i
< count
&& table
[i
] != NULL
; i
++)
2939 asymbol
*s
= table
[i
];
2941 /* It's not enough to consult bfd_is_local_label, since it does not
2942 mean "local" in the sense of linkable-and-observable-after-link.
2943 Let's just check the BSF_GLOBAL flag.
2945 Also, don't export symbols with characters not in the allowed set. */
2946 if ((s
->flags
& (BSF_DEBUGGING
|BSF_GLOBAL
)) == BSF_GLOBAL
2948 valid_mmo_symbol_character_set
) == strlen (s
->name
))
2950 struct mmo_symbol sym
;
2951 memset (&sym
, 0, sizeof (sym
));
2953 /* Need to strip const here; strdup:ing would leak and the
2954 existing string must be safe to reuse. */
2955 sym
.name
= (char *) s
->name
;
2958 + s
->section
->output_section
->vma
2959 + s
->section
->output_offset
;
2961 if (bfd_is_und_section (s
->section
))
2962 sym
.sym_type
= mmo_undef_sym
;
2963 else if (strcmp (s
->section
->name
, MMO_DATA_SECTION_NAME
) == 0
2964 /* The encoding of data symbols require that the "rest"
2965 of the value fits in 6 bytes, so the upper two bytes
2966 must be 0x2000. All other symbols get to be the
2968 && (sym
.value
>> 48) == 0x2000)
2969 sym
.sym_type
= mmo_data_sym
;
2970 else if (strcmp (s
->section
->name
, MMIX_REG_SECTION_NAME
) == 0)
2971 sym
.sym_type
= mmo_reg_sym
;
2972 else if (strcmp (s
->section
->name
,
2973 MMIX_REG_CONTENTS_SECTION_NAME
) == 0)
2975 sym
.sym_type
= mmo_reg_sym
;
2979 sym
.sym_type
= mmo_abs_sym
;
2981 /* FIXME: We assume the order of the received symbols is an
2982 ordered mapping of the serial numbers. This is not
2983 necessarily true if we e.g. objcopy a mmo file to another and
2984 there are gaps in the numbering. Not sure if this can
2985 happen. Not sure what to do. */
2986 sym
.serno
= serno
++;
2988 if (! mmo_internal_add_3_sym (abfd
, &root
, &sym
))
2993 /* Change the root node to be a ":"-prefix. */
2995 root
.middle
= root
.left
;
2999 /* We have to find out if we can fit the whole symbol table in the mmo
3000 symtab. It would be bad to assume we can always fit it in 262144
3001 bytes. If we can't, just leave the Main symbol. */
3002 trie_len
= (mmo_internal_3_length (abfd
, &root
) + 3)/4;
3004 if (trie_len
> 0xffff)
3006 /* Test this code by using a lower limit in the test above and check
3007 that the single "Main" symbol is emitted and handled properly.
3008 There's no specific test-case. */
3009 struct mmo_symbol sym
;
3011 (*_bfd_error_handler
)
3012 (_("%s: warning: symbol table too large for mmo, larger than 65535"
3013 " 32-bit words: %d. Only `Main' will be emitted.\n"),
3014 bfd_get_filename (abfd
), trie_len
);
3016 memset (&sym
, 0, sizeof (sym
));
3017 sym
.sym_type
= mmo_abs_sym
;
3018 sym
.name
= MMIX_START_SYMBOL_NAME
;
3020 sym
.value
= bfd_get_start_address (abfd
);
3022 /* Then patch up a symbol table to be just the ":Main" symbol. */
3023 memset (&root
, 0, sizeof (root
));
3024 root
.left
= root
.middle
;
3025 root
.symchar
= 0xff;
3029 if (! mmo_internal_add_3_sym (abfd
, &root
, &sym
))
3033 root
.middle
= root
.left
;
3037 trie_len
= (mmo_internal_3_length (abfd
, &root
) + 3)/4;
3040 /* Reset the written-bytes counter. */
3041 abfd
->tdata
.mmo_data
->byte_no
= 0;
3043 /* Put out the lop_stab mark. */
3044 bfd_put_32 (abfd
, (LOP
<< 24) | (LOP_STAB
<< 16), buf
);
3045 if (bfd_bwrite (buf
, 4, abfd
) != 4)
3048 /* Dump out symbols. */
3049 mmo_internal_3_dump (abfd
, &root
);
3051 if (trie_len
!= (abfd
->tdata
.mmo_data
->byte_no
+ 3)/4)
3053 /* I haven't seen this trig. It seems no use claiming this case
3054 isn't debugged and abort if we get here. Instead emit a
3055 diagnostic and fail "normally". */
3056 (*_bfd_error_handler
)
3057 (_("%s: internal error, symbol table changed size from %d to %d"
3059 bfd_get_filename (abfd
), trie_len
,
3060 (abfd
->tdata
.mmo_data
->byte_no
+ 3)/4);
3061 bfd_set_error (bfd_error_bad_value
);
3065 /* Dump out remaining bytes in the buffer and handle I/O errors by
3066 propagating errors. */
3067 if ((abfd
->tdata
.mmo_data
->byte_no
% 4) != 0
3068 || abfd
->tdata
.mmo_data
->have_error
)
3070 memset (abfd
->tdata
.mmo_data
->buf
+ (abfd
->tdata
.mmo_data
->byte_no
% 4),
3071 0, 4 - (abfd
->tdata
.mmo_data
->byte_no
% 4));
3073 if (abfd
->tdata
.mmo_data
->have_error
3074 || bfd_bwrite (abfd
->tdata
.mmo_data
->buf
, 4, abfd
) != 4)
3078 bfd_put_32 (abfd
, (LOP
<< 24) | (LOP_END
<< 16) | trie_len
, buf
);
3079 return bfd_bwrite (buf
, 4, abfd
) == 4;
3082 /* Write section unless it is the register contents section. For that, we
3083 instead store the section in the supplied pointer. This function is
3084 used through bfd_map_over_sections. */
3087 mmo_write_section_unless_reg_contents (bfd
*abfd
, asection
*sec
, void *p
)
3089 struct mmo_write_sec_info
*infop
= (struct mmo_write_sec_info
*) p
;
3091 if (! infop
->retval
)
3094 if (strcmp (sec
->name
, MMIX_REG_CONTENTS_SECTION_NAME
) == 0)
3096 infop
->reg_section
= sec
;
3100 /* Exclude the convenience register section. */
3101 if (strcmp (sec
->name
, MMIX_REG_SECTION_NAME
) == 0)
3103 if (bfd_get_section_flags (abfd
, sec
) & SEC_HAS_CONTENTS
)
3105 /* Make sure it hasn't got contents. It seems impossible to
3106 make it carry contents, so we don't have a test-case for
3108 (*_bfd_error_handler
)
3109 (_("%s: internal error, internal register section %s had"
3111 bfd_get_filename (abfd
), sec
->name
);
3112 bfd_set_error (bfd_error_bad_value
);
3113 infop
->retval
= FALSE
;
3120 infop
->retval
= mmo_internal_write_section (abfd
, sec
);
3123 /* Do the actual output of a file. Assumes mmo_set_section_contents is
3127 mmo_write_object_contents (bfd
*abfd
)
3129 struct mmo_write_sec_info wsecinfo
;
3131 /* First, there are a few words of preamble. */
3132 if (! mmo_internal_write_header (abfd
))
3135 wsecinfo
.reg_section
= NULL
;
3136 wsecinfo
.retval
= TRUE
;
3138 bfd_map_over_sections (abfd
, mmo_write_section_unless_reg_contents
,
3141 if (! wsecinfo
.retval
)
3144 if (wsecinfo
.reg_section
!= NULL
)
3146 asection
*sec
= wsecinfo
.reg_section
;
3147 unsigned int z
= (unsigned int) (sec
->vma
/ 8);
3149 /* Registers 0..31 must not be global. Do sanity check on the "vma"
3150 of the register contents section and check that it corresponds to
3151 the length of the section. */
3152 if (z
< 32 || z
>= 255 || (sec
->vma
& 7) != 0
3153 || sec
->vma
!= 256 * 8 - sec
->size
- 8)
3155 bfd_set_error (bfd_error_bad_value
);
3158 /* There must always be at least one such register. */
3159 (*_bfd_error_handler
)
3160 (_("%s: no initialized registers; section length 0\n"),
3161 bfd_get_filename (abfd
));
3162 else if (sec
->vma
> (256 - 32) * 8)
3163 /* Provide better error message for the case of too many
3164 global registers. */
3165 (*_bfd_error_handler
)
3166 (_("%s: too many initialized registers; section length %ld\n"),
3167 bfd_get_filename (abfd
),
3170 (*_bfd_error_handler
)
3171 (_("%s: invalid start address for initialized registers of"
3172 " length %ld: 0x%lx%08lx\n"),
3173 bfd_get_filename (abfd
),
3175 (unsigned long) (sec
->vma
>> 32), (unsigned long) (sec
->vma
));
3180 if (! mmo_internal_write_post (abfd
, z
, sec
))
3184 if (! mmo_internal_write_post (abfd
, 255, NULL
))
3187 return mmo_write_symbols_and_terminator (abfd
);
3190 /* If there's anything in particular in a mmo bfd that we want to free,
3191 make this a real function. Only do this if you see major memory
3192 thrashing; zealous free:ing will cause unwanted behavior, especially if
3193 you "free" memory allocated with "bfd_alloc", or even "bfd_release" a
3194 block allocated with "bfd_alloc"; they're really allocated from an
3195 obstack, and we don't know what was allocated there since this
3196 particular allocation. */
3198 #define mmo_close_and_cleanup _bfd_generic_close_and_cleanup
3199 #define mmo_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
3201 /* Perhaps we need to adjust this one; mmo labels (originally) without a
3202 leading ':' might more appropriately be called local. */
3203 #define mmo_bfd_is_local_label_name bfd_generic_is_local_label_name
3204 #define mmo_bfd_is_target_special_symbol \
3205 ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
3207 /* Is this one really used or defined by anyone? */
3208 #define mmo_get_lineno _bfd_nosymbols_get_lineno
3210 /* FIXME: We can do better on this one, if we have a dwarf2 .debug_line
3211 section or if MMO line numbers are implemented. */
3212 #define mmo_find_nearest_line _bfd_nosymbols_find_nearest_line
3213 #define mmo_find_line _bfd_nosymbols_find_line
3214 #define mmo_find_inliner_info _bfd_nosymbols_find_inliner_info
3215 #define mmo_make_empty_symbol _bfd_generic_make_empty_symbol
3216 #define mmo_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
3217 #define mmo_read_minisymbols _bfd_generic_read_minisymbols
3218 #define mmo_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
3220 #define mmo_get_section_contents_in_window \
3221 _bfd_generic_get_section_contents_in_window
3222 #define mmo_bfd_get_relocated_section_contents \
3223 bfd_generic_get_relocated_section_contents
3224 #define mmo_bfd_gc_sections bfd_generic_gc_sections
3225 #define mmo_bfd_lookup_section_flags bfd_generic_lookup_section_flags
3226 #define mmo_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
3227 #define mmo_bfd_link_add_symbols _bfd_generic_link_add_symbols
3228 #define mmo_bfd_link_just_syms _bfd_generic_link_just_syms
3229 #define mmo_bfd_copy_link_hash_symbol_type \
3230 _bfd_generic_copy_link_hash_symbol_type
3231 #define mmo_bfd_final_link _bfd_generic_final_link
3232 #define mmo_bfd_link_split_section _bfd_generic_link_split_section
3234 /* Strictly speaking, only MMIX uses this restricted format, but let's not
3235 stop anybody from shooting themselves in the foot. */
3236 #define mmo_set_arch_mach bfd_default_set_arch_mach
3237 #define mmo_bfd_relax_section bfd_generic_relax_section
3238 #define mmo_bfd_merge_sections bfd_generic_merge_sections
3239 #define mmo_bfd_is_group_section bfd_generic_is_group_section
3240 #define mmo_bfd_discard_group bfd_generic_discard_group
3241 #define mmo_section_already_linked \
3242 _bfd_generic_section_already_linked
3243 #define mmo_bfd_define_common_symbol bfd_generic_define_common_symbol
3245 /* We want to copy time of creation, otherwise we'd use
3246 BFD_JUMP_TABLE_COPY (_bfd_generic). */
3247 #define mmo_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
3248 #define mmo_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
3249 #define mmo_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
3250 #define mmo_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
3251 #define mmo_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
3252 #define mmo_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
3254 const bfd_target mmix_mmo_vec
=
3257 bfd_target_mmo_flavour
,
3258 BFD_ENDIAN_BIG
, /* target byte order */
3259 BFD_ENDIAN_BIG
, /* target headers byte order */
3261 /* FIXME: Might need adjustments. */
3262 (HAS_RELOC
| EXEC_P
| /* object flags */
3263 HAS_LINENO
| HAS_DEBUG
|
3264 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
),
3266 /* FIXME: Might need adjustments. */
3267 (SEC_CODE
| SEC_DATA
| SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
3268 | SEC_READONLY
| SEC_EXCLUDE
| SEC_DEBUGGING
| SEC_IN_MEMORY
),
3270 0, /* leading underscore */
3271 ' ', /* ar_pad_char */
3272 16, /* ar_max_namelen */
3273 0, /* match priority. */
3274 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
3275 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
3276 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
3277 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
3278 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
3279 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
3283 mmo_object_p
, /* bfd_check_format */
3293 { /* bfd_write_contents */
3295 mmo_write_object_contents
,
3300 BFD_JUMP_TABLE_GENERIC (mmo
),
3301 BFD_JUMP_TABLE_COPY (mmo
),
3302 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
3303 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive
),
3304 BFD_JUMP_TABLE_SYMBOLS (mmo
),
3305 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs
),
3306 BFD_JUMP_TABLE_WRITE (mmo
),
3307 BFD_JUMP_TABLE_LINK (mmo
),
3308 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),