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