···11+// binary.cc -- binary input files for gold
22+33+// Copyright 2008 Free Software Foundation, Inc.
44+// Written by Ian Lance Taylor <iant@google.com>.
55+66+// This file is part of gold.
77+88+// This program is free software; you can redistribute it and/or modify
99+// it under the terms of the GNU General Public License as published by
1010+// the Free Software Foundation; either version 3 of the License, or
1111+// (at your option) any later version.
1212+1313+// This program is distributed in the hope that it will be useful,
1414+// but WITHOUT ANY WARRANTY; without even the implied warranty of
1515+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1616+// GNU General Public License for more details.
1717+1818+// You should have received a copy of the GNU General Public License
1919+// along with this program; if not, write to the Free Software
2020+// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
2121+// MA 02110-1301, USA.
2222+2323+#include "gold.h"
2424+2525+#include <cerrno>
2626+#include <cstring>
2727+#include "safe-ctype.h"
2828+2929+#include "elfcpp.h"
3030+#include "stringpool.h"
3131+#include "fileread.h"
3232+#include "output.h"
3333+#include "binary.h"
3434+3535+// Support for reading binary files as input. These become blobs in
3636+// the final output. These files are treated as though they have a
3737+// single .data section and define three symbols:
3838+// _binary_FILENAME_start, _binary_FILENAME_end, _binary_FILENAME_end.
3939+// The FILENAME is the name of the input file, with any
4040+// non-alphanumeric character changed to an underscore.
4141+4242+// We implement this by creating an ELF file in memory.
4343+4444+namespace gold
4545+{
4646+4747+// class Binary_to_elf.
4848+4949+Binary_to_elf::Binary_to_elf(elfcpp::EM machine, int size, bool big_endian,
5050+ const std::string& filename)
5151+ : elf_machine_(machine), size_(size), big_endian_(big_endian),
5252+ filename_(filename), data_(NULL), filesize_(0)
5353+{
5454+}
5555+5656+Binary_to_elf::~Binary_to_elf()
5757+{
5858+ if (this->data_ != NULL)
5959+ delete[] this->data_;
6060+}
6161+6262+// Given FILENAME, create a buffer which looks like an ELF file with
6363+// the contents of FILENAME as the contents of the only section. The
6464+// TASK parameters is mainly for debugging, and records who holds
6565+// locks.
6666+6767+bool
6868+Binary_to_elf::convert(const Task* task)
6969+{
7070+ if (this->size_ == 32)
7171+ {
7272+ if (!this->big_endian_)
7373+ {
7474+#ifdef HAVE_TARGET_32_LITTLE
7575+ return this->sized_convert<32, false>(task);
7676+#else
7777+ gold_unreachable();
7878+#endif
7979+ }
8080+ else
8181+ {
8282+#ifdef HAVE_TARGET_32_BIG
8383+ return this->sized_convert<32, true>(task);
8484+#else
8585+ gold_unreachable();
8686+#endif
8787+ }
8888+ }
8989+ else if (this->size_ == 64)
9090+ {
9191+ if (!this->big_endian_)
9292+ {
9393+#ifdef HAVE_TARGET_64_LITTLE
9494+ return this->sized_convert<64, false>(task);
9595+#else
9696+ gold_unreachable();
9797+#endif
9898+ }
9999+ else
100100+ {
101101+#ifdef HAVE_TARGET_64_BIG
102102+ return this->sized_convert<64, true>(task);
103103+#else
104104+ gold_unreachable();
105105+#endif
106106+ }
107107+ }
108108+ else
109109+ gold_unreachable();
110110+}
111111+112112+// We are going to create:
113113+// * The ELF file header.
114114+// * Five sections: null section, .data, .symtab, .strtab, .shstrtab
115115+// * The contents of the file.
116116+// * Four symbols: null, begin, end, size.
117117+// * Three symbol names.
118118+// * Four section names.
119119+120120+template<int size, bool big_endian>
121121+bool
122122+Binary_to_elf::sized_convert(const Task* task)
123123+{
124124+ // Read the input file.
125125+126126+ File_read f;
127127+ if (!f.open(task, this->filename_))
128128+ {
129129+ gold_error(_("cannot open %s: %s:"), this->filename_.c_str(),
130130+ strerror(errno));
131131+ return false;
132132+ }
133133+134134+ section_size_type filesize = convert_to_section_size_type(f.filesize());
135135+ const unsigned char* fileview = f.get_view(0, filesize, false);
136136+137137+ unsigned int align;
138138+ if (size == 32)
139139+ align = 4;
140140+ else if (size == 64)
141141+ align = 8;
142142+ else
143143+ gold_unreachable();
144144+ section_size_type aligned_filesize = align_address(filesize, align);
145145+146146+ // Build the stringpool for the symbol table.
147147+148148+ std::string mangled_name = this->filename_;
149149+ for (std::string::iterator p = mangled_name.begin();
150150+ p != mangled_name.end();
151151+ ++p)
152152+ if (!ISALNUM(*p))
153153+ *p = '_';
154154+ mangled_name = "_binary_" + mangled_name;
155155+ std::string start_symbol_name = mangled_name + "_start";
156156+ std::string end_symbol_name = mangled_name + "_end";
157157+ std::string size_symbol_name = mangled_name + "_size";
158158+159159+ Stringpool strtab;
160160+ strtab.add(start_symbol_name.c_str(), false, NULL);
161161+ strtab.add(end_symbol_name.c_str(), false, NULL);
162162+ strtab.add(size_symbol_name.c_str(), false, NULL);
163163+ strtab.set_string_offsets();
164164+165165+ // Build the stringpool for the section name table.
166166+167167+ Stringpool shstrtab;
168168+ shstrtab.add(".data", false, NULL);
169169+ shstrtab.add(".symtab", false, NULL);
170170+ shstrtab.add(".strtab", false, NULL);
171171+ shstrtab.add(".shstrtab", false, NULL);
172172+ shstrtab.set_string_offsets();
173173+174174+ // Work out the size of the generated file, and the offsets of the
175175+ // various sections, and allocate a buffer.
176176+177177+ const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
178178+179179+ size_t output_size = (elfcpp::Elf_sizes<size>::ehdr_size
180180+ + 5 * elfcpp::Elf_sizes<size>::shdr_size);
181181+ size_t data_offset = output_size;
182182+ output_size += aligned_filesize;
183183+ size_t symtab_offset = output_size;
184184+ output_size += 4 * sym_size;
185185+ size_t strtab_offset = output_size;
186186+ output_size += strtab.get_strtab_size();
187187+ size_t shstrtab_offset = output_size;
188188+ output_size += shstrtab.get_strtab_size();
189189+190190+ unsigned char* buffer = new unsigned char[output_size];
191191+192192+ // Write out the data.
193193+194194+ unsigned char* pout = buffer;
195195+196196+ this->write_file_header<size, big_endian>(&pout);
197197+198198+ this->write_section_header<size, big_endian>("", &shstrtab, elfcpp::SHT_NULL,
199199+ 0, 0, 0, 0, 0,
200200+ 0, 0, &pout);
201201+ // Having the section be named ".data" and having it be writable is
202202+ // because th GNU linker does it that way, and existing linker
203203+ // script expect it.
204204+ this->write_section_header<size, big_endian>(".data", &shstrtab,
205205+ elfcpp::SHT_PROGBITS,
206206+ (elfcpp::SHF_ALLOC
207207+ | elfcpp::SHF_WRITE),
208208+ data_offset,
209209+ filesize, 0, 0,
210210+ align, 0, &pout);
211211+ this->write_section_header<size, big_endian>(".symtab", &shstrtab,
212212+ elfcpp::SHT_SYMTAB,
213213+ 0, symtab_offset, 4 * sym_size,
214214+ 3, 1, align, sym_size, &pout);
215215+ this->write_section_header<size, big_endian>(".strtab", &shstrtab,
216216+ elfcpp::SHT_STRTAB,
217217+ 0, strtab_offset,
218218+ strtab.get_strtab_size(),
219219+ 0, 0, 1, 0, &pout);
220220+ this->write_section_header<size, big_endian>(".shstrtab", &shstrtab,
221221+ elfcpp::SHT_STRTAB,
222222+ 0, shstrtab_offset,
223223+ shstrtab.get_strtab_size(),
224224+ 0, 0, 1, 0, &pout);
225225+226226+ memcpy(pout, fileview, filesize);
227227+ pout += filesize;
228228+ memset(pout, 0, aligned_filesize - filesize);
229229+ pout += aligned_filesize - filesize;
230230+231231+ this->write_symbol<size, big_endian>("", &strtab, 0, 0, &pout);
232232+ this->write_symbol<size, big_endian>(start_symbol_name, &strtab, 0, 1,
233233+ &pout);
234234+ this->write_symbol<size, big_endian>(end_symbol_name, &strtab, filesize, 1,
235235+ &pout);
236236+ this->write_symbol<size, big_endian>(size_symbol_name, &strtab, filesize,
237237+ elfcpp::SHN_ABS, &pout);
238238+239239+ strtab.write_to_buffer(pout, strtab.get_strtab_size());
240240+ pout += strtab.get_strtab_size();
241241+242242+ shstrtab.write_to_buffer(pout, shstrtab.get_strtab_size());
243243+ pout += shstrtab.get_strtab_size();
244244+245245+ gold_assert(static_cast<size_t>(pout - buffer) == output_size);
246246+247247+ this->data_ = buffer;
248248+ this->filesize_ = output_size;
249249+250250+ f.unlock(task);
251251+252252+ return true;
253253+}
254254+255255+// Write out the file header.
256256+257257+template<int size, bool big_endian>
258258+void
259259+Binary_to_elf::write_file_header(unsigned char** ppout)
260260+{
261261+ elfcpp::Ehdr_write<size, big_endian> oehdr(*ppout);
262262+263263+ unsigned char e_ident[elfcpp::EI_NIDENT];
264264+ memset(e_ident, 0, elfcpp::EI_NIDENT);
265265+ e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
266266+ e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
267267+ e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
268268+ e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
269269+ if (size == 32)
270270+ e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
271271+ else if (size == 64)
272272+ e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
273273+ else
274274+ gold_unreachable();
275275+ e_ident[elfcpp::EI_DATA] = (big_endian
276276+ ? elfcpp::ELFDATA2MSB
277277+ : elfcpp::ELFDATA2LSB);
278278+ e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
279279+ oehdr.put_e_ident(e_ident);
280280+281281+ oehdr.put_e_type(elfcpp::ET_REL);
282282+ oehdr.put_e_machine(this->elf_machine_);
283283+ oehdr.put_e_version(elfcpp::EV_CURRENT);
284284+ oehdr.put_e_entry(0);
285285+ oehdr.put_e_phoff(0);
286286+ oehdr.put_e_shoff(elfcpp::Elf_sizes<size>::ehdr_size);
287287+ oehdr.put_e_flags(0);
288288+ oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
289289+ oehdr.put_e_phentsize(0);
290290+ oehdr.put_e_phnum(0);
291291+ oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
292292+ oehdr.put_e_shnum(5);
293293+ oehdr.put_e_shstrndx(4);
294294+295295+ *ppout += elfcpp::Elf_sizes<size>::ehdr_size;
296296+}
297297+298298+// Write out a section header.
299299+300300+template<int size, bool big_endian>
301301+void
302302+Binary_to_elf::write_section_header(
303303+ const char* name,
304304+ const Stringpool* shstrtab,
305305+ elfcpp::SHT type,
306306+ unsigned int flags,
307307+ section_size_type offset,
308308+ section_size_type section_size,
309309+ unsigned int link,
310310+ unsigned int info,
311311+ unsigned int addralign,
312312+ unsigned int entsize,
313313+ unsigned char** ppout)
314314+{
315315+ elfcpp::Shdr_write<size, big_endian> oshdr(*ppout);
316316+317317+ oshdr.put_sh_name(*name == '\0' ? 0 : shstrtab->get_offset(name));
318318+ oshdr.put_sh_type(type);
319319+ oshdr.put_sh_flags(flags);
320320+ oshdr.put_sh_addr(0);
321321+ oshdr.put_sh_offset(offset);
322322+ oshdr.put_sh_size(section_size);
323323+ oshdr.put_sh_link(link);
324324+ oshdr.put_sh_info(info);
325325+ oshdr.put_sh_addralign(addralign);
326326+ oshdr.put_sh_entsize(entsize);
327327+328328+ *ppout += elfcpp::Elf_sizes<size>::shdr_size;
329329+}
330330+331331+// Write out a symbol.
332332+333333+template<int size, bool big_endian>
334334+void
335335+Binary_to_elf::write_symbol(
336336+ const std::string& name,
337337+ const Stringpool* strtab,
338338+ section_size_type value,
339339+ unsigned int shndx,
340340+ unsigned char** ppout)
341341+{
342342+ unsigned char* pout = *ppout;
343343+344344+ elfcpp::Sym_write<size, big_endian> osym(pout);
345345+ osym.put_st_name(name.empty() ? 0 : strtab->get_offset(name.c_str()));
346346+ osym.put_st_value(value);
347347+ osym.put_st_size(0);
348348+ osym.put_st_info(name.empty() ? elfcpp::STB_LOCAL : elfcpp::STB_GLOBAL,
349349+ elfcpp::STT_NOTYPE);
350350+ osym.put_st_other(elfcpp::STV_DEFAULT, 0);
351351+ osym.put_st_shndx(shndx);
352352+353353+ *ppout += elfcpp::Elf_sizes<size>::sym_size;
354354+}
355355+356356+} // End namespace gold.
+116
gold/binary.h
···11+// binary.h -- binary input files for gold -*- C++ -*-
22+33+// Copyright 2008 Free Software Foundation, Inc.
44+// Written by Ian Lance Taylor <iant@google.com>.
55+66+// This file is part of gold.
77+88+// This program is free software; you can redistribute it and/or modify
99+// it under the terms of the GNU General Public License as published by
1010+// the Free Software Foundation; either version 3 of the License, or
1111+// (at your option) any later version.
1212+1313+// This program is distributed in the hope that it will be useful,
1414+// but WITHOUT ANY WARRANTY; without even the implied warranty of
1515+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1616+// GNU General Public License for more details.
1717+1818+// You should have received a copy of the GNU General Public License
1919+// along with this program; if not, write to the Free Software
2020+// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
2121+// MA 02110-1301, USA.
2222+2323+// Support binary input files by making them look like an ELF file.
2424+2525+#ifndef GOLD_BINARY_H
2626+#define GOLD_BINARY_H
2727+2828+#include <string>
2929+3030+#include "elfcpp.h"
3131+3232+namespace gold
3333+{
3434+3535+class Task;
3636+3737+template<typename Stringpool_char>
3838+class Stringpool_template;
3939+4040+// This class takes a file name and creates a buffer which looks like
4141+// an ELF file read into memory.
4242+4343+class Binary_to_elf
4444+{
4545+ public:
4646+ Binary_to_elf(elfcpp::EM machine, int size, bool big_endian,
4747+ const std::string& filename);
4848+4949+ ~Binary_to_elf();
5050+5151+ // Read contents and create an ELF buffer. Return true if this
5252+ // succeeds, false otherwise.
5353+ bool
5454+ convert(const Task*);
5555+5656+ // Return a pointer to the contents of the ELF file.
5757+ const unsigned char*
5858+ converted_data() const
5959+ { return this->data_; }
6060+6161+ // Return a pointer to the contents of the ELF file and let the
6262+ // caller take charge of it. It was allocated using new[].
6363+ unsigned char*
6464+ converted_data_leak()
6565+ {
6666+ unsigned char* ret = this->data_;
6767+ this->data_ = NULL;
6868+ return ret;
6969+ }
7070+7171+ // Return the size of the ELF file.
7272+ size_t
7373+ converted_size() const
7474+ { return this->filesize_; }
7575+7676+ private:
7777+ Binary_to_elf(const Binary_to_elf&);
7878+ Binary_to_elf& operator=(const Binary_to_elf&);
7979+8080+ template<int size, bool big_endian>
8181+ bool
8282+ sized_convert(const Task*);
8383+8484+ template<int size, bool big_endian>
8585+ void
8686+ write_file_header(unsigned char**);
8787+8888+ template<int size, bool big_endian>
8989+ void
9090+ write_section_header(const char*, const Stringpool_template<char>*,
9191+ elfcpp::SHT, unsigned int, section_size_type,
9292+ section_size_type, unsigned int, unsigned int,
9393+ unsigned int, unsigned int, unsigned char**);
9494+9595+ template<int size, bool big_endian>
9696+ void
9797+ write_symbol(const std::string&, const Stringpool_template<char>*,
9898+ section_size_type, unsigned int, unsigned char**);
9999+100100+ // The ELF machine code of the file to create.
101101+ elfcpp::EM elf_machine_;
102102+ // The size of the file to create, 32 or 64.
103103+ int size_;
104104+ // Whether to create a big endian file.
105105+ bool big_endian_;
106106+ // The name of the file to read.
107107+ std::string filename_;
108108+ // The ELF file data, allocated by new [].
109109+ unsigned char* data_;
110110+ // The ELF file size.
111111+ section_size_type filesize_;
112112+};
113113+114114+} // End namespace gold.
115115+116116+#endif // !defined(GOLD_BINARY_H)
+49-2
gold/fileread.cc
···3030#include <sys/uio.h>
3131#include "filenames.h"
32323333+#include "parameters.h"
3334#include "options.h"
3435#include "dirsearch.h"
3636+#include "target.h"
3737+#include "binary.h"
3538#include "fileread.h"
36393740namespace gold
···122125 return this->descriptor_ >= 0;
123126}
124127125125-// Open the file for testing purposes.
128128+// Open the file with the contents in memory.
126129127130bool
128131File_read::open(const Task* task, const std::string& name,
···686689 }
687690688691 // Now that we've figured out where the file lives, try to open it.
689689- if (!this->file_.open(task, name))
692692+693693+ General_options::Object_format format =
694694+ this->input_argument_->options().input_format();
695695+ bool ok;
696696+ if (format == General_options::OBJECT_FORMAT_ELF)
697697+ ok = this->file_.open(task, name);
698698+ else
699699+ {
700700+ gold_assert(format == General_options::OBJECT_FORMAT_BINARY);
701701+ ok = this->open_binary(task, name);
702702+ }
703703+704704+ if (!ok)
690705 {
691706 gold_error(_("cannot open %s: %s"),
692707 name.c_str(), strerror(errno));
···694709 }
695710696711 return true;
712712+}
713713+714714+// Open a file for --format binary.
715715+716716+bool
717717+Input_file::open_binary(const Task* task, const std::string& name)
718718+{
719719+ // In order to open a binary file, we need machine code, size, and
720720+ // endianness. If we have a target already, use it, otherwise use
721721+ // the defaults.
722722+ elfcpp::EM machine;
723723+ int size;
724724+ bool big_endian;
725725+ if (parameters->is_target_valid())
726726+ {
727727+ Target* target = parameters->target();
728728+ machine = target->machine_code();
729729+ size = target->get_size();
730730+ big_endian = target->is_big_endian();
731731+ }
732732+ else
733733+ {
734734+ machine = elfcpp::GOLD_DEFAULT_MACHINE;
735735+ size = GOLD_DEFAULT_SIZE;
736736+ big_endian = GOLD_DEFAULT_BIG_ENDIAN;
737737+ }
738738+739739+ Binary_to_elf binary_to_elf(machine, size, big_endian, name);
740740+ if (!binary_to_elf.convert(task))
741741+ return false;
742742+ return this->file_.open(task, name, binary_to_elf.converted_data_leak(),
743743+ binary_to_elf.converted_size());
697744}
698745699746} // End namespace gold.
+4
gold/fileread.h
···426426 Input_file(const Input_file&);
427427 Input_file& operator=(const Input_file&);
428428429429+ // Open a binary file.
430430+ bool
431431+ open_binary(const Task* task, const std::string& name);
432432+429433 // The argument from the command line.
430434 const Input_file_argument* input_argument_;
431435 // The name under which we opened the file. This is like the name
+1-1
gold/gold.cc
···196196 gold_error(_("cannot mix -r with dynamic object %s"),
197197 (*input_objects->dynobj_begin())->name().c_str());
198198 if (!doing_static_link
199199- && options.output_format() != General_options::OUTPUT_FORMAT_ELF)
199199+ && options.output_format() != General_options::OBJECT_FORMAT_ELF)
200200 gold_fatal(_("cannot use non-ELF output format with dynamic object %s"),
201201 (*input_objects->dynobj_begin())->name().c_str());
202202
+4-4
gold/layout.cc
···5757 // Now we know the final size of the output file and we know where
5858 // each piece of information goes.
5959 Output_file* of = new Output_file(parameters->output_file_name());
6060- if (this->options_.output_format() != General_options::OUTPUT_FORMAT_ELF)
6060+ if (this->options_.output_format() != General_options::OBJECT_FORMAT_ELF)
6161 of->set_is_temporary();
6262 of->open(file_size);
6363···951951 else
952952 load_seg = this->find_first_load_seg();
953953954954- if (this->options_.output_format() != General_options::OUTPUT_FORMAT_ELF)
954954+ if (this->options_.output_format() != General_options::OBJECT_FORMAT_ELF)
955955 load_seg = NULL;
956956957957 gold_assert(phdr_seg == NULL || load_seg != NULL);
···25022502Layout::write_binary(Output_file* in) const
25032503{
25042504 gold_assert(this->options_.output_format()
25052505- == General_options::OUTPUT_FORMAT_BINARY);
25052505+ == General_options::OBJECT_FORMAT_BINARY);
2506250625072507 // Get the size of the binary file.
25082508 uint64_t max_load_address = 0;
···26722672Close_task_runner::run(Workqueue*, const Task*)
26732673{
26742674 // If we've been asked to create a binary file, we do so here.
26752675- if (this->options_->output_format() != General_options::OUTPUT_FORMAT_ELF)
26752675+ if (this->options_->output_format() != General_options::OBJECT_FORMAT_ELF)
26762676 this->layout_->write_binary(this->of_);
2677267726782678 this->of_->close();
+40-15
gold/options.cc
···135135namespace
136136{
137137138138+// Recognize input and output target names. The GNU linker accepts
139139+// these with --format and --oformat. This code is intended to be
140140+// minimally compatible. In practice for an ELF target this would be
141141+// the same target as the input files; that name always start with
142142+// "elf". Non-ELF targets would be "srec", "symbolsrec", "tekhex",
143143+// "binary", "ihex".
144144+145145+gold::General_options::Object_format
146146+string_to_object_format(const char* arg)
147147+{
148148+ if (strncmp(arg, "elf", 3) == 0)
149149+ return gold::General_options::OBJECT_FORMAT_ELF;
150150+ else if (strcmp(arg, "binary") == 0)
151151+ return gold::General_options::OBJECT_FORMAT_BINARY;
152152+ else
153153+ {
154154+ gold::gold_error(_("format '%s' not supported "
155155+ "(supported formats: elf, binary)"),
156156+ arg);
157157+ return gold::General_options::OBJECT_FORMAT_ELF;
158158+ }
159159+}
160160+138161// Handle the special -l option, which adds an input file.
139162140163int
···428451 &Position_dependent_options::set_static_search),
429452 GENERAL_NOARG('\0', "Bsymbolic", N_("Bind defined symbols locally"),
430453 NULL, ONE_DASH, &General_options::set_symbolic),
454454+ POSDEP_ARG('b', "format", N_("Set input format (elf, binary)"),
455455+ N_("-b FORMAT, --format FORMAT"), TWO_DASHES,
456456+ &Position_dependent_options::set_input_format),
431457#ifdef HAVE_ZLIB_H
432458# define ZLIB_STR ",zlib"
433459#else
···608634 search_path_(),
609635 optimization_level_(0),
610636 output_file_name_("a.out"),
611611- output_format_(OUTPUT_FORMAT_ELF),
637637+ output_format_(OBJECT_FORMAT_ELF),
612638 is_relocatable_(false),
613639 strip_(STRIP_NONE),
614640 allow_shlib_undefined_(false),
···647673 this->script_options_->define_symbol(arg);
648674}
649675650650-// Handle the --oformat option. The GNU linker accepts a target name
651651-// with --oformat. In practice for an ELF target this would be the
652652-// same target as the input files. That name always start with "elf".
653653-// Non-ELF targets would be "srec", "symbolsrec", "tekhex", "binary",
654654-// "ihex".
676676+// Handle the --oformat option.
655677656678void
657679General_options::set_output_format(const char* arg)
658680{
659659- if (strncmp(arg, "elf", 3) == 0)
660660- this->output_format_ = OUTPUT_FORMAT_ELF;
661661- else if (strcmp(arg, "binary") == 0)
662662- this->output_format_ = OUTPUT_FORMAT_BINARY;
663663- else
664664- gold_error(_("format '%s' not supported (supported formats: elf, binary)"),
665665- arg);
681681+ this->output_format_ = string_to_object_format(arg);
666682}
667683668684// Handle the -z option.
···738754Position_dependent_options::Position_dependent_options()
739755 : do_static_search_(false),
740756 as_needed_(false),
741741- include_whole_archive_(false)
757757+ include_whole_archive_(false),
758758+ input_format_(General_options::OBJECT_FORMAT_ELF)
742759{
760760+}
761761+762762+// Set the input format.
763763+764764+void
765765+Position_dependent_options::set_input_format(const char* arg)
766766+{
767767+ this->input_format_ = string_to_object_format(arg);
743768}
744769745770// Search_directory methods.
···10451070 if (this->options_.is_shared() && this->options_.is_relocatable())
10461071 gold_fatal(_("-shared and -r are incompatible"));
1047107210481048- if (this->options_.output_format() != General_options::OUTPUT_FORMAT_ELF
10731073+ if (this->options_.output_format() != General_options::OBJECT_FORMAT_ELF
10491074 && (this->options_.is_shared() || this->options_.is_relocatable()))
10501075 gold_fatal(_("binary output format not compatible with -shared or -r"));
10511076
···11+// binary_test.cc -- test --format binary for gold
22+33+// Copyright 2008 Free Software Foundation, Inc.
44+// Written by Ian Lance Taylor <iant@google.com>.
55+66+// This file is part of gold.
77+88+// This program is free software; you can redistribute it and/or modify
99+// it under the terms of the GNU General Public License as published by
1010+// the Free Software Foundation; either version 3 of the License, or
1111+// (at your option) any later version.
1212+1313+// This program is distributed in the hope that it will be useful,
1414+// but WITHOUT ANY WARRANTY; without even the implied warranty of
1515+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1616+// GNU General Public License for more details.
1717+1818+// You should have received a copy of the GNU General Public License
1919+// along with this program; if not, write to the Free Software
2020+// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
2121+// MA 02110-1301, USA.
2222+2323+// This program is linked with a small text file named binary.txt
2424+// using --formatbinary.
2525+2626+#include <cassert>
2727+#include <cstddef>
2828+#include <cstring>
2929+#include <stdint.h>
3030+3131+extern char _binary_binary_txt_start[];
3232+extern char _binary_binary_txt_end[];
3333+extern char _binary_binary_txt_size[];
3434+3535+int
3636+main(int, char**)
3737+{
3838+ int size = reinterpret_cast<uintptr_t>(_binary_binary_txt_size);
3939+ assert(size == _binary_binary_txt_end - _binary_binary_txt_start);
4040+4141+ const char* const txt = "This file is used for the binary test.\n";
4242+ assert(strncmp(txt, _binary_binary_txt_start, size) == 0);
4343+ assert(static_cast<size_t>(size) == strlen(txt));
4444+4545+ return 0;
4646+}
+147
gold/testsuite/binary_unittest.cc
···11+// binary_unittest.cc -- test Binary_to_elf
22+33+// Copyright 2008 Free Software Foundation, Inc.
44+// Written by Ian Lance Taylor <iant@google.com>.
55+66+// This file is part of gold.
77+88+// This program is free software; you can redistribute it and/or modify
99+// it under the terms of the GNU General Public License as published by
1010+// the Free Software Foundation; either version 3 of the License, or
1111+// (at your option) any later version.
1212+1313+// This program is distributed in the hope that it will be useful,
1414+// but WITHOUT ANY WARRANTY; without even the implied warranty of
1515+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1616+// GNU General Public License for more details.
1717+1818+// You should have received a copy of the GNU General Public License
1919+// along with this program; if not, write to the Free Software
2020+// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
2121+// MA 02110-1301, USA.
2222+2323+#include "gold.h"
2424+2525+#include <unistd.h>
2626+#include <sys/types.h>
2727+#include <sys/stat.h>
2828+#include <fcntl.h>
2929+3030+#include "elfcpp.h"
3131+#include "parameters.h"
3232+#include "errors.h"
3333+#include "options.h"
3434+#include "binary.h"
3535+#include "object.h"
3636+3737+#include "test.h"
3838+#include "testfile.h"
3939+4040+namespace gold_testsuite
4141+{
4242+4343+using namespace gold;
4444+4545+template<int size, bool big_endian>
4646+bool
4747+Sized_binary_test(Target* target)
4848+{
4949+ // We need a pretend Task.
5050+ const Task* task = reinterpret_cast<const Task*>(-1);
5151+5252+ // Use the executable itself as the binary data.
5353+ struct stat st;
5454+ CHECK(::stat(gold::program_name, &st) == 0);
5555+ int o = ::open(gold::program_name, O_RDONLY);
5656+ CHECK(o >= 0);
5757+ unsigned char* filedata = new unsigned char[st.st_size];
5858+ CHECK(::read(o, filedata, st.st_size) == st.st_size);
5959+ CHECK(::close(o) == 0);
6060+6161+ Binary_to_elf binary(static_cast<elfcpp::EM>(0xffff), size, big_endian,
6262+ gold::program_name);
6363+6464+ CHECK(binary.convert(task));
6565+6666+ Input_file input_file(task, "test.o", binary.converted_data(),
6767+ binary.converted_size());
6868+ Object* object = make_elf_object("test.o", &input_file, 0,
6969+ binary.converted_data(),
7070+ binary.converted_size());
7171+ CHECK(object != NULL);
7272+ if (object == NULL)
7373+ return false;
7474+7575+ CHECK(!object->is_dynamic());
7676+ CHECK(object->target() == target);
7777+ CHECK(object->shnum() == 5);
7878+ CHECK(object->section_name(1) == ".data");
7979+ CHECK(object->section_flags(1) == elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE);
8080+ section_size_type len;
8181+ const unsigned char* contents = object->section_contents(1, &len, false);
8282+ CHECK(len == st.st_size);
8383+ CHECK(memcmp(filedata, contents, len) == 0);
8484+8585+ // Force the symbols to be read internally, so that
8686+ // symbol_section_and_value will work.
8787+ Read_symbols_data sd;
8888+ object->read_symbols(&sd);
8989+ delete sd.section_headers;
9090+ delete sd.section_names;
9191+ delete sd.symbols;
9292+ delete sd.symbol_names;
9393+9494+ Sized_relobj<size, big_endian>* relobj =
9595+ static_cast<Sized_relobj<size, big_endian>*>(object);
9696+ typename Sized_relobj<size, big_endian>::Address value;
9797+ CHECK(relobj->symbol_section_and_value(0, &value) == 0);
9898+ CHECK(value == 0);
9999+ CHECK(relobj->symbol_section_and_value(1, &value) == 1);
100100+ CHECK(value == 0);
101101+ CHECK(relobj->symbol_section_and_value(2, &value) == 1);
102102+ CHECK(static_cast<off_t>(value) == st.st_size);
103103+ CHECK(relobj->symbol_section_and_value(3, &value) == elfcpp::SHN_ABS);
104104+ CHECK(static_cast<off_t>(value) == st.st_size);
105105+106106+ object->unlock(task);
107107+ return true;
108108+}
109109+110110+bool
111111+Binary_test(Test_report*)
112112+{
113113+ Errors errors(gold::program_name);
114114+ initialize_parameters(&errors);
115115+116116+ Script_options script_options;
117117+ General_options options(&script_options);
118118+ set_parameters_from_options(&options);
119119+120120+ int fail = 0;
121121+122122+#ifdef HAVE_TARGET_32_LITTLE
123123+ if (!Sized_binary_test<32, false>(target_test_pointer_32_little))
124124+ ++fail;
125125+#endif
126126+127127+#ifdef HAVE_TARGET_32_BIG
128128+ if (!Sized_binary_test<32, true>(target_test_pointer_32_big))
129129+ ++fail;
130130+#endif
131131+132132+#ifdef HAVE_TARGET_64_LITTLE
133133+ if (!Sized_binary_test<64, false>(target_test_pointer_64_little))
134134+ ++fail;
135135+#endif
136136+137137+#ifdef HAVE_TARGET_64_BIG
138138+ if (!Sized_binary_test<64, true>(target_test_pointer_64_big))
139139+ ++fail;
140140+#endif
141141+142142+ return fail == 0;
143143+}
144144+145145+Register_test binary_register("Binary", Binary_test);
146146+147147+} // End namespace gold_testsuite.