Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // -*- mode: C++ -*- |
michael@0 | 2 | |
michael@0 | 3 | // Copyright (c) 2011, Google Inc. |
michael@0 | 4 | // All rights reserved. |
michael@0 | 5 | // |
michael@0 | 6 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 7 | // modification, are permitted provided that the following conditions are |
michael@0 | 8 | // met: |
michael@0 | 9 | // |
michael@0 | 10 | // * Redistributions of source code must retain the above copyright |
michael@0 | 11 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 12 | // * Redistributions in binary form must reproduce the above |
michael@0 | 13 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 14 | // in the documentation and/or other materials provided with the |
michael@0 | 15 | // distribution. |
michael@0 | 16 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 17 | // contributors may be used to endorse or promote products derived from |
michael@0 | 18 | // this software without specific prior written permission. |
michael@0 | 19 | // |
michael@0 | 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 31 | |
michael@0 | 32 | // Original author: Ted Mielczarek <ted.mielczarek@gmail.com> |
michael@0 | 33 | |
michael@0 | 34 | // synth_elf.h: Interface to synth_elf::ELF: fake ELF generator. |
michael@0 | 35 | |
michael@0 | 36 | #ifndef COMMON_LINUX_SYNTH_ELF_H_ |
michael@0 | 37 | #define COMMON_LINUX_SYNTH_ELF_H_ |
michael@0 | 38 | |
michael@0 | 39 | #include "common/test_assembler.h" |
michael@0 | 40 | |
michael@0 | 41 | #include <list> |
michael@0 | 42 | #include <vector> |
michael@0 | 43 | #include <map> |
michael@0 | 44 | #include <string> |
michael@0 | 45 | #include <utility> |
michael@0 | 46 | |
michael@0 | 47 | #include "common/using_std_string.h" |
michael@0 | 48 | |
michael@0 | 49 | namespace google_breakpad { |
michael@0 | 50 | namespace synth_elf { |
michael@0 | 51 | |
michael@0 | 52 | using std::list; |
michael@0 | 53 | using std::vector; |
michael@0 | 54 | using std::map; |
michael@0 | 55 | using std::pair; |
michael@0 | 56 | using test_assembler::Endianness; |
michael@0 | 57 | using test_assembler::kLittleEndian; |
michael@0 | 58 | using test_assembler::kUnsetEndian; |
michael@0 | 59 | using test_assembler::Label; |
michael@0 | 60 | using test_assembler::Section; |
michael@0 | 61 | |
michael@0 | 62 | // String tables are common in ELF headers, so subclass Section |
michael@0 | 63 | // to make them easy to generate. |
michael@0 | 64 | class StringTable : public Section { |
michael@0 | 65 | public: |
michael@0 | 66 | StringTable(Endianness endianness = kUnsetEndian) |
michael@0 | 67 | : Section(endianness) { |
michael@0 | 68 | start() = 0; |
michael@0 | 69 | empty_string = Add(""); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | // Add the string s to the string table, and return |
michael@0 | 73 | // a label containing the offset into the string table |
michael@0 | 74 | // at which it was added. |
michael@0 | 75 | Label Add(const string& s) { |
michael@0 | 76 | if (strings_.find(s) != strings_.end()) |
michael@0 | 77 | return strings_[s]; |
michael@0 | 78 | |
michael@0 | 79 | Label string_label(Here()); |
michael@0 | 80 | AppendCString(s); |
michael@0 | 81 | strings_[s] = string_label; |
michael@0 | 82 | return string_label; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | // All StringTables contain an empty string as their first |
michael@0 | 86 | // entry. |
michael@0 | 87 | Label empty_string; |
michael@0 | 88 | |
michael@0 | 89 | // Avoid inserting duplicate strings. |
michael@0 | 90 | map<string,Label> strings_; |
michael@0 | 91 | }; |
michael@0 | 92 | |
michael@0 | 93 | // A Section representing an entire ELF file. |
michael@0 | 94 | class ELF : public Section { |
michael@0 | 95 | public: |
michael@0 | 96 | ELF(uint16_t machine, // EM_386, etc |
michael@0 | 97 | uint8_t file_class, // ELFCLASS{32,64} |
michael@0 | 98 | Endianness endianness = kLittleEndian); |
michael@0 | 99 | |
michael@0 | 100 | // Add the Section section to the section header table and append it |
michael@0 | 101 | // to the file. Returns the index of the section in the section |
michael@0 | 102 | // header table. |
michael@0 | 103 | int AddSection(const string& name, const Section& section, |
michael@0 | 104 | uint32_t type, uint32_t flags = 0, uint64_t addr = 0, |
michael@0 | 105 | uint32_t link = 0, uint64_t entsize = 0, uint64_t offset = 0); |
michael@0 | 106 | |
michael@0 | 107 | // Add a segment containing from section index start to section index end. |
michael@0 | 108 | // The indexes must have been gotten from AddSection. |
michael@0 | 109 | void AddSegment(int start, int end, uint32_t type, uint32_t flags = 0); |
michael@0 | 110 | |
michael@0 | 111 | // Write out all data. GetContents may be used after this. |
michael@0 | 112 | void Finish(); |
michael@0 | 113 | |
michael@0 | 114 | private: |
michael@0 | 115 | // Size of an address, in bytes. |
michael@0 | 116 | const size_t addr_size_; |
michael@0 | 117 | |
michael@0 | 118 | // Offset to the program header table. |
michael@0 | 119 | Label program_header_label_; |
michael@0 | 120 | // Number of entries in the program header table. |
michael@0 | 121 | int program_count_; |
michael@0 | 122 | Label program_count_label_; |
michael@0 | 123 | // The program header table itself. |
michael@0 | 124 | Section program_header_table_; |
michael@0 | 125 | |
michael@0 | 126 | // Offset to the section header table. |
michael@0 | 127 | Label section_header_label_; |
michael@0 | 128 | // Number of entries in the section header table. |
michael@0 | 129 | int section_count_; |
michael@0 | 130 | Label section_count_label_; |
michael@0 | 131 | // The section header table itself. |
michael@0 | 132 | Section section_header_table_; |
michael@0 | 133 | |
michael@0 | 134 | // Index of the section header string table in the section |
michael@0 | 135 | // header table. |
michael@0 | 136 | Label section_header_string_index_; |
michael@0 | 137 | // Section containing the names of section header table entries. |
michael@0 | 138 | StringTable section_header_strings_; |
michael@0 | 139 | |
michael@0 | 140 | // Record of an added section |
michael@0 | 141 | struct ElfSection : public Section { |
michael@0 | 142 | ElfSection(const Section& section, uint32_t type, uint32_t addr, |
michael@0 | 143 | uint32_t offset, Label offset_label, uint32_t size) |
michael@0 | 144 | : Section(section), type_(type), addr_(addr), offset_(offset) |
michael@0 | 145 | , offset_label_(offset_label), size_(size) { |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | uint32_t type_; |
michael@0 | 149 | uint32_t addr_; |
michael@0 | 150 | uint32_t offset_; |
michael@0 | 151 | Label offset_label_; |
michael@0 | 152 | uint32_t size_; |
michael@0 | 153 | }; |
michael@0 | 154 | |
michael@0 | 155 | vector<ElfSection> sections_; |
michael@0 | 156 | |
michael@0 | 157 | void AppendSection(ElfSection §ion); |
michael@0 | 158 | }; |
michael@0 | 159 | |
michael@0 | 160 | // A class to build .symtab or .dynsym sections. |
michael@0 | 161 | class SymbolTable : public Section { |
michael@0 | 162 | public: |
michael@0 | 163 | // table is the StringTable that contains symbol names. The caller |
michael@0 | 164 | // must ensure that it remains alive for the life of the |
michael@0 | 165 | // SymbolTable. |
michael@0 | 166 | SymbolTable(Endianness endianness, size_t addr_size, StringTable& table); |
michael@0 | 167 | |
michael@0 | 168 | // Add an Elf32_Sym. |
michael@0 | 169 | void AddSymbol(const string& name, uint32_t value, |
michael@0 | 170 | uint32_t size, unsigned info, uint16_t shndx); |
michael@0 | 171 | // Add an Elf64_Sym. |
michael@0 | 172 | void AddSymbol(const string& name, uint64_t value, |
michael@0 | 173 | uint64_t size, unsigned info, uint16_t shndx); |
michael@0 | 174 | |
michael@0 | 175 | private: |
michael@0 | 176 | size_t addr_size_; |
michael@0 | 177 | StringTable& table_; |
michael@0 | 178 | }; |
michael@0 | 179 | |
michael@0 | 180 | // A class for note sections |
michael@0 | 181 | class Notes : public Section { |
michael@0 | 182 | public: |
michael@0 | 183 | Notes(Endianness endianness) |
michael@0 | 184 | : Section(endianness) { |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | // Add a note. |
michael@0 | 188 | void AddNote(int type, const string &name, const uint8_t* desc_bytes, |
michael@0 | 189 | size_t desc_size); |
michael@0 | 190 | }; |
michael@0 | 191 | |
michael@0 | 192 | } // namespace synth_elf |
michael@0 | 193 | } // namespace google_breakpad |
michael@0 | 194 | |
michael@0 | 195 | #endif // COMMON_LINUX_SYNTH_ELF_H_ |