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