1 /* BFD back-end for mmo objects (MMIX-specific object-format).
2 Copyright (C) 2001-2022 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. */
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. */
334 /* Whether we've calculated symbol consistency requirement yet. We do this
335 when-needed, which must be at some time after all section
336 contents is known. */
337 bool symbol_consistency_override_calculated
;
339 /* Whether to consistency-check symbol values, in particular "Main". */
340 bool ignore_symbol_consistency
;
343 typedef struct mmo_data_struct tdata_type
;
345 struct mmo_section_data_struct
347 mmo_data_list_type
*head
;
348 mmo_data_list_type
*tail
;
351 #define mmo_section_data(sec) \
352 ((struct mmo_section_data_struct *) (sec)->used_by_bfd)
354 /* These structures are used in bfd_map_over_sections constructs. */
356 /* Used when writing out sections; all but the register contents section
357 which is stored in reg_section. */
358 struct mmo_write_sec_info
360 asection
*reg_section
;
364 /* Used when trying to find a section corresponding to addr. */
365 struct mmo_find_sec_info
371 static bool mmo_bfd_copy_private_bfd_data (bfd
*, bfd
*);
372 static void mmo_write_section_unless_reg_contents (bfd
*, asection
*, void *);
373 static void mmo_find_sec_w_addr (bfd
*, asection
*, void *);
374 static void mmo_find_sec_w_addr_grow (bfd
*, asection
*, void *);
375 static asection
*mmo_make_section (bfd
*, const char *);
376 static void mmo_get_symbol_info (bfd
*, asymbol
*, symbol_info
*);
377 static void mmo_print_symbol (bfd
*, void *, asymbol
*,
378 bfd_print_symbol_type
);
379 static void mmo_init (void);
380 static bool mmo_mkobject (bfd
*);
381 static bool mmo_scan (bfd
*);
382 static asection
*mmo_decide_section (bfd
*, bfd_vma
);
383 static asection
*mmo_get_generic_spec_data_section (bfd
*, int);
384 static asection
*mmo_get_spec_section (bfd
*, int);
385 static bfd_byte
*mmo_get_loc (asection
*, bfd_vma
, unsigned int);
386 static bfd_cleanup
mmo_object_p (bfd
*);
387 static void mmo_map_set_sizes (bfd
*, asection
*, void *);
388 static bool mmo_get_symbols (bfd
*);
389 static bool mmo_create_symbol (bfd
*, const char *, bfd_vma
,
390 enum mmo_sym_type
, unsigned int);
391 static bool mmo_get_section_contents (bfd
*, asection
*, void *,
392 file_ptr
, bfd_size_type
);
393 static long mmo_get_symtab_upper_bound (bfd
*);
394 static long mmo_canonicalize_symtab (bfd
*, asymbol
**);
395 static void mmo_get_symbol_info (bfd
*, asymbol
*, symbol_info
*);
396 static void mmo_print_symbol (bfd
*, void *, asymbol
*,
397 bfd_print_symbol_type
);
398 static bool mmo_set_section_contents (bfd
*, sec_ptr
, const void *,
399 file_ptr
, bfd_size_type
);
400 static int mmo_sizeof_headers (bfd
*, struct bfd_link_info
*);
401 static bool mmo_internal_write_header (bfd
*);
402 static bool mmo_internal_write_post (bfd
*, int, asection
*);
403 static bool mmo_internal_add_3_sym (bfd
*, struct mmo_symbol_trie
*,
404 const struct mmo_symbol
*);
405 static unsigned int mmo_internal_3_length (bfd
*, struct mmo_symbol_trie
*);
406 static void mmo_internal_3_dump (bfd
*, struct mmo_symbol_trie
*);
407 static void mmo_beb128_out (bfd
*, int, int);
408 static bool mmo_internal_write_section (bfd
*, asection
*);
409 static void mmo_write_tetra (bfd
*, unsigned int);
410 static void mmo_write_tetra_raw (bfd
*, unsigned int);
411 static void mmo_write_octa (bfd
*, bfd_vma
);
412 static void mmo_write_octa_raw (bfd
*, bfd_vma
);
413 static bool mmo_write_chunk (bfd
*, const bfd_byte
*, unsigned int);
414 static bool mmo_flush_chunk (bfd
*);
415 static bool mmo_write_loc_chunk (bfd
*, bfd_vma
, const bfd_byte
*,
416 unsigned int, bfd_vma
*);
417 static bool mmo_write_chunk_list (bfd
*, mmo_data_list_type
*);
418 static bool mmo_write_loc_chunk_list (bfd
*, mmo_data_list_type
*);
419 static bool mmo_write_symbols_and_terminator (bfd
*);
420 static flagword
mmo_sec_flags_from_bfd_flags (flagword
);
421 static flagword
bfd_sec_flags_from_mmo_flags (flagword
);
422 static bfd_byte
mmo_get_byte (bfd
*);
423 static void mmo_write_byte (bfd
*, bfd_byte
);
424 static bool mmo_new_section_hook (bfd
*, asection
*);
425 static int mmo_sort_mmo_symbols (const void *, const void *);
426 static bool mmo_write_object_contents (bfd
*);
427 static bool mmo_write_section_description (bfd
*, asection
*);
428 static bool mmo_has_leading_or_trailing_zero_tetra_p (bfd
*, asection
*);
431 valid_mmo_symbol_character_set
[] =
433 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
434 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
435 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
436 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
437 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
438 ':', '_', 126, 127, 128, 129,
439 130, 131, 132, 133, 134, 135, 136, 137, 138, 139,
440 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
441 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
442 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
443 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
444 180, 181, 182, 183, 184, 185, 186, 187, 188, 189,
445 190, 191, 192, 193, 194, 195, 196, 197, 198, 199,
446 200, 201, 202, 203, 204, 205, 206, 207, 208, 209,
447 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
448 220, 221, 222, 223, 224, 225, 226, 227, 228, 229,
449 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
450 240, 241, 242, 243, 244, 245, 246, 247, 248, 249,
451 250, 251, 252, 253, 254, 255,
456 /* Get section SECNAME or create one if it doesn't exist. When creating
457 one, new memory for the name is allocated. */
460 mmo_make_section (bfd
*abfd
, const char *secname
)
462 asection
*sec
= bfd_get_section_by_name (abfd
, secname
);
466 char *newsecname
= strdup (secname
);
468 if (newsecname
== NULL
)
471 /* xgettext:c-format */
472 (_("%pB: no core to allocate section name %s"),
474 bfd_set_error (bfd_error_system_call
);
477 sec
= bfd_make_section (abfd
, newsecname
);
483 /* Nothing to do, but keep as a placeholder if we need it.
484 Note that state that might differ between bfd:s must not be initialized
485 here, nor must it be static. Add it to tdata information instead. */
490 static bool inited
= false;
497 /* Check whether an existing file is an mmo file. */
500 mmo_object_p (bfd
*abfd
)
507 if (bfd_stat (abfd
, &statbuf
) < 0
508 || bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0
509 || bfd_bread (b
, 4, abfd
) != 4)
512 /* All mmo files are a multiple of four bytes long.
513 Only recognize version one. */
514 if ((statbuf
.st_size
% 4) != 0
515 || b
[0] != LOP
|| b
[1] != LOP_PRE
|| b
[2] != 1)
518 /* Get the last 32-bit word. */
519 if (bfd_seek (abfd
, (file_ptr
) statbuf
.st_size
- 4, SEEK_SET
) != 0
520 || bfd_bread (b
, 4, abfd
) != 4)
523 /* Check if the file ends in a lop_end lopcode. */
524 if (b
[0] != LOP
|| b
[1] != LOP_END
|| ! mmo_mkobject (abfd
))
527 /* Compute an upper bound on the max symbol length. Not really
528 important as all of the symbol information can only be 256k. */
529 abfd
->tdata
.mmo_data
->max_symbol_length
= (b
[2] * 256 + b
[3]) * 4;
530 abfd
->tdata
.mmo_data
->lop_stab_symbol
531 = bfd_malloc (abfd
->tdata
.mmo_data
->max_symbol_length
+ 1);
533 if (abfd
->tdata
.mmo_data
->lop_stab_symbol
== NULL
)
536 /* xgettext:c-format */
537 (_("%pB: no core to allocate a symbol %d bytes long"),
538 abfd
, abfd
->tdata
.mmo_data
->max_symbol_length
);
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 _bfd_no_cleanup
;
558 free (abfd
->tdata
.mmo_data
->lop_stab_symbol
);
560 bfd_set_error (bfd_error_wrong_format
);
565 /* Set up the mmo tdata information. */
568 mmo_mkobject (bfd
*abfd
)
572 if (abfd
->tdata
.mmo_data
== NULL
)
576 /* All fields are zero-initialized, so we don't have to explicitly
578 tdata_type
*tdata
= (tdata_type
*) bfd_zalloc (abfd
, sizeof (tdata_type
));
582 created
= time (NULL
);
583 bfd_put_32 (abfd
, created
, tdata
->created
);
585 abfd
->tdata
.mmo_data
= tdata
;
592 mmo_section_has_contents (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *p ATTRIBUTE_UNUSED
)
594 /* The point is to match what --extract-symbols does (well, negated). */
595 return bfd_section_size (sec
) != 0;
598 /* Find out whether we should omit symbol consistency checks for this
599 bfd and cache the value.
601 This function must only be called when all section contents is
602 known. However, calculating symbol consistency at the time the
603 private BFD data is initialized is too late for some uses. */
606 mmo_ignore_symbol_consistency (bfd
*abfd
)
608 if (!abfd
->tdata
.mmo_data
->symbol_consistency_override_calculated
)
610 abfd
->tdata
.mmo_data
->ignore_symbol_consistency
=
611 bfd_sections_find_if (abfd
, mmo_section_has_contents
, NULL
) == NULL
;
613 abfd
->tdata
.mmo_data
->symbol_consistency_override_calculated
= true;
616 return abfd
->tdata
.mmo_data
->ignore_symbol_consistency
;
620 mmo_bfd_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
622 if (bfd_get_flavour (ibfd
) != bfd_target_mmo_flavour
623 || bfd_get_flavour (obfd
) != bfd_target_mmo_flavour
)
626 /* Copy the time the copied-from file was created. If people want the
627 time the file was last *modified*, they have that in the normal file
629 memcpy (obfd
->tdata
.mmo_data
->created
, ibfd
->tdata
.mmo_data
->created
,
630 sizeof (obfd
->tdata
.mmo_data
->created
));
634 /* Helper functions for mmo_decide_section, used through
635 bfd_map_over_sections. */
638 mmo_find_sec_w_addr (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *p
)
640 struct mmo_find_sec_info
*infop
= (struct mmo_find_sec_info
*) p
;
641 bfd_vma vma
= bfd_section_vma (sec
);
643 /* Ignore sections that aren't loaded. */
644 if ((bfd_section_flags (sec
) & (SEC_LOAD
| SEC_ALLOC
))
645 != (SEC_LOAD
| SEC_ALLOC
))
648 if (infop
->addr
>= vma
&& infop
->addr
< vma
+ sec
->size
)
653 mmo_find_sec_w_addr_grow (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *p
)
655 struct mmo_find_sec_info
*infop
= (struct mmo_find_sec_info
*) p
;
656 bfd_vma vma
= bfd_section_vma (sec
);
658 /* Ignore sections that aren't loaded. */
659 if ((bfd_section_flags (sec
) & (SEC_LOAD
| SEC_ALLOC
))
660 != (SEC_LOAD
| SEC_ALLOC
))
663 if (infop
->addr
>= vma
&& infop
->addr
< vma
+ MAX_ARTIFICIAL_SECTION_SIZE
)
667 /* Find a section that corresponds to a VMA. Automatically create .text
668 or .data and set current section to it, depending on what vma. If we
669 can't deduce a section, make one up as ".MMIX.sec.N", where N is an
670 increasing number. */
673 mmo_decide_section (bfd
*abfd
, bfd_vma vma
)
675 asection
*sec
= NULL
;
676 char sec_name
[sizeof (".MMIX.sec.") + 20];
677 struct mmo_find_sec_info info
;
682 /* First see if there's a section that would match exactly. */
683 bfd_map_over_sections (abfd
, mmo_find_sec_w_addr
, &info
);
685 if (info
.sec
!= NULL
)
688 /* If there's no such section, try and expand one of the existing ones,
689 up to a limit. Make sure we have .text and .data before we try that;
690 create them corresponding to expected addresses and set flags to make
691 them match the "loaded and with contents" expectation. */
692 if ((vma
>> 56) == 0)
694 sec
= bfd_make_section_old_way (abfd
, MMO_TEXT_SECTION_NAME
);
699 if (!sec
->user_set_vma
&& !bfd_set_section_vma (sec
, vma
))
702 if (!bfd_set_section_flags (sec
, (bfd_section_flags (sec
)
703 | SEC_CODE
| SEC_LOAD
| SEC_ALLOC
)))
706 else if ((vma
>> 56) == 0x20)
708 sec
= bfd_make_section_old_way (abfd
, MMO_DATA_SECTION_NAME
);
713 if (!sec
->user_set_vma
&& !bfd_set_section_vma (sec
, vma
))
716 if (!bfd_set_section_flags (sec
, (bfd_section_flags (sec
)
717 | SEC_LOAD
| SEC_ALLOC
)))
721 bfd_map_over_sections (abfd
, mmo_find_sec_w_addr_grow
, &info
);
723 if (info
.sec
!= NULL
)
726 /* If there's still no suitable section, make a new one. */
727 sprintf (sec_name
, ".MMIX.sec.%d", abfd
->tdata
.mmo_data
->sec_no
++);
728 sec
= mmo_make_section (abfd
, sec_name
);
730 if (!sec
->user_set_vma
&& !bfd_set_section_vma (sec
, vma
))
733 if (!bfd_set_section_flags (sec
, (bfd_section_flags (sec
)
734 | SEC_LOAD
| SEC_ALLOC
)))
739 /* Xor in a 64-bit value VALUE at VMA. */
741 static inline bfd_byte
*
742 mmo_xore_64 (asection
*sec
, bfd_vma vma
, bfd_vma value
)
744 bfd_byte
*loc
= mmo_get_loc (sec
, vma
, 8);
747 bfd_vma prev
= bfd_get_64 (sec
->owner
, loc
);
750 bfd_put_64 (sec
->owner
, value
, loc
);
755 /* Xor in a 32-bit value VALUE at VMA. */
757 static inline bfd_byte
*
758 mmo_xore_32 (asection
*sec
, bfd_vma vma
, unsigned int value
)
760 bfd_byte
*loc
= mmo_get_loc (sec
, vma
, 4);
763 unsigned int prev
= bfd_get_32 (sec
->owner
, loc
);
766 bfd_put_32 (sec
->owner
, value
, loc
);
771 /* Xor in a 16-bit value VALUE at VMA. */
773 static inline bfd_byte
*
774 mmo_xore_16 (asection
*sec
, bfd_vma vma
, unsigned int value
)
776 bfd_byte
*loc
= mmo_get_loc (sec
, vma
, 2);
779 unsigned int prev
= bfd_get_16 (sec
->owner
, loc
);
782 bfd_put_16 (sec
->owner
, value
, loc
);
787 /* Write a 32-bit word to output file, no lop_quote generated. */
790 mmo_write_tetra_raw (bfd
*abfd
, unsigned int value
)
794 bfd_put_32 (abfd
, value
, buf
);
796 if (bfd_bwrite (buf
, 4, abfd
) != 4)
797 abfd
->tdata
.mmo_data
->have_error
= true;
800 /* Write a 32-bit word to output file; lop_quote if necessary. */
803 mmo_write_tetra (bfd
*abfd
, unsigned int value
)
805 if (((value
>> 24) & 0xff) == LOP
)
806 mmo_write_tetra_raw (abfd
, LOP_QUOTE_NEXT
);
808 mmo_write_tetra_raw (abfd
, value
);
811 /* Write a 64-bit word to output file, perhaps with lop_quoting. */
814 mmo_write_octa (bfd
*abfd
, bfd_vma value
)
816 mmo_write_tetra (abfd
, (unsigned int) (value
>> 32));
817 mmo_write_tetra (abfd
, (unsigned int) value
);
820 /* Write a 64-bit word to output file, without lop_quoting. */
823 mmo_write_octa_raw (bfd
*abfd
, bfd_vma value
)
825 mmo_write_tetra_raw (abfd
, (unsigned int) (value
>> 32));
826 mmo_write_tetra_raw (abfd
, (unsigned int) value
);
829 /* Write quoted contents. Intended to be called multiple times in
830 sequence, followed by a call to mmo_flush_chunk. */
833 mmo_write_chunk (bfd
*abfd
, const bfd_byte
*loc
, unsigned int len
)
836 struct mmo_data_struct
*mmop
= abfd
->tdata
.mmo_data
;
838 /* Fill up a tetra from bytes remaining from a previous chunk. */
839 if (mmop
->byte_no
!= 0)
841 while (mmop
->byte_no
< 4 && len
!= 0)
843 mmop
->buf
[mmop
->byte_no
++] = *loc
++;
847 if (mmop
->byte_no
== 4)
849 mmo_write_tetra (abfd
, bfd_get_32 (abfd
, mmop
->buf
));
857 mmo_write_tetra_raw (abfd
, LOP_QUOTE_NEXT
);
860 && ! mmop
->have_error
861 && 4 == bfd_bwrite (loc
, 4, abfd
));
869 /* We must have flushed a previous remainder if we get one from
871 BFD_ASSERT (mmop
->byte_no
== 0);
872 memcpy (mmop
->buf
, loc
, len
);
877 mmop
->have_error
= true;
881 /* Flush remaining bytes, from a previous mmo_write_chunk, zero-padded to
885 mmo_flush_chunk (bfd
*abfd
)
887 if (abfd
->tdata
.mmo_data
->byte_no
!= 0)
889 memset (abfd
->tdata
.mmo_data
->buf
+ abfd
->tdata
.mmo_data
->byte_no
,
890 0, 4 - abfd
->tdata
.mmo_data
->byte_no
);
891 mmo_write_tetra (abfd
,
892 bfd_get_32 (abfd
, abfd
->tdata
.mmo_data
->buf
));
893 abfd
->tdata
.mmo_data
->byte_no
= 0;
896 return ! abfd
->tdata
.mmo_data
->have_error
;
899 /* Same, but from a list. */
902 mmo_write_chunk_list (bfd
*abfd
, mmo_data_list_type
*datap
)
904 for (; datap
!= NULL
; datap
= datap
->next
)
905 if (! mmo_write_chunk (abfd
, datap
->data
, datap
->size
))
908 return mmo_flush_chunk (abfd
);
911 /* Write a lop_loc and some contents. A caller needs to call
912 mmo_flush_chunk after calling this function. The location is only
913 output if different than *LAST_VMAP, which is updated after this call. */
916 mmo_write_loc_chunk (bfd
*abfd
, bfd_vma vma
, const bfd_byte
*loc
,
917 unsigned int len
, bfd_vma
*last_vmap
)
919 /* Find an initial and trailing section of zero (aligned) tetras; we don't
920 need to write out zeros. FIXME: When we do this, we should emit
921 section size and address specifiers, else objcopy can't always perform
922 an identity translation. Only do this if we *don't* have left-over
923 data from a previous write (and will not add any) or else the vma of
924 this chunk is *not* the next address, because then data isn't
925 tetrabyte-aligned and we're concatenating to that left-over data. */
928 && (abfd
->tdata
.mmo_data
->byte_no
== 0 || vma
!= *last_vmap
))
930 while (len
> 4 && bfd_get_32 (abfd
, loc
) == 0)
938 while (len
> 4 && bfd_get_32 (abfd
, loc
+ len
- 4) == 0)
942 /* Only write out the location if it's different than the one the caller
943 (supposedly) previously handled, accounting for omitted leading zeros. */
944 if (vma
!= *last_vmap
)
946 /* We might be in the middle of a sequence. */
947 mmo_flush_chunk (abfd
);
949 /* This should not happen during normal usage, but can presumably
950 happen with an erroneous linker-script, so handle gracefully.
951 Avoid Knuth-specific terms in the message, such as "tetrabyte".
952 Note that this function will get non-4-multiple lengths and
953 unaligned vmas but those come in tuples (mostly pairs) and are
954 continuous (i.e. the if-condition above false) and they are
955 group-wise aligned. */
959 /* xgettext:c-format */
960 (_("%pB: attempt to emit contents at non-multiple-of-4"
961 " address %#" PRIx64
""),
962 abfd
, (uint64_t) vma
);
963 bfd_set_error (bfd_error_bad_value
);
967 /* We always write the location as 64 bits; no use saving bytes
969 mmo_write_tetra_raw (abfd
, (LOP
<< 24) | (LOP_LOC
<< 16) | 2);
970 mmo_write_octa_raw (abfd
, vma
);
973 /* Update to reflect end of this chunk, with trailing zeros omitted. */
974 *last_vmap
= vma
+ len
;
976 return (! abfd
->tdata
.mmo_data
->have_error
977 && mmo_write_chunk (abfd
, loc
, len
));
980 /* Same, but from a list. */
983 mmo_write_loc_chunk_list (bfd
*abfd
, mmo_data_list_type
*datap
)
985 /* Get an address different than the address of the first chunk. */
986 bfd_vma last_vma
= datap
? datap
->where
- 1 : 0;
988 for (; datap
!= NULL
; datap
= datap
->next
)
989 if (! mmo_write_loc_chunk (abfd
, datap
->where
, datap
->data
, datap
->size
,
993 return mmo_flush_chunk (abfd
);
996 /* Make a .MMIX.spec_data.N section. */
999 mmo_get_generic_spec_data_section (bfd
*abfd
, int spec_data_number
)
1002 char secname
[sizeof (MMIX_OTHER_SPEC_SECTION_PREFIX
) + 20]
1003 = MMIX_OTHER_SPEC_SECTION_PREFIX
;
1005 sprintf (secname
+ strlen (MMIX_OTHER_SPEC_SECTION_PREFIX
),
1006 "%d", spec_data_number
);
1008 sec
= mmo_make_section (abfd
, secname
);
1013 /* Make a special section for SPEC_DATA_NUMBER. If it is the one we use
1014 ourselves, parse some of its data to get at the section name. */
1017 mmo_get_spec_section (bfd
*abfd
, int spec_data_number
)
1022 unsigned int secname_length
;
1024 bfd_vma section_length
;
1025 bfd_vma section_vma
;
1026 mmo_data_list_type
*loc
;
1030 /* If this isn't the "special" special data, then make a placeholder
1032 if (spec_data_number
!= SPEC_DATA_SECTION
)
1033 return mmo_get_generic_spec_data_section (abfd
, spec_data_number
);
1035 /* Seek back to this position if there was a format error. */
1036 orig_pos
= bfd_tell (abfd
);
1038 /* Read the length (in 32-bit words). */
1039 if (bfd_bread (buf
, 4, abfd
) != 4)
1044 if (buf
[1] != LOP_QUOTE
)
1047 if (bfd_bread (buf
, 4, abfd
) != 4)
1051 /* We don't care to keep the name length accurate. It's
1053 secname_length
= bfd_get_32 (abfd
, buf
) * 4;
1055 /* Check section name length for sanity. */
1056 if (secname_length
> MAX_SECTION_NAME_SIZE
)
1059 /* This should be free'd regardless if a section is created. */
1060 secname
= bfd_malloc (secname_length
+ 1);
1061 secname
[secname_length
] = 0;
1063 for (i
= 0; i
< secname_length
/ 4; i
++)
1065 if (bfd_bread (secname
+ i
* 4, 4, abfd
) != 4)
1066 goto format_error_free
;
1068 if (secname
[i
* 4] == (char) LOP
)
1070 /* A bit of overkill, but we handle char 0x98 in a section name,
1071 and recognize misparsing. */
1072 if (secname
[i
* 4 + 1] != LOP_QUOTE
1073 || bfd_bread (secname
+ i
* 4, 4, abfd
) != 4)
1074 /* Whoops. We thought this was a name, and now we found a
1075 non-lop_quote lopcode before we parsed the whole length of
1076 the name. Signal end-of-file in the same manner. */
1077 goto format_error_free
;
1081 /* Get the section flags. */
1082 if (bfd_bread (buf
, 4, abfd
) != 4
1084 && (buf
[1] != LOP_QUOTE
|| bfd_bread (buf
, 4, abfd
) != 4)))
1085 goto format_error_free
;
1087 flags
= bfd_get_32 (abfd
, buf
);
1089 /* Get the section length. */
1090 if (bfd_bread (buf
, 4, abfd
) != 4
1092 && (buf
[1] != LOP_QUOTE
|| bfd_bread (buf
, 4, abfd
) != 4)))
1093 goto format_error_free
;
1095 section_length
= (bfd_vma
) bfd_get_32 (abfd
, buf
) << 32;
1097 /* That's the first, high-part. Now get the low part. */
1099 if (bfd_bread (buf
, 4, abfd
) != 4
1101 && (buf
[1] != LOP_QUOTE
|| bfd_bread (buf
, 4, abfd
) != 4)))
1102 goto format_error_free
;
1104 section_length
|= (bfd_vma
) bfd_get_32 (abfd
, buf
);
1106 /* Check the section length for sanity. */
1107 if (section_length
> MAX_ARTIFICIAL_SECTION_SIZE
)
1108 goto format_error_free
;
1110 /* Get the section VMA. */
1111 if (bfd_bread (buf
, 4, abfd
) != 4
1113 && (buf
[1] != LOP_QUOTE
|| bfd_bread (buf
, 4, abfd
) != 4)))
1114 goto format_error_free
;
1116 section_vma
= (bfd_vma
) bfd_get_32 (abfd
, buf
) << 32;
1118 /* That's the first, high-part. Now get the low part. */
1119 if (bfd_bread (buf
, 4, abfd
) != 4
1121 && (buf
[1] != LOP_QUOTE
|| bfd_bread (buf
, 4, abfd
) != 4)))
1122 goto format_error_free
;
1124 section_vma
|= (bfd_vma
) bfd_get_32 (abfd
, buf
);
1126 sec
= mmo_make_section (abfd
, secname
);
1131 /* We allocate a buffer here for the advertised size, with head room for
1132 tetrabyte alignment. */
1133 loc
= bfd_zmalloc (section_length
+ 3
1134 + sizeof (struct mmo_data_list_struct
));
1138 /* Use a TETRA-rounded size for the allocated buffer; we set the
1139 "visible" section size below. */
1140 loc
->size
= (section_length
+ 3) & ~3;
1142 /* Add in the section flags we found to those bfd entered during this
1143 process and set the contents. */
1144 if (!bfd_set_section_flags (sec
,
1145 (bfd_sec_flags_from_mmo_flags (flags
)
1146 | bfd_section_flags (sec
)
1147 | (section_length
!= 0 ? SEC_HAS_CONTENTS
: 0)))
1148 || !bfd_set_section_size (sec
, sec
->size
+ section_length
)
1149 /* Set VMA only for the first occurrence. */
1150 || (!sec
->user_set_vma
&& !bfd_set_section_vma (sec
, section_vma
)))
1152 /* If we get an error for any of the calls above, signal more than
1153 just a format error for the spec section. */
1158 if (mmo_section_data (sec
)->tail
!= NULL
)
1159 mmo_section_data (sec
)->tail
->next
= loc
;
1161 mmo_section_data (sec
)->head
= loc
;
1162 mmo_section_data (sec
)->tail
= loc
;
1163 loc
->where
= section_vma
;
1170 if (bfd_seek (abfd
, orig_pos
, SEEK_SET
) != 0)
1173 return mmo_get_generic_spec_data_section (abfd
, spec_data_number
);
1176 /* Read a byte, but read from file in multiples of 32-bit words. */
1179 mmo_get_byte (bfd
*abfd
)
1183 if (abfd
->tdata
.mmo_data
->byte_no
== 0)
1185 if (! abfd
->tdata
.mmo_data
->have_error
1186 && bfd_bread (abfd
->tdata
.mmo_data
->buf
, 4, abfd
) != 4)
1188 abfd
->tdata
.mmo_data
->have_error
= true;
1190 /* A value somewhat safe against tripping on some inconsistency
1191 when mopping up after this error. */
1196 retval
= abfd
->tdata
.mmo_data
->buf
[abfd
->tdata
.mmo_data
->byte_no
];
1197 abfd
->tdata
.mmo_data
->byte_no
= (abfd
->tdata
.mmo_data
->byte_no
+ 1) % 4;
1202 /* Write a byte, in multiples of 32-bit words. */
1205 mmo_write_byte (bfd
*abfd
, bfd_byte value
)
1207 abfd
->tdata
.mmo_data
->buf
[(abfd
->tdata
.mmo_data
->byte_no
++ % 4)] = value
;
1208 if ((abfd
->tdata
.mmo_data
->byte_no
% 4) == 0)
1210 if (! abfd
->tdata
.mmo_data
->have_error
1211 && bfd_bwrite (abfd
->tdata
.mmo_data
->buf
, 4, abfd
) != 4)
1212 abfd
->tdata
.mmo_data
->have_error
= true;
1216 /* Create a symbol. */
1219 mmo_create_symbol (bfd
*abfd
, const char *symname
, bfd_vma addr
, enum
1220 mmo_sym_type sym_type
, unsigned int serno
)
1222 struct mmo_symbol
*n
;
1224 n
= (struct mmo_symbol
*) bfd_alloc (abfd
, sizeof (struct mmo_symbol
));
1228 n
->name
= bfd_alloc (abfd
, strlen (symname
) + 1);
1229 if (n
->name
== NULL
)
1232 strcpy (n
->name
, symname
);
1235 n
->sym_type
= sym_type
;
1238 if (abfd
->tdata
.mmo_data
->symbols
== NULL
)
1239 abfd
->tdata
.mmo_data
->symbols
= n
;
1241 abfd
->tdata
.mmo_data
->symtail
->next
= n
;
1242 abfd
->tdata
.mmo_data
->symtail
= n
;
1247 /* Check that :Main equals the last octa of the .MMIX.reg_contents
1248 section, as it's the one place we're sure to pass when reading a mmo
1249 object. For written objects, we do it while setting the symbol
1251 if (strcmp (symname
, MMIX_START_SYMBOL_NAME
) == 0
1252 && bfd_get_start_address (abfd
) != addr
1253 && !mmo_ignore_symbol_consistency (abfd
))
1256 (_("%pB: invalid mmo file: initialization value for $255"
1257 " is not `Main'\n"),
1259 bfd_set_error (bfd_error_bad_value
);
1266 /* Read in symbols. */
1269 mmo_get_symbols (bfd
*abfd
)
1273 Symbol-table, mmo section mapping, File layout, mmo
1277 From mmixal.w (or really, the generated mmixal.tex) in the
1278 MMIXware package which also contains the @command{mmix} simulator:
1279 ``Symbols are stored and retrieved by means of a @samp{ternary
1280 search trie}, following ideas of Bentley and Sedgewick. (See
1281 ACM--SIAM Symp.@: on Discrete Algorithms @samp{8} (1997), 360--369;
1282 R.@:Sedgewick, @samp{Algorithms in C} (Reading, Mass.@:
1283 Addison--Wesley, 1998), @samp{15.4}.) Each trie node stores a
1284 character, and there are branches to subtries for the cases where
1285 a given character is less than, equal to, or greater than the
1286 character in the trie. There also is a pointer to a symbol table
1287 entry if a symbol ends at the current node.''
1289 So it's a tree encoded as a stream of bytes. The stream of bytes
1290 acts on a single virtual global symbol, adding and removing
1291 characters and signalling complete symbol points. Here, we read
1292 the stream and create symbols at the completion points.
1294 First, there's a control byte <<m>>. If any of the listed bits
1295 in <<m>> is nonzero, we execute what stands at the right, in
1299 | 0x40 - Traverse left trie.
1300 | (Read a new command byte and recurse.)
1303 | 0x2f - Read the next byte as a character and store it in the
1304 | current character position; increment character position.
1305 | Test the bits of <<m>>:
1308 | 0x80 - The character is 16-bit (so read another byte,
1309 | merge into current character.
1312 | 0xf - We have a complete symbol; parse the type, value
1313 | and serial number and do what should be done
1314 | with a symbol. The type and length information
1315 | is in j = (m & 0xf).
1317 | (MMO3_REGQUAL_BITS)
1318 | j == 0xf: A register variable. The following
1319 | byte tells which register.
1320 | j <= 8: An absolute symbol. Read j bytes as the
1321 | big-endian number the symbol equals.
1322 | A j = 2 with two zero bytes denotes an
1324 | j > 8: As with j <= 8, but add (0x20 << 56)
1325 | to the value in the following j - 8
1328 | Then comes the serial number, as a variant of
1329 | uleb128, but better named ubeb128:
1330 | Read bytes and shift the previous value left 7
1331 | (multiply by 128). Add in the new byte, repeat
1332 | until a byte has bit 7 set. The serial number
1333 | is the computed value minus 128.
1336 | 0x20 - Traverse middle trie. (Read a new command byte
1337 | and recurse.) Decrement character position.
1340 | 0x10 - Traverse right trie. (Read a new command byte and
1343 Let's look again at the <<lop_stab>> for the trivial file
1344 (@pxref{File layout}).
1346 | 0x980b0000 - lop_stab for ":Main" = 0, serial 1.
1353 This forms the trivial trie (note that the path between ``:'' and
1354 ``M'' is redundant):
1365 | 016e "n" is the last character in a full symbol, and
1366 | with a value represented in one byte.
1367 | 00 The value is 0.
1368 | 81 The serial number is 1. */
1370 bfd_byte m
= mmo_get_byte (abfd
);
1372 /* Check first if we have a bad hair day. */
1373 if (abfd
->tdata
.mmo_data
->have_error
)
1377 /* Traverse left trie. */
1378 mmo_get_symbols (abfd
);
1380 if (m
& MMO3_SYMBITS
)
1382 bfd_byte c
= mmo_get_byte (abfd
);
1383 bfd_byte j
= m
& MMO3_TYPEBITS
;
1385 enum mmo_sym_type sym_type
;
1386 unsigned int serno
= 0;
1391 bfd_byte c2
= mmo_get_byte (abfd
);
1393 /* A two-byte character. We can't grok this, but neither can
1394 mmotype, for other cases than the second byte being zero. */
1398 abfd
->tdata
.mmo_data
->lop_stab_symbol
1399 [abfd
->tdata
.mmo_data
->symbol_position
] = 0;
1402 /* xgettext:c-format */
1403 (_("%pB: unsupported wide character sequence"
1404 " 0x%02X 0x%02X after symbol name starting with `%s'\n"),
1405 abfd
, c
, c2
, abfd
->tdata
.mmo_data
->lop_stab_symbol
);
1406 bfd_set_error (bfd_error_bad_value
);
1407 abfd
->tdata
.mmo_data
->have_error
= true;
1414 if (abfd
->tdata
.mmo_data
->symbol_position
1415 >= abfd
->tdata
.mmo_data
->max_symbol_length
)
1418 /* xgettext:c-format */
1419 (_("%pB: symbol name exceeds given max length of %d"),
1420 abfd
, abfd
->tdata
.mmo_data
->max_symbol_length
);
1421 abfd
->tdata
.mmo_data
->have_error
= true;
1424 abfd
->tdata
.mmo_data
->lop_stab_symbol
[abfd
->tdata
.mmo_data
->symbol_position
++] = c
;
1425 abfd
->tdata
.mmo_data
->lop_stab_symbol
[abfd
->tdata
.mmo_data
->symbol_position
] = 0;
1427 if (j
& MMO3_REGQUAL_BITS
)
1429 if (j
== MMO3_REGQUAL_BITS
)
1431 sym_type
= mmo_reg_sym
;
1432 addr
= mmo_get_byte (abfd
);
1438 for (i
= 0; i
< j
; i
++)
1439 addr
= (addr
<< 8) + mmo_get_byte (abfd
);
1441 if (addr
== 0 && j
== MMO3_UNDEF
)
1442 sym_type
= mmo_undef_sym
;
1444 sym_type
= mmo_abs_sym
;
1450 for (i
= MMO3_DATA
; i
< j
; i
++)
1451 addr
= (addr
<< 8) + mmo_get_byte (abfd
);
1453 addr
+= (bfd_vma
) 0x20 << 56;
1454 sym_type
= mmo_data_sym
;
1457 /* Get the serial number. */
1460 k
= mmo_get_byte (abfd
);
1461 serno
= (serno
<< 7) + k
;
1466 /* Got it. Now enter it. Skip a leading ":". */
1467 if (! abfd
->tdata
.mmo_data
->have_error
1468 && ! mmo_create_symbol (abfd
,
1469 abfd
->tdata
.mmo_data
->lop_stab_symbol
1471 addr
, sym_type
, serno
))
1472 abfd
->tdata
.mmo_data
->have_error
= true;
1475 if (m
& MMO3_MIDDLE
)
1476 /* Traverse middle trie. */
1477 mmo_get_symbols (abfd
);
1479 abfd
->tdata
.mmo_data
->symbol_position
--;
1483 /* Traverse right trie. */
1484 mmo_get_symbols (abfd
);
1486 return ! abfd
->tdata
.mmo_data
->have_error
;
1489 /* Get the location of memory area [VMA..VMA + SIZE - 1], which we think
1490 is in section SEC. Adjust and reallocate zero-initialized contents.
1491 If there's new contents, allocate to the next multiple of
1492 MMO_SEC_CONTENTS_CHUNK_SIZE. */
1495 mmo_get_loc (asection
*sec
, bfd_vma vma
, unsigned int size
)
1497 bfd_size_type allocated_size
;
1498 struct mmo_section_data_struct
*sdatap
= mmo_section_data (sec
);
1499 struct mmo_data_list_struct
*datap
= sdatap
->head
;
1500 struct mmo_data_list_struct
*entry
;
1502 /* First search the list to see if we have the requested chunk in one
1503 piece, or perhaps if we have a suitable chunk with room to fit. */
1504 for (; datap
!= NULL
; datap
= datap
->next
)
1506 if (datap
->where
<= vma
1507 && datap
->size
>= size
1508 && datap
->size
- size
>= vma
- datap
->where
)
1509 return datap
->data
+ (vma
- datap
->where
);
1510 else if (datap
->where
<= vma
1511 && datap
->allocated_size
>= size
1512 && datap
->allocated_size
- size
>= vma
- datap
->where
1513 /* Only munch on the "allocated size" if it does not
1514 overlap the next chunk. */
1515 && (datap
->next
== NULL
|| datap
->next
->where
>= vma
+ size
))
1517 /* There was room allocated, but the size wasn't set to include
1519 datap
->size
= vma
- datap
->where
+ size
;
1521 /* Update the section size. This happens only if we update the
1522 32-bit-aligned chunk size. Callers that have
1523 non-32-bit-aligned sections should do all allocation and
1524 size-setting by themselves or at least set the section size
1525 after the last allocating call to this function. */
1526 if (vma
- sec
->vma
+ size
> sec
->size
)
1527 sec
->size
= vma
- sec
->vma
+ size
;
1529 return datap
->data
+ (vma
- datap
->where
);
1533 /* Not found; allocate a new block. First check in case we get a
1534 request for a size split up over several blocks; we'll have to return
1535 NULL for those cases, requesting the caller to split up the request.
1536 Requests with an address aligned on MMO_SEC_CONTENTS_CHUNK_SIZE bytes and
1537 for no more than MMO_SEC_CONTENTS_CHUNK_SIZE will always get resolved. */
1539 for (datap
= sdatap
->head
; datap
!= NULL
; datap
= datap
->next
)
1540 if ((datap
->where
<= vma
&& datap
->size
> vma
- datap
->where
)
1541 || (datap
->where
< vma
+ size
1542 && datap
->where
+ datap
->size
>= vma
+ size
))
1546 = (size
+ MMO_SEC_CONTENTS_CHUNK_SIZE
- 1) & ~(MMO_SEC_CONTENTS_CHUNK_SIZE
- 1);
1547 entry
= (mmo_data_list_type
*)
1548 bfd_zalloc (sec
->owner
, sizeof (mmo_data_list_type
) + allocated_size
);
1553 entry
->allocated_size
= allocated_size
;
1555 datap
= sdatap
->head
;
1557 /* Sort the records by address. Optimize for the common case of adding
1558 a record to the end of the list. */
1559 if (sdatap
->tail
!= NULL
&& entry
->where
>= sdatap
->tail
->where
)
1561 sdatap
->tail
->next
= entry
;
1563 sdatap
->tail
= entry
;
1567 mmo_data_list_type
**look
;
1568 for (look
= &sdatap
->head
;
1569 *look
!= NULL
&& (*look
)->where
< entry
->where
;
1570 look
= &(*look
)->next
)
1572 entry
->next
= *look
;
1574 if (entry
->next
== NULL
)
1576 sdatap
->tail
= entry
;
1578 /* We get here for the first time (at other times too) for this
1579 section. Say we have contents. */
1580 if (!bfd_set_section_flags (sec
, (bfd_section_flags (sec
)
1581 | SEC_HAS_CONTENTS
)))
1586 /* Update the section size. This happens only when we add contents and
1587 re-size as we go. The section size will then be aligned to 32 bits. */
1588 if (vma
- sec
->vma
+ size
> sec
->size
)
1589 sec
->size
= vma
- sec
->vma
+ size
;
1593 /* Set sizes once we've read in all sections. */
1596 mmo_map_set_sizes (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
,
1597 void *ignored ATTRIBUTE_UNUSED
)
1599 sec
->lma
= sec
->vma
;
1602 /* Read the mmo file and turn it into sections. */
1605 mmo_scan (bfd
*abfd
)
1608 unsigned int lineno
= 1;
1611 asection
*sec
= NULL
;
1612 asection
*non_spec_sec
= NULL
;
1613 bfd_vma non_spec_vma
= 0;
1614 bfd_size_type nbytes_read
= 0;
1615 /* Buffer with room to read a 64-bit value. */
1617 file_ptr stab_loc
= -1;
1618 char *file_names
[256];
1621 memset (file_names
, 0, sizeof (file_names
));
1623 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0)
1626 while ((nbytes_read
= bfd_bread (buf
, 4, abfd
)) == 4)
1630 unsigned int y
= bfd_get_8 (abfd
, buf
+ 2);
1631 unsigned int z
= bfd_get_8 (abfd
, buf
+ 3);
1633 /* Change back to the original section for lopcodes other
1634 than LOP_QUOTE that comes after a LOP_SPEC. */
1635 if ((buf
[1] != LOP_QUOTE
|| y
!= 0 || z
!= 1)
1636 && non_spec_sec
!= NULL
)
1640 non_spec_sec
= NULL
;
1647 /* xgettext:c-format */
1648 (_("%pB: invalid mmo file: unsupported lopcode `%d'\n"),
1650 bfd_set_error (bfd_error_bad_value
);
1654 /* Quote the next 32-bit word. */
1655 if (y
!= 0 || z
!= 1)
1658 /* xgettext:c-format */
1659 (_("%pB: invalid mmo file: expected YZ = 1"
1660 " got YZ = %d for lop_quote\n"),
1662 bfd_set_error (bfd_error_bad_value
);
1665 if (bfd_bread (buf
, 4, abfd
) != 4)
1670 sec
= bfd_make_section_old_way (abfd
, MMO_TEXT_SECTION_NAME
);
1671 if (!mmo_xore_32 (sec
, vma
, bfd_get_32 (abfd
, buf
)))
1673 bfd_set_error (bfd_error_bad_value
);
1681 /* Set vma (and section). */
1682 vma
= (bfd_vma
) y
<< 56;
1685 /* Get a 32-bit value. */
1686 if (bfd_bread (buf
, 4, abfd
) != 4)
1689 vma
+= bfd_get_32 (abfd
, buf
);
1693 /* Get a 64-bit value. */
1694 if (bfd_bread (buf
, 8, abfd
) != 8)
1697 vma
+= bfd_get_64 (abfd
, buf
);
1702 /* xgettext:c-format */
1703 (_("%pB: invalid mmo file: expected z = 1 or z = 2,"
1704 " got z = %d for lop_loc\n"),
1706 bfd_set_error (bfd_error_bad_value
);
1710 /* When we decide which section the data goes into, we might
1711 create the section. If that happens, make sure the VMA at
1712 creation time is tetra-aligned. */
1713 sec
= mmo_decide_section (abfd
, vma
& ~3);
1719 /* Move forward within the same section. */
1722 sec
= mmo_decide_section (abfd
, vma
);
1728 /* A fixup: Store the current vma somewhere. Position using
1729 same format as LOP_LOC. */
1731 bfd_vma p
= (bfd_vma
) y
<< 56;
1736 /* Get a 32-bit value. */
1737 if (bfd_bread (buf
, 4, abfd
) != 4)
1740 p
+= bfd_get_32 (abfd
, buf
);
1744 /* Get a 64-bit value. */
1745 if (bfd_bread (buf
, 8, abfd
) != 8)
1748 p
+= bfd_get_64 (abfd
, buf
);
1753 /* xgettext:c-format */
1754 (_("%pB: invalid mmo file: expected z = 1 or z = 2,"
1755 " got z = %d for lop_fixo\n"),
1757 bfd_set_error (bfd_error_bad_value
);
1761 /* The section where we store this address might be a
1762 different one than the current section. */
1763 fixosec
= mmo_decide_section (abfd
, p
);
1764 if (fixosec
== NULL
)
1766 if (!mmo_xore_64 (fixosec
, p
, vma
))
1768 bfd_set_error (bfd_error_bad_value
);
1775 /* A fixup: Store YZ of this lopcode into YZ at vma - 4 * yz. */
1777 unsigned int yz
= (y
* 256 + z
);
1778 bfd_vma p
= vma
+ 2 - 4 * yz
;
1779 asection
*fixrsec
= mmo_decide_section (abfd
, p
);
1780 if (fixrsec
== NULL
)
1782 if (!mmo_xore_16 (fixrsec
, p
, yz
))
1784 bfd_set_error (bfd_error_bad_value
);
1791 /* A fixup, similar to lop_fixr, but taking larger numbers
1792 and can change branches into the opposite direction
1802 /* xgettext:c-format */
1803 (_("%pB: invalid mmo file: expected y = 0,"
1804 " got y = %d for lop_fixrx\n"),
1806 bfd_set_error (bfd_error_bad_value
);
1810 if (z
!= 16 && z
!= 24)
1813 /* xgettext:c-format */
1814 (_("%pB: invalid mmo file: expected z = 16 or z = 24,"
1815 " got z = %d for lop_fixrx\n"),
1817 bfd_set_error (bfd_error_bad_value
);
1821 /* Get the next 32-bit value. */
1822 if (bfd_bread (buf
, 4, abfd
) != 4)
1825 delta
= bfd_get_32 (abfd
, buf
);
1827 /* Do an, ehm, involved calculation for the location of
1828 the fixup. See mmixal documentation for a verbose
1829 explanation. We follow it verbosely here for the
1832 p
= vma
- 4 * delta
;
1833 else if (buf
[0] == 1)
1834 p
= vma
- 4 * ((delta
& 0xffffff) - (1 << z
));
1838 /* xgettext:c-format */
1839 (_("%pB: invalid mmo file: leading byte of operand word"
1840 " must be 0 or 1, got %d for lop_fixrx\n"),
1842 bfd_set_error (bfd_error_bad_value
);
1846 fixrsec
= mmo_decide_section (abfd
, vma
);
1847 if (fixrsec
== NULL
)
1849 if (!mmo_xore_32 (fixrsec
, p
, delta
))
1851 bfd_set_error (bfd_error_bad_value
);
1858 /* Set current file and perhaps the file name. Reset line
1862 char *fname
= bfd_malloc (z
* 4 + 1);
1867 /* xgettext:c-format */
1868 (_("%pB: cannot allocate file name for file number %d,"
1870 abfd
, y
, z
* 4 + 1);
1871 bfd_set_error (bfd_error_system_call
);
1877 for (i
= 0; i
< z
; i
++)
1879 if (bfd_bread (fname
+ i
* 4, 4, abfd
) != 4)
1886 if (file_names
[y
] != NULL
)
1889 /* xgettext:c-format */
1890 (_("%pB: invalid mmo file: file number %d `%s',"
1891 " was already entered as `%s'\n"),
1892 abfd
, y
, fname
, file_names
[y
]);
1893 bfd_set_error (bfd_error_bad_value
);
1897 file_names
[y
] = fname
;
1900 if (file_names
[y
] == NULL
)
1903 /* xgettext:c-format */
1904 (_("%pB: invalid mmo file: file name for number %d"
1905 " was not specified before use\n"),
1907 bfd_set_error (bfd_error_bad_value
);
1915 /* Set line number. */
1916 lineno
= y
* 256 + z
;
1917 /* FIXME: Create a sequence of mmo-specific line number
1918 entries for each section, then translate into canonical
1923 /* Special data follows until the next non-lop_quote
1927 sec
= mmo_get_spec_section (abfd
, y
* 256 + z
);
1936 /* We ignore header information, except we read in the
1937 creation time from the first 32-bit word with the time
1938 in seconds since era. */
1940 && bfd_bread (abfd
->tdata
.mmo_data
->created
, 4,
1944 for (i
= 1; i
< z
; i
++)
1945 if (bfd_bread (buf
, 4, abfd
) != 4)
1951 /* This tells of the contents of registers $Z..$255 at
1952 startup. We make a section out of it, with VMA = Z * 8,
1953 but only if Z != 255 or the contents is non-zero. */
1958 bfd_vma startaddr_octa
;
1960 /* Read first octaword outside loop to simplify logic when
1961 excluding the Z == 255, octa == 0 case. */
1962 if (bfd_bread (buf
, 8, abfd
) != 8)
1965 first_octa
= bfd_get_64 (abfd
, buf
);
1967 /* Don't emit contents for the trivial case which is
1968 always present; $255 pointing to Main. */
1972 = bfd_make_section_old_way (abfd
,
1973 MMIX_REG_CONTENTS_SECTION_NAME
);
1974 rsec
->flags
|= SEC_LINKER_CREATED
;
1976 loc
= mmo_get_loc (rsec
, z
* 8, (255 - z
) * 8);
1979 bfd_set_error (bfd_error_bad_value
);
1982 bfd_put_64 (abfd
, first_octa
, loc
);
1984 for (i
= z
+ 1; i
< 255; i
++)
1986 if (bfd_bread (loc
+ (i
- z
) * 8, 8, abfd
) != 8)
1990 /* Read out the last octabyte, and use it to set the
1992 if (bfd_bread (buf
, 8, abfd
) != 8)
1995 startaddr_octa
= bfd_get_64 (abfd
, buf
);
1998 startaddr_octa
= first_octa
;
2000 if (! bfd_set_start_address (abfd
, startaddr_octa
))
2002 /* Currently this can't fail, but this should handle
2004 bfd_set_error (bfd_error_bad_value
);
2011 /* We read in the symbols now, not later. */
2012 if (y
!= 0 || z
!= 0)
2015 /* xgettext:c-format */
2016 (_("%pB: invalid mmo file: fields y and z of lop_stab"
2017 " non-zero, y: %d, z: %d\n"),
2019 bfd_set_error (bfd_error_bad_value
);
2023 /* Save the location, so we can check that YZ in the LOP_END
2025 stab_loc
= bfd_tell (abfd
);
2027 /* It's not said that an MMO can be without symbols (though
2028 mmixal will refuse to assemble files without Main), but
2029 it seems it would still be a valid mmo-file, so allow it.
2030 We detect the absence of a symbol area in that the upper
2031 limit is computed (from the lop_end YZ field) as 0.
2032 Don't call mmo_get_symbols; it can only detect the end of
2033 a valid symbol trie, not the absence of one. */
2034 if (abfd
->tdata
.mmo_data
->max_symbol_length
!= 0
2035 && ! mmo_get_symbols (abfd
))
2041 /* This must be the last 32-bit word in an mmo file.
2043 struct stat statbuf
;
2044 file_ptr curpos
= bfd_tell (abfd
);
2046 if (bfd_stat (abfd
, &statbuf
) < 0)
2049 if (statbuf
.st_size
!= curpos
)
2052 /* xgettext:c-format */
2053 (_("%pB: invalid mmo file: lop_end not last item in"
2056 bfd_set_error (bfd_error_bad_value
);
2060 /* Check that the YZ field is right. Subtract the size of
2061 this LOP_END in the calculation; YZ does not include
2063 if ((long) (y
* 256 + z
) * 4 != (curpos
- stab_loc
) - 4)
2066 /* xgettext:c-format */
2067 (_("%pB: invalid mmo file: YZ of lop_end (%ld)"
2068 " not equal to the number of tetras to the preceding"
2069 " lop_stab (%ld)\n"),
2070 abfd
, (long) (y
* 256 + z
),
2071 (long) (curpos
- stab_loc
- 4)/4);
2072 bfd_set_error (bfd_error_bad_value
);
2076 bfd_map_over_sections (abfd
, mmo_map_set_sizes
, NULL
);
2083 /* This wasn't a lopcode, so store it in the current section. */
2085 sec
= bfd_make_section_old_way (abfd
, MMO_TEXT_SECTION_NAME
);
2086 if (!mmo_xore_32 (sec
, vma
& ~3, bfd_get_32 (abfd
, buf
)))
2088 bfd_set_error (bfd_error_bad_value
);
2097 /* We know this file is a multiple of four bytes (checked in
2098 mmo_object_p), so if we got something other than 0, this was a bad
2099 file (although it's more likely we'll get 0 in that case too).
2100 If we got end-of-file, then there was no lop_stab, so the file has
2103 if (nbytes_read
!= 0)
2104 bfd_set_error (bfd_error_system_call
);
2106 bfd_set_error (bfd_error_bad_value
);
2111 /* Mark the .text and .data section with their normal attribute if they
2112 contain anything. This is not redundant wrt. mmo_decide_section,
2113 since that code might never execute, and conversely the alloc+code
2114 section flags must be set then. */
2115 sec
= bfd_get_section_by_name (abfd
, MMO_TEXT_SECTION_NAME
);
2117 && (bfd_section_flags (sec
) & SEC_HAS_CONTENTS
)
2118 && !bfd_set_section_flags (sec
, (bfd_section_flags (sec
)
2119 | SEC_ALLOC
| SEC_LOAD
| SEC_CODE
)))
2122 sec
= bfd_get_section_by_name (abfd
, MMO_DATA_SECTION_NAME
);
2124 && (bfd_section_flags (sec
) & SEC_HAS_CONTENTS
)
2125 && !bfd_set_section_flags (sec
, (bfd_section_flags (sec
)
2126 | SEC_ALLOC
| SEC_LOAD
| SEC_DATA
)))
2129 /* Free whatever resources we took. */
2130 for (i
= 0; i
< sizeof (file_names
) / sizeof (file_names
[0]); i
++)
2131 free (file_names
[i
]);
2135 /* A hook to set up object file dependent section information. For mmo,
2136 we point out the shape of allocated section contents. */
2139 mmo_new_section_hook (bfd
*abfd
, asection
*newsect
)
2141 if (!newsect
->used_by_bfd
)
2143 /* We zero-fill all fields and assume NULL is represented by an all
2144 zero-bit pattern. */
2145 newsect
->used_by_bfd
2146 = bfd_zalloc (abfd
, sizeof (struct mmo_section_data_struct
));
2147 if (!newsect
->used_by_bfd
)
2151 /* Always align to at least 32-bit words. */
2152 newsect
->alignment_power
= 2;
2153 return _bfd_generic_new_section_hook (abfd
, newsect
);
2156 /* We already have section contents loaded for sections that have
2160 mmo_get_section_contents (bfd
*abfd ATTRIBUTE_UNUSED
,
2164 bfd_size_type bytes_to_do
)
2166 /* Iterate over diminishing chunk sizes, copying contents, like
2167 mmo_set_section_contents. */
2170 /* A minor song-and-dance to make sure we're not bitten by the
2171 distant possibility of the cast from bfd_vma to int making the
2172 chunk zero-sized. */
2174 = (int) bytes_to_do
!= 0 ? bytes_to_do
: MMO_SEC_CONTENTS_CHUNK_SIZE
;
2178 loc
= mmo_get_loc (sec
, sec
->vma
+ offset
, chunk_size
);
2179 while (loc
== NULL
&& (chunk_size
/= 2) != 0);
2181 if (chunk_size
== 0)
2184 memcpy (location
, loc
, chunk_size
);
2186 location
= (bfd_byte
*) location
+ chunk_size
;
2187 bytes_to_do
-= chunk_size
;
2188 offset
+= chunk_size
;
2193 /* Return the amount of memory needed to read the symbol table. */
2196 mmo_get_symtab_upper_bound (bfd
*abfd
)
2198 return (abfd
->symcount
+ 1) * sizeof (asymbol
*);
2201 /* Sort mmo symbols by serial number. */
2204 mmo_sort_mmo_symbols (const void *arg1
, const void *arg2
)
2206 const struct mmo_symbol
*sym1
= *(const struct mmo_symbol
**) arg1
;
2207 const struct mmo_symbol
*sym2
= *(const struct mmo_symbol
**) arg2
;
2209 /* Sort by serial number first. */
2210 if (sym1
->serno
< sym2
->serno
)
2212 else if (sym1
->serno
> sym2
->serno
)
2215 /* Then sort by address of the table entries. */
2216 return ((const char *) arg1
- (const char *) arg2
);
2219 /* Translate the symbol table. */
2222 mmo_canonicalize_symtab (bfd
*abfd
, asymbol
**alocation
)
2224 unsigned int symcount
= bfd_get_symcount (abfd
);
2228 csymbols
= abfd
->tdata
.mmo_data
->csymbols
;
2229 if (csymbols
== NULL
&& symcount
!= 0)
2232 struct mmo_symbol
*s
;
2233 struct mmo_symbol
**msp
;
2235 /* First we store the symbols into the table we'll return, then we
2236 qsort it on the serial number, with secondary on the address of
2237 the symbol, to preserve order if there would be non-unique serial
2239 for (s
= abfd
->tdata
.mmo_data
->symbols
,
2240 msp
= (struct mmo_symbol
**) alocation
;
2247 qsort (alocation
, symcount
, sizeof (struct mmo_symbol
*),
2248 mmo_sort_mmo_symbols
);
2250 csymbols
= (asymbol
*) bfd_alloc (abfd
, symcount
* sizeof (asymbol
));
2251 if (csymbols
== NULL
)
2253 abfd
->tdata
.mmo_data
->csymbols
= csymbols
;
2255 for (msp
= (struct mmo_symbol
**) alocation
, c
= csymbols
;
2262 c
->value
= s
->value
;
2263 c
->flags
= BSF_GLOBAL
;
2265 if (s
->sym_type
== mmo_data_sym
)
2268 = bfd_get_section_by_name (abfd
, MMO_DATA_SECTION_NAME
);
2270 if (c
->section
== NULL
)
2271 c
->section
= bfd_abs_section_ptr
;
2273 c
->value
-= c
->section
->vma
;
2275 else if (s
->sym_type
== mmo_undef_sym
)
2276 c
->section
= bfd_und_section_ptr
;
2277 else if (s
->sym_type
== mmo_reg_sym
)
2280 = bfd_make_section_old_way (abfd
, MMIX_REG_SECTION_NAME
);
2281 c
->section
->flags
|= SEC_LINKER_CREATED
;
2286 = bfd_get_section_by_name (abfd
, MMO_TEXT_SECTION_NAME
);
2290 && c
->value
>= textsec
->vma
2291 && c
->value
<= textsec
->vma
+ textsec
->size
)
2293 c
->section
= textsec
;
2294 c
->value
-= c
->section
->vma
;
2296 /* In mmo, symbol types depend on the VMA. Therefore, if
2297 the data section isn't within the usual bounds, its
2298 symbols are marked as absolute. Correct that. This
2299 means we can't have absolute symbols with values matching
2300 data section addresses, but we also can't have with
2301 absolute symbols with values matching text section
2302 addresses. For such needs, use the ELF format. */
2304 = bfd_get_section_by_name (abfd
,
2305 MMO_DATA_SECTION_NAME
))
2307 && c
->value
>= datasec
->vma
2308 && c
->value
<= datasec
->vma
+ datasec
->size
)
2310 c
->section
= datasec
;
2311 c
->value
-= c
->section
->vma
;
2314 c
->section
= bfd_abs_section_ptr
;
2321 /* Last, overwrite the incoming table with the right-type entries. */
2322 for (i
= 0; i
< symcount
; i
++)
2323 *alocation
++ = csymbols
++;
2329 /* Get information about a symbol. */
2332 mmo_get_symbol_info (bfd
*ignore_abfd ATTRIBUTE_UNUSED
,
2333 asymbol
*symbol
, symbol_info
*ret
)
2335 bfd_symbol_info (symbol
, ret
);
2339 mmo_print_symbol (bfd
*abfd
, void *afile
, asymbol
*symbol
,
2340 bfd_print_symbol_type how
)
2342 FILE *file
= (FILE *) afile
;
2346 case bfd_print_symbol_name
:
2347 fprintf (file
, "%s", symbol
->name
);
2350 bfd_print_symbol_vandf (abfd
, file
, symbol
);
2352 fprintf (file
, " %-5s %s",
2353 symbol
->section
->name
,
2358 /* We can't map a file directly into executable code, so the
2359 size of header information is irrelevant. */
2362 mmo_sizeof_headers (bfd
*abfd ATTRIBUTE_UNUSED
,
2363 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
2368 /* Write the (section-neutral) file preamble. */
2371 mmo_internal_write_header (bfd
*abfd
)
2373 const char lop_pre_bfd
[] = { LOP
, LOP_PRE
, 1, 1};
2375 if (bfd_bwrite (lop_pre_bfd
, 4, abfd
) != 4)
2378 /* Copy creation time of original file. */
2379 if (bfd_bwrite (abfd
->tdata
.mmo_data
->created
, 4, abfd
) != 4)
2385 /* Write the LOP_POST record, with global register initializations.
2386 Z is the Z field of the LOP_POST, corresponding to 255 - number of
2387 registers at DATA. The Z = 255 field is filled in with the
2391 mmo_internal_write_post (bfd
*abfd
, int z
, asection
*sec
)
2395 mmo_write_tetra_raw (abfd
, (LOP
<< 24) | (LOP_POST
<< 16) | z
);
2397 for (i
= z
; i
< 255; i
++)
2399 bfd_byte
*data
= mmo_get_loc (sec
, i
* 8, 8);
2401 if (bfd_bwrite (data
, 8, abfd
) != 8)
2405 /* For Z == $255, we always emit the start location; supposedly Main,
2406 but we have it handy at bfd_get_start_address. If we're called with
2407 Z == 255, don't assume DATA is valid. */
2408 bfd_put_64 (abfd
, bfd_get_start_address (abfd
), buf
);
2410 return ! abfd
->tdata
.mmo_data
->have_error
&& bfd_bwrite (buf
, 8, abfd
) == 8;
2413 /* Translate to and from BFD flags. This is to make sure that we don't
2414 get bitten by BFD flag number changes. */
2417 mmo_sec_flags_from_bfd_flags (flagword flags
)
2419 flagword oflags
= 0;
2421 if (flags
& SEC_ALLOC
)
2422 oflags
|= MMO_SEC_ALLOC
;
2423 if (flags
& SEC_LOAD
)
2424 oflags
|= MMO_SEC_LOAD
;
2425 if (flags
& SEC_RELOC
)
2426 oflags
|= MMO_SEC_RELOC
;
2427 if (flags
& SEC_READONLY
)
2428 oflags
|= MMO_SEC_READONLY
;
2429 if (flags
& SEC_CODE
)
2430 oflags
|= MMO_SEC_CODE
;
2431 if (flags
& SEC_DATA
)
2432 oflags
|= MMO_SEC_DATA
;
2433 if (flags
& SEC_NEVER_LOAD
)
2434 oflags
|= MMO_SEC_NEVER_LOAD
;
2435 if (flags
& SEC_IS_COMMON
)
2436 oflags
|= MMO_SEC_IS_COMMON
;
2437 if (flags
& SEC_DEBUGGING
)
2438 oflags
|= MMO_SEC_DEBUGGING
;
2444 bfd_sec_flags_from_mmo_flags (flagword flags
)
2446 flagword oflags
= 0;
2448 if (flags
& MMO_SEC_ALLOC
)
2449 oflags
|= SEC_ALLOC
;
2450 if (flags
& MMO_SEC_LOAD
)
2452 if (flags
& MMO_SEC_RELOC
)
2453 oflags
|= SEC_RELOC
;
2454 if (flags
& MMO_SEC_READONLY
)
2455 oflags
|= SEC_READONLY
;
2456 if (flags
& MMO_SEC_CODE
)
2458 if (flags
& MMO_SEC_DATA
)
2460 if (flags
& MMO_SEC_NEVER_LOAD
)
2461 oflags
|= SEC_NEVER_LOAD
;
2462 if (flags
& MMO_SEC_IS_COMMON
)
2463 oflags
|= SEC_IS_COMMON
;
2464 if (flags
& MMO_SEC_DEBUGGING
)
2465 oflags
|= SEC_DEBUGGING
;
2470 /* Return TRUE iff the leading or trailing tetrabyte in SEC is defined and
2474 mmo_has_leading_or_trailing_zero_tetra_p (bfd
*abfd
, asection
*sec
)
2476 bfd_vma secaddr
= bfd_section_vma (sec
);
2481 if (bfd_get_32 (abfd
, mmo_get_loc (sec
, secaddr
, 4)) == 0
2482 && bfd_get_32 (abfd
,
2483 mmo_get_loc (sec
, secaddr
+ sec
->size
- 4, 4)) == 0)
2489 /* Write a section. */
2492 mmo_internal_write_section (bfd
*abfd
, asection
*sec
)
2494 /* We do it differently depending on what section this is:
2496 ".text": Output, prepended by information about the first source file
2497 (not yet implemented.)
2501 (".MMIX.reg_contents": Not handled here.)
2503 Anything else: Output inside a lop_spec 80, in the format described
2506 if (strcmp (sec
->name
, MMO_TEXT_SECTION_NAME
) == 0)
2508 bfd_vma secaddr
= bfd_section_vma (sec
);
2510 /* Because leading and trailing zeros are omitted in output, we need to
2511 specify the section boundaries so they're correct when the file
2512 is read in again. That's also the case if this section is
2513 specified as not within its usual boundaries or alignments. */
2515 && (secaddr
+ sec
->size
>= (bfd_vma
) 1 << 56
2516 || (secaddr
& 3) != 0
2517 || (sec
->size
& 3) != 0
2518 || mmo_has_leading_or_trailing_zero_tetra_p (abfd
, sec
)))
2520 if (!mmo_write_section_description (abfd
, sec
))
2524 /* FIXME: Output source file name and line number. */
2525 return mmo_write_loc_chunk_list (abfd
, mmo_section_data (sec
)->head
);
2527 else if (strcmp (sec
->name
, MMO_DATA_SECTION_NAME
) == 0)
2529 bfd_vma secaddr
= bfd_section_vma (sec
);
2531 /* Same goes as for MMO_TEXT_SECTION_NAME above. */
2533 && (secaddr
< (bfd_vma
) 0x20 << 56
2534 || secaddr
+ sec
->size
>= (bfd_vma
) 0x21 << 56
2535 || (secaddr
& 3) != 0
2536 || (sec
->size
& 3) != 0
2537 || mmo_has_leading_or_trailing_zero_tetra_p (abfd
, sec
)))
2539 if (!mmo_write_section_description (abfd
, sec
))
2543 return mmo_write_loc_chunk_list (abfd
, mmo_section_data (sec
)->head
);
2545 else if (strcmp (sec
->name
, MMIX_REG_CONTENTS_SECTION_NAME
) == 0)
2546 /* Not handled here. */
2548 /* This would normally be an abort call since this can't happen, but
2549 we don't do that. */
2550 bfd_set_error (bfd_error_bad_value
);
2553 else if (startswith (sec
->name
, MMIX_OTHER_SPEC_SECTION_PREFIX
))
2555 int n
= atoi (sec
->name
+ strlen (MMIX_OTHER_SPEC_SECTION_PREFIX
));
2557 mmo_write_tetra_raw (abfd
, (LOP
<< 24) | (LOP_SPEC
<< 16) | n
);
2558 return (! abfd
->tdata
.mmo_data
->have_error
2559 && mmo_write_chunk_list (abfd
, mmo_section_data (sec
)->head
));
2561 /* Ignore sections that are just allocated or empty; we write out
2563 else if ((bfd_section_flags (sec
) & SEC_HAS_CONTENTS
) != 0
2566 if (!mmo_write_section_description (abfd
, sec
))
2569 /* Writing a LOP_LOC ends the LOP_SPEC data, and makes data actually
2571 if (bfd_section_flags (sec
) & SEC_LOAD
)
2572 return (! abfd
->tdata
.mmo_data
->have_error
2573 && mmo_write_loc_chunk_list (abfd
,
2574 mmo_section_data (sec
)->head
));
2575 return (! abfd
->tdata
.mmo_data
->have_error
2576 && mmo_write_chunk_list (abfd
, mmo_section_data (sec
)->head
));
2579 /* Some section without contents. */
2583 /* Write the description of a section, extended-mmo-style. */
2586 mmo_write_section_description (bfd
*abfd
, asection
*sec
)
2588 /* Keep the following document-comment formatted the way it is. */
2591 mmo section mapping, , Symbol-table, mmo
2595 The implementation in BFD uses special data type 80 (decimal) to
2596 encapsulate and describe named sections, containing e.g.@: debug
2597 information. If needed, any datum in the encapsulation will be
2598 quoted using lop_quote. First comes a 32-bit word holding the
2599 number of 32-bit words containing the zero-terminated zero-padded
2600 segment name. After the name there's a 32-bit word holding flags
2601 describing the section type. Then comes a 64-bit big-endian word
2602 with the section length (in bytes), then another with the section
2603 start address. Depending on the type of section, the contents
2604 might follow, zero-padded to 32-bit boundary. For a loadable
2605 section (such as data or code), the contents might follow at some
2606 later point, not necessarily immediately, as a lop_loc with the
2607 same start address as in the section description, followed by the
2608 contents. This in effect forms a descriptor that must be emitted
2609 before the actual contents. Sections described this way must not
2612 For areas that don't have such descriptors, synthetic sections are
2613 formed by BFD. Consecutive contents in the two memory areas
2614 @samp{0x0000@dots{}00} to @samp{0x01ff@dots{}ff} and
2615 @samp{0x2000@dots{}00} to @samp{0x20ff@dots{}ff} are entered in
2616 sections named <<.text>> and <<.data>> respectively. If an area
2617 is not otherwise described, but would together with a neighboring
2618 lower area be less than @samp{0x40000000} bytes long, it is joined
2619 with the lower area and the gap is zero-filled. For other cases,
2620 a new section is formed, named <<.MMIX.sec.@var{n}>>. Here,
2621 @var{n} is a number, a running count through the mmo file,
2625 A loadable section specified as:
2627 | .section secname,"ax"
2628 | TETRA 1,2,3,4,-1,-2009
2631 and linked to address @samp{0x4}, is represented by the sequence:
2633 | 0x98080050 - lop_spec 80
2634 | 0x00000002 - two 32-bit words for the section name
2635 | 0x7365636e - "secn"
2636 | 0x616d6500 - "ame\0"
2637 | 0x00000033 - flags CODE, READONLY, LOAD, ALLOC
2638 | 0x00000000 - high 32 bits of section length
2639 | 0x0000001c - section length is 28 bytes; 6 * 4 + 1 + alignment to 32 bits
2640 | 0x00000000 - high 32 bits of section address
2641 | 0x00000004 - section address is 4
2642 | 0x98010002 - 64 bits with address of following data
2643 | 0x00000000 - high 32 bits of address
2644 | 0x00000004 - low 32 bits: data starts at address 4
2650 | 0xfffff827 - -2009
2651 | 0x50000000 - 80 as a byte, padded with zeros.
2653 Note that the lop_spec wrapping does not include the section
2654 contents. Compare this to a non-loaded section specified as:
2657 | TETRA 200001,100002
2660 This, when linked to address @samp{0x200000000000001c}, is
2663 | 0x98080050 - lop_spec 80
2664 | 0x00000002 - two 32-bit words for the section name
2665 | 0x7365636e - "thir"
2666 | 0x616d6500 - "dsec"
2667 | 0x00000010 - flag READONLY
2668 | 0x00000000 - high 32 bits of section length
2669 | 0x0000000c - section length is 12 bytes; 2 * 4 + 2 + alignment to 32 bits
2670 | 0x20000000 - high 32 bits of address
2671 | 0x0000001c - low 32 bits of address 0x200000000000001c
2672 | 0x00030d41 - 200001
2673 | 0x000186a2 - 100002
2674 | 0x26280000 - 38, 40 as bytes, padded with zeros
2676 For the latter example, the section contents must not be
2677 loaded in memory, and is therefore specified as part of the
2678 special data. The address is usually unimportant but might
2679 provide information for e.g.@: the DWARF 2 debugging format. */
2681 mmo_write_tetra_raw (abfd
, LOP_SPEC_SECTION
);
2682 mmo_write_tetra (abfd
, (strlen (sec
->name
) + 3) / 4);
2683 mmo_write_chunk (abfd
, (bfd_byte
*) sec
->name
, strlen (sec
->name
));
2684 mmo_flush_chunk (abfd
);
2685 /* FIXME: We can get debug sections (.debug_line & Co.) with a section
2686 flag still having SEC_RELOC set. Investigate. This might be true
2687 for all alien sections; perhaps mmo.em should clear that flag. Might
2688 be related to weak references. */
2689 mmo_write_tetra (abfd
,
2690 mmo_sec_flags_from_bfd_flags (bfd_section_flags (sec
)));
2691 mmo_write_octa (abfd
, sec
->size
);
2692 mmo_write_octa (abfd
, bfd_section_vma (sec
));
2696 /* We save up all data before output. */
2699 mmo_set_section_contents (bfd
*abfd ATTRIBUTE_UNUSED
, sec_ptr sec
,
2700 const void *location
, file_ptr offset
,
2701 bfd_size_type bytes_to_do
)
2703 /* Iterate over diminishing chunk sizes, copying contents. */
2706 /* A minor song-and-dance to make sure we're not bitten by the
2707 distant possibility of the cast from bfd_vma to int making the
2708 chunk zero-sized. */
2710 = (int) bytes_to_do
!= 0 ? bytes_to_do
: MMO_SEC_CONTENTS_CHUNK_SIZE
;
2714 loc
= mmo_get_loc (sec
, sec
->vma
+ offset
, chunk_size
);
2715 while (loc
== NULL
&& (chunk_size
/= 2) != 0);
2717 if (chunk_size
== 0)
2720 memcpy (loc
, location
, chunk_size
);
2722 location
= (bfd_byte
*) location
+ chunk_size
;
2723 bytes_to_do
-= chunk_size
;
2724 offset
+= chunk_size
;
2729 /* Add a symbol to a trie-tree. */
2732 mmo_internal_add_3_sym (bfd
*abfd
, struct mmo_symbol_trie
*rootp
,
2733 const struct mmo_symbol
*symp
)
2735 const char *name
= symp
->name
;
2736 struct mmo_symbol_trie
*trie
= rootp
;
2737 struct mmo_symbol_trie
**triep
= NULL
;
2739 while (*name
&& trie
!= NULL
)
2741 if (*name
< trie
->symchar
)
2743 triep
= &trie
->left
;
2746 else if (*name
> trie
->symchar
)
2748 triep
= &trie
->right
;
2751 else if (*name
== trie
->symchar
)
2753 triep
= &trie
->middle
;
2756 /* Make sure "trie" points to where we should fill in the
2757 current symbol whenever we've iterated through "name". We
2758 would lose the right position if we encounter "foobar" then
2761 trie
= trie
->middle
;
2767 /* Create middle branches for the rest of the characters. */
2768 trie
= bfd_zalloc (abfd
, sizeof (struct mmo_symbol_trie
));
2770 trie
->symchar
= *name
++;
2771 triep
= &trie
->middle
;
2774 /* We discover a duplicate symbol rather late in the process, but still;
2775 we discover it and bail out. */
2776 if (trie
->sym
.name
!= NULL
)
2779 /* xgettext:c-format */
2780 (_("%pB: invalid symbol table: duplicate symbol `%s'\n"),
2781 abfd
, trie
->sym
.name
);
2782 bfd_set_error (bfd_error_bad_value
);
2786 memcpy (&trie
->sym
, symp
, sizeof *symp
);
2790 /* Find out the length of the serialized version of a trie in bytes. */
2793 mmo_internal_3_length (bfd
*abfd
, struct mmo_symbol_trie
*trie
)
2795 /* First, one for the control byte. */
2796 unsigned int length
= 1;
2801 /* Add in the recursion to the left. */
2802 length
+= mmo_internal_3_length (abfd
, trie
->left
);
2804 /* Add in the middle trie and the character. */
2805 length
+= 1 + mmo_internal_3_length (abfd
, trie
->middle
);
2807 /* Add in the recursion to the right. */
2808 length
+= mmo_internal_3_length (abfd
, trie
->right
);
2810 /* Add in bytes for the symbol (if this is an endnode). */
2811 if (trie
->sym
.name
!= NULL
)
2813 unsigned int serno
= trie
->sym
.serno
;
2815 /* First what it takes to encode the value. */
2816 if (trie
->sym
.sym_type
== mmo_reg_sym
)
2818 else if (trie
->sym
.sym_type
== mmo_undef_sym
)
2822 bfd_vma value
= trie
->sym
.value
;
2824 /* Coded in one to eight following bytes. */
2825 if (trie
->sym
.sym_type
== mmo_data_sym
)
2826 value
-= (bfd_vma
) 0x20 << 56;
2836 /* Find out what it takes to encode the serial number. */
2848 /* Helper function for outputting the serial number of a symbol, output as
2849 a variant of leb128 (see dwarf2 documentation) which could be called
2850 beb128. Using a helper function and recursion simplifies debugging. */
2853 mmo_beb128_out (bfd
*abfd
, int serno
, int marker
)
2856 mmo_beb128_out (abfd
, serno
>> 7, 0);
2857 mmo_write_byte (abfd
, marker
| (serno
& 0x7f));
2860 /* Serialize a trie. */
2863 mmo_internal_3_dump (bfd
*abfd
, struct mmo_symbol_trie
*trie
)
2865 bfd_byte control
= 0;
2871 control
|= MMO3_LEFT
;
2874 control
|= MMO3_MIDDLE
;
2877 control
|= MMO3_RIGHT
;
2879 if (trie
->sym
.name
!= NULL
)
2881 /* Encode the symbol type and length of value bytes. */
2882 if (trie
->sym
.sym_type
== mmo_reg_sym
)
2883 control
|= MMO3_REGQUAL_BITS
;
2884 else if (trie
->sym
.sym_type
== mmo_undef_sym
)
2885 control
|= MMO3_UNDEF
;
2888 bfd_vma value
= trie
->sym
.value
;
2890 /* Coded in 1..8 following bytes. */
2891 if (trie
->sym
.sym_type
== mmo_data_sym
)
2893 control
|= MMO3_DATA
;
2894 value
-= (bfd_vma
) 0x20 << 56;
2906 /* The control byte is output before recursing. */
2907 mmo_write_byte (abfd
, control
);
2909 mmo_internal_3_dump (abfd
, trie
->left
);
2911 if (control
& MMO3_SYMBITS
)
2913 mmo_write_byte (abfd
, trie
->symchar
);
2915 if (trie
->sym
.name
!= NULL
)
2917 if (trie
->sym
.sym_type
== mmo_reg_sym
)
2918 mmo_write_byte (abfd
, trie
->sym
.value
);
2919 else if (trie
->sym
.sym_type
== mmo_undef_sym
)
2921 mmo_write_byte (abfd
, 0);
2922 mmo_write_byte (abfd
, 0);
2926 bfd_vma value
= trie
->sym
.value
;
2928 bfd_byte byte_n
= control
& 15;
2930 /* Coded in 1..8 following bytes. Note that the value is
2931 shifted out big-endian. */
2932 if (trie
->sym
.sym_type
== mmo_data_sym
)
2934 value
-= (bfd_vma
) 0x20 << 56;
2940 mmo_write_byte (abfd
, (value
>> ((byte_n
- 1) * 8)) & 0xff);
2943 while (byte_n
!= 0);
2946 mmo_beb128_out (abfd
, trie
->sym
.serno
, 128);
2948 mmo_internal_3_dump (abfd
, trie
->middle
);
2950 mmo_internal_3_dump (abfd
, trie
->right
);
2953 /* Write symbols in mmo format. Also write the lop_end terminator. */
2956 mmo_write_symbols_and_terminator (bfd
*abfd
)
2958 int count
= bfd_get_symcount (abfd
);
2960 asymbol
**orig_table
= bfd_get_outsymbols (abfd
);
2962 struct mmo_symbol_trie root
;
2967 /* Create a symbol for "Main". */
2968 asymbol
*fakemain
= bfd_make_empty_symbol (abfd
);
2970 fakemain
->flags
= BSF_GLOBAL
;
2971 fakemain
->value
= bfd_get_start_address (abfd
);
2972 fakemain
->name
= MMIX_START_SYMBOL_NAME
;
2973 fakemain
->section
= bfd_abs_section_ptr
;
2975 memset (&root
, 0, sizeof (root
));
2977 /* Make all symbols take a left turn. */
2978 root
.symchar
= 0xff;
2980 /* There must always be a ":Main", so we'll add one if there are no
2981 symbols. Make sure we have room for it. */
2982 table
= bfd_alloc (abfd
, (count
+ 1) * sizeof (asymbol
*));
2987 memcpy (table
, orig_table
, count
* sizeof (asymbol
*));
2989 /* Move :Main (if there is one) to the first position. This is
2990 necessary to get the same layout of the trie-tree when linking as
2991 when objcopying the result as in the objcopy.exp test "simple objcopy
2992 of executable". It also automatically takes care of assigning serial
2993 number 1 to :Main (as is mandatory). */
2994 for (i
= 0; i
< count
; i
++)
2995 if (table
[i
] != NULL
2996 && strcmp (table
[i
]->name
, MMIX_START_SYMBOL_NAME
) == 0
2997 && (table
[i
]->flags
& (BSF_DEBUGGING
|BSF_GLOBAL
)) == BSF_GLOBAL
)
2999 asymbol
*mainsym
= table
[i
];
3002 + mainsym
->section
->output_section
->vma
3003 + mainsym
->section
->output_offset
);
3004 memcpy (table
+ 1, orig_table
, i
* sizeof (asymbol
*));
3007 /* Check that the value assigned to :Main is the same as the entry
3008 address. The default linker script asserts this. This is as
3009 good a place as any to check this consistency. */
3010 if (mainvalue
!= bfd_get_start_address (abfd
)
3011 && !mmo_ignore_symbol_consistency (abfd
))
3013 /* Arbitrary buffer to hold the printable representation of a
3015 bfd_vma vma_start
= bfd_get_start_address (abfd
);
3018 /* xgettext:c-format */
3019 (_("%pB: bad symbol definition: `Main' set to %" PRIx64
" rather"
3020 " than the start address %" PRIx64
"\n"),
3021 abfd
, mainvalue
, vma_start
);
3022 bfd_set_error (bfd_error_bad_value
);
3027 if (i
== count
&& count
!= 0)
3029 /* When there are symbols, there must be a :Main. There was no
3030 :Main, so we need to add it manually. */
3031 memcpy (table
+ 1, orig_table
, count
* sizeof (asymbol
*));
3032 table
[0] = fakemain
;
3036 /* Don't bother inspecting symbols in plugin dummy objects; their
3037 symbols aren't fully inspectable. */
3038 if ((abfd
->flags
& BFD_PLUGIN
) == 0)
3040 for (i
= 0, serno
= 1; i
< count
&& table
[i
] != NULL
; i
++)
3042 asymbol
*s
= table
[i
];
3044 /* It's not enough to consult bfd_is_local_label, since it does not
3045 mean "local" in the sense of linkable-and-observable-after-link.
3046 Let's just check the BSF_GLOBAL flag.
3048 Also, don't export symbols with characters not in the
3050 if ((s
->flags
& (BSF_DEBUGGING
|BSF_GLOBAL
)) == BSF_GLOBAL
3052 valid_mmo_symbol_character_set
) == strlen (s
->name
))
3054 struct mmo_symbol sym
;
3055 memset (&sym
, 0, sizeof (sym
));
3057 /* Need to strip const here; strdup:ing would leak and the
3058 existing string must be safe to reuse. */
3059 sym
.name
= (char *) s
->name
;
3062 + s
->section
->output_section
->vma
3063 + s
->section
->output_offset
;
3065 if (bfd_is_und_section (s
->section
))
3066 sym
.sym_type
= mmo_undef_sym
;
3067 else if (strcmp (s
->section
->name
, MMO_DATA_SECTION_NAME
) == 0
3068 /* The encoding of data symbols require that the "rest"
3069 of the value fits in 6 bytes, so the upper two bytes
3070 must be 0x2000. All other symbols get to be the
3072 && (sym
.value
>> 48) == 0x2000)
3073 sym
.sym_type
= mmo_data_sym
;
3074 else if (strcmp (s
->section
->name
, MMIX_REG_SECTION_NAME
) == 0)
3075 sym
.sym_type
= mmo_reg_sym
;
3076 else if (strcmp (s
->section
->name
,
3077 MMIX_REG_CONTENTS_SECTION_NAME
) == 0)
3079 sym
.sym_type
= mmo_reg_sym
;
3083 sym
.sym_type
= mmo_abs_sym
;
3085 /* FIXME: We assume the order of the received symbols is an
3086 ordered mapping of the serial numbers. This is not
3087 necessarily true if we e.g. objcopy a mmo file to another and
3088 there are gaps in the numbering. Not sure if this can
3089 happen. Not sure what to do. */
3090 sym
.serno
= serno
++;
3092 if (! mmo_internal_add_3_sym (abfd
, &root
, &sym
))
3098 /* Change the root node to be a ":"-prefix. */
3100 root
.middle
= root
.left
;
3104 /* We have to find out if we can fit the whole symbol table in the mmo
3105 symtab. It would be bad to assume we can always fit it in 262144
3106 bytes. If we can't, just leave the Main symbol. */
3107 trie_len
= (mmo_internal_3_length (abfd
, &root
) + 3)/4;
3109 if (trie_len
> 0xffff)
3111 /* Test this code by using a lower limit in the test above and check
3112 that the single "Main" symbol is emitted and handled properly.
3113 There's no specific test-case. */
3114 struct mmo_symbol sym
;
3117 /* xgettext:c-format */
3118 (_("%pB: warning: symbol table too large for mmo, larger than 65535"
3119 " 32-bit words: %d. Only `Main' will be emitted.\n"),
3122 memset (&sym
, 0, sizeof (sym
));
3123 sym
.sym_type
= mmo_abs_sym
;
3124 sym
.name
= MMIX_START_SYMBOL_NAME
;
3126 sym
.value
= bfd_get_start_address (abfd
);
3128 /* Then patch up a symbol table to be just the ":Main" symbol. */
3129 memset (&root
, 0, sizeof (root
));
3130 root
.left
= root
.middle
;
3131 root
.symchar
= 0xff;
3135 if (! mmo_internal_add_3_sym (abfd
, &root
, &sym
))
3139 root
.middle
= root
.left
;
3143 trie_len
= (mmo_internal_3_length (abfd
, &root
) + 3)/4;
3146 /* Reset the written-bytes counter. */
3147 abfd
->tdata
.mmo_data
->byte_no
= 0;
3149 /* Put out the lop_stab mark. */
3150 bfd_put_32 (abfd
, (LOP
<< 24) | (LOP_STAB
<< 16), buf
);
3151 if (bfd_bwrite (buf
, 4, abfd
) != 4)
3154 /* Dump out symbols. */
3155 mmo_internal_3_dump (abfd
, &root
);
3157 if (trie_len
!= (abfd
->tdata
.mmo_data
->byte_no
+ 3)/4)
3159 /* I haven't seen this trig. It seems no use claiming this case
3160 isn't debugged and abort if we get here. Instead emit a
3161 diagnostic and fail "normally". */
3163 /* xgettext:c-format */
3164 (_("%pB: internal error, symbol table changed size from %d to %d"
3167 (abfd
->tdata
.mmo_data
->byte_no
+ 3)/4);
3168 bfd_set_error (bfd_error_bad_value
);
3172 /* Dump out remaining bytes in the buffer and handle I/O errors by
3173 propagating errors. */
3174 if ((abfd
->tdata
.mmo_data
->byte_no
% 4) != 0
3175 || abfd
->tdata
.mmo_data
->have_error
)
3177 memset (abfd
->tdata
.mmo_data
->buf
+ (abfd
->tdata
.mmo_data
->byte_no
% 4),
3178 0, 4 - (abfd
->tdata
.mmo_data
->byte_no
% 4));
3180 if (abfd
->tdata
.mmo_data
->have_error
3181 || bfd_bwrite (abfd
->tdata
.mmo_data
->buf
, 4, abfd
) != 4)
3185 bfd_put_32 (abfd
, (LOP
<< 24) | (LOP_END
<< 16) | trie_len
, buf
);
3186 return bfd_bwrite (buf
, 4, abfd
) == 4;
3189 /* Write section unless it is the register contents section. For that, we
3190 instead store the section in the supplied pointer. This function is
3191 used through bfd_map_over_sections. */
3194 mmo_write_section_unless_reg_contents (bfd
*abfd
, asection
*sec
, void *p
)
3196 struct mmo_write_sec_info
*infop
= (struct mmo_write_sec_info
*) p
;
3198 if (! infop
->retval
)
3201 if (strcmp (sec
->name
, MMIX_REG_CONTENTS_SECTION_NAME
) == 0)
3203 infop
->reg_section
= sec
;
3207 /* Exclude the convenience register section. */
3208 if (strcmp (sec
->name
, MMIX_REG_SECTION_NAME
) == 0)
3210 if (bfd_section_flags (sec
) & SEC_HAS_CONTENTS
)
3212 /* Make sure it hasn't got contents. It seems impossible to
3213 make it carry contents, so we don't have a test-case for
3216 /* xgettext:c-format */
3217 (_("%pB: internal error, internal register section %pA had"
3220 bfd_set_error (bfd_error_bad_value
);
3221 infop
->retval
= false;
3228 infop
->retval
= mmo_internal_write_section (abfd
, sec
);
3231 /* Do the actual output of a file. Assumes mmo_set_section_contents is
3235 mmo_write_object_contents (bfd
*abfd
)
3237 struct mmo_write_sec_info wsecinfo
;
3239 /* First, there are a few words of preamble. */
3240 if (! mmo_internal_write_header (abfd
))
3243 wsecinfo
.reg_section
= NULL
;
3244 wsecinfo
.retval
= true;
3246 bfd_map_over_sections (abfd
, mmo_write_section_unless_reg_contents
,
3249 if (! wsecinfo
.retval
)
3252 if (wsecinfo
.reg_section
!= NULL
)
3254 asection
*sec
= wsecinfo
.reg_section
;
3255 unsigned int z
= (unsigned int) (sec
->vma
/ 8);
3257 /* Registers 0..31 must not be global. Do sanity check on the "vma"
3258 of the register contents section and check that it corresponds to
3259 the length of the section. */
3260 if (z
< 32 || z
>= 255 || (sec
->vma
& 7) != 0
3261 || sec
->vma
!= 256 * 8 - sec
->size
- 8)
3263 bfd_set_error (bfd_error_bad_value
);
3266 /* There must always be at least one such register. */
3268 (_("%pB: no initialized registers; section length 0\n"),
3270 else if (sec
->vma
> (256 - 32) * 8)
3271 /* Provide better error message for the case of too many
3272 global registers. */
3274 /* xgettext:c-format */
3275 (_("%pB: too many initialized registers; section length %" PRId64
),
3276 abfd
, (int64_t) sec
->size
);
3279 /* xgettext:c-format */
3280 (_("%pB: invalid start address for initialized registers of"
3281 " length %" PRId64
": %#" PRIx64
),
3282 abfd
, (int64_t) sec
->size
, (uint64_t) sec
->vma
);
3287 if (! mmo_internal_write_post (abfd
, z
, sec
))
3291 if (! mmo_internal_write_post (abfd
, 255, NULL
))
3294 return mmo_write_symbols_and_terminator (abfd
);
3297 /* If there's anything in particular in a mmo bfd that we want to free,
3298 make this a real function. Only do this if you see major memory
3299 thrashing; zealous free:ing will cause unwanted behavior, especially if
3300 you "free" memory allocated with "bfd_alloc", or even "bfd_release" a
3301 block allocated with "bfd_alloc"; they're really allocated from an
3302 obstack, and we don't know what was allocated there since this
3303 particular allocation. */
3305 #define mmo_close_and_cleanup _bfd_generic_close_and_cleanup
3306 #define mmo_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
3308 /* Perhaps we need to adjust this one; mmo labels (originally) without a
3309 leading ':' might more appropriately be called local. */
3310 #define mmo_bfd_is_local_label_name bfd_generic_is_local_label_name
3311 #define mmo_bfd_is_target_special_symbol _bfd_bool_bfd_asymbol_false
3313 #define mmo_get_symbol_version_string \
3314 _bfd_nosymbols_get_symbol_version_string
3316 /* Is this one really used or defined by anyone? */
3317 #define mmo_get_lineno _bfd_nosymbols_get_lineno
3319 /* FIXME: We can do better on this one, if we have a dwarf2 .debug_line
3320 section or if MMO line numbers are implemented. */
3321 #define mmo_find_nearest_line _bfd_nosymbols_find_nearest_line
3322 #define mmo_find_line _bfd_nosymbols_find_line
3323 #define mmo_find_inliner_info _bfd_nosymbols_find_inliner_info
3324 #define mmo_make_empty_symbol _bfd_generic_make_empty_symbol
3325 #define mmo_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
3326 #define mmo_read_minisymbols _bfd_generic_read_minisymbols
3327 #define mmo_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
3329 #define mmo_get_section_contents_in_window \
3330 _bfd_generic_get_section_contents_in_window
3331 #define mmo_bfd_get_relocated_section_contents \
3332 bfd_generic_get_relocated_section_contents
3333 #define mmo_bfd_gc_sections bfd_generic_gc_sections
3334 #define mmo_bfd_lookup_section_flags bfd_generic_lookup_section_flags
3335 #define mmo_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
3336 #define mmo_bfd_link_add_symbols _bfd_generic_link_add_symbols
3337 #define mmo_bfd_link_just_syms _bfd_generic_link_just_syms
3338 #define mmo_bfd_copy_link_hash_symbol_type \
3339 _bfd_generic_copy_link_hash_symbol_type
3340 #define mmo_bfd_final_link _bfd_generic_final_link
3341 #define mmo_bfd_link_split_section _bfd_generic_link_split_section
3342 #define mmo_bfd_link_check_relocs _bfd_generic_link_check_relocs
3344 /* Strictly speaking, only MMIX uses this restricted format, but let's not
3345 stop anybody from shooting themselves in the foot. */
3346 #define mmo_set_arch_mach bfd_default_set_arch_mach
3347 #define mmo_bfd_relax_section bfd_generic_relax_section
3348 #define mmo_bfd_merge_sections bfd_generic_merge_sections
3349 #define mmo_bfd_is_group_section bfd_generic_is_group_section
3350 #define mmo_bfd_group_name bfd_generic_group_name
3351 #define mmo_bfd_discard_group bfd_generic_discard_group
3352 #define mmo_section_already_linked \
3353 _bfd_generic_section_already_linked
3354 #define mmo_bfd_define_common_symbol bfd_generic_define_common_symbol
3355 #define mmo_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
3356 #define mmo_bfd_define_start_stop bfd_generic_define_start_stop
3358 /* We want to copy time of creation, otherwise we'd use
3359 BFD_JUMP_TABLE_COPY (_bfd_generic). */
3360 #define mmo_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
3361 #define mmo_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
3362 #define mmo_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
3363 #define mmo_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
3364 #define mmo_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
3365 #define mmo_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
3367 const bfd_target mmix_mmo_vec
=
3370 bfd_target_mmo_flavour
,
3371 BFD_ENDIAN_BIG
, /* target byte order */
3372 BFD_ENDIAN_BIG
, /* target headers byte order */
3374 /* FIXME: Might need adjustments. */
3375 (HAS_RELOC
| EXEC_P
| /* object flags */
3376 HAS_LINENO
| HAS_DEBUG
|
3377 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
),
3379 /* FIXME: Might need adjustments. */
3380 (SEC_CODE
| SEC_DATA
| SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
3381 | SEC_READONLY
| SEC_EXCLUDE
| SEC_DEBUGGING
| SEC_IN_MEMORY
),
3383 0, /* leading underscore */
3384 ' ', /* ar_pad_char */
3385 16, /* ar_max_namelen */
3386 0, /* match priority. */
3387 TARGET_KEEP_UNUSED_SECTION_SYMBOLS
, /* keep unused section symbols. */
3388 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
3389 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
3390 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
3391 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
3392 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
3393 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
3397 mmo_object_p
, /* bfd_check_format */
3402 _bfd_bool_bfd_false_error
,
3404 _bfd_bool_bfd_false_error
,
3405 _bfd_bool_bfd_false_error
,
3407 { /* bfd_write_contents */
3408 _bfd_bool_bfd_false_error
,
3409 mmo_write_object_contents
,
3410 _bfd_bool_bfd_false_error
,
3411 _bfd_bool_bfd_false_error
,
3414 BFD_JUMP_TABLE_GENERIC (mmo
),
3415 BFD_JUMP_TABLE_COPY (mmo
),
3416 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
3417 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive
),
3418 BFD_JUMP_TABLE_SYMBOLS (mmo
),
3419 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs
),
3420 BFD_JUMP_TABLE_WRITE (mmo
),
3421 BFD_JUMP_TABLE_LINK (mmo
),
3422 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),