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 | #include "common/linux/synth_elf.h" |
michael@0 | 2 | |
michael@0 | 3 | #include <assert.h> |
michael@0 | 4 | #include <elf.h> |
michael@0 | 5 | #include <stdio.h> |
michael@0 | 6 | #include <string.h> |
michael@0 | 7 | |
michael@0 | 8 | #include "common/using_std_string.h" |
michael@0 | 9 | |
michael@0 | 10 | namespace google_breakpad { |
michael@0 | 11 | namespace synth_elf { |
michael@0 | 12 | |
michael@0 | 13 | #ifndef NT_GNU_BUILD_ID |
michael@0 | 14 | #define NT_GNU_BUILD_ID 3 |
michael@0 | 15 | #endif |
michael@0 | 16 | |
michael@0 | 17 | ELF::ELF(uint16_t machine, |
michael@0 | 18 | uint8_t file_class, |
michael@0 | 19 | Endianness endianness) |
michael@0 | 20 | : Section(endianness), |
michael@0 | 21 | addr_size_(file_class == ELFCLASS64 ? 8 : 4), |
michael@0 | 22 | program_count_(0), |
michael@0 | 23 | program_header_table_(endianness), |
michael@0 | 24 | section_count_(0), |
michael@0 | 25 | section_header_table_(endianness), |
michael@0 | 26 | section_header_strings_(endianness) { |
michael@0 | 27 | // Could add support for more machine types here if needed. |
michael@0 | 28 | assert(machine == EM_386 || |
michael@0 | 29 | machine == EM_X86_64 || |
michael@0 | 30 | machine == EM_ARM); |
michael@0 | 31 | assert(file_class == ELFCLASS32 || file_class == ELFCLASS64); |
michael@0 | 32 | |
michael@0 | 33 | start() = 0; |
michael@0 | 34 | // Add ELF header |
michael@0 | 35 | // e_ident |
michael@0 | 36 | // EI_MAG0...EI_MAG3 |
michael@0 | 37 | D8(ELFMAG0); |
michael@0 | 38 | D8(ELFMAG1); |
michael@0 | 39 | D8(ELFMAG2); |
michael@0 | 40 | D8(ELFMAG3); |
michael@0 | 41 | // EI_CLASS |
michael@0 | 42 | D8(file_class); |
michael@0 | 43 | // EI_DATA |
michael@0 | 44 | D8(endianness == kLittleEndian ? ELFDATA2LSB : ELFDATA2MSB); |
michael@0 | 45 | // EI_VERSION |
michael@0 | 46 | D8(EV_CURRENT); |
michael@0 | 47 | // EI_OSABI |
michael@0 | 48 | D8(ELFOSABI_SYSV); |
michael@0 | 49 | // EI_ABIVERSION |
michael@0 | 50 | D8(0); |
michael@0 | 51 | // EI_PAD |
michael@0 | 52 | Append(7, 0); |
michael@0 | 53 | assert(Size() == EI_NIDENT); |
michael@0 | 54 | |
michael@0 | 55 | // e_type |
michael@0 | 56 | D16(ET_EXEC); //TODO: allow passing ET_DYN? |
michael@0 | 57 | // e_machine |
michael@0 | 58 | D16(machine); |
michael@0 | 59 | // e_version |
michael@0 | 60 | D32(EV_CURRENT); |
michael@0 | 61 | // e_entry |
michael@0 | 62 | Append(endianness, addr_size_, 0); |
michael@0 | 63 | // e_phoff |
michael@0 | 64 | Append(endianness, addr_size_, program_header_label_); |
michael@0 | 65 | // e_shoff |
michael@0 | 66 | Append(endianness, addr_size_, section_header_label_); |
michael@0 | 67 | // e_flags |
michael@0 | 68 | D32(0); |
michael@0 | 69 | // e_ehsize |
michael@0 | 70 | D16(addr_size_ == 8 ? sizeof(Elf64_Ehdr) : sizeof(Elf32_Ehdr)); |
michael@0 | 71 | // e_phentsize |
michael@0 | 72 | D16(addr_size_ == 8 ? sizeof(Elf64_Phdr) : sizeof(Elf32_Phdr)); |
michael@0 | 73 | // e_phnum |
michael@0 | 74 | D16(program_count_label_); |
michael@0 | 75 | // e_shentsize |
michael@0 | 76 | D16(addr_size_ == 8 ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr)); |
michael@0 | 77 | // e_shnum |
michael@0 | 78 | D16(section_count_label_); |
michael@0 | 79 | // e_shstrndx |
michael@0 | 80 | D16(section_header_string_index_); |
michael@0 | 81 | |
michael@0 | 82 | // Add an empty section for SHN_UNDEF. |
michael@0 | 83 | Section shn_undef; |
michael@0 | 84 | AddSection("", shn_undef, SHT_NULL); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | int ELF::AddSection(const string& name, const Section& section, |
michael@0 | 88 | uint32_t type, uint32_t flags, uint64_t addr, |
michael@0 | 89 | uint32_t link, uint64_t entsize, uint64_t offset) { |
michael@0 | 90 | Label offset_label; |
michael@0 | 91 | Label string_label(section_header_strings_.Add(name)); |
michael@0 | 92 | size_t size = section.Size(); |
michael@0 | 93 | |
michael@0 | 94 | int index = section_count_; |
michael@0 | 95 | ++section_count_; |
michael@0 | 96 | |
michael@0 | 97 | section_header_table_ |
michael@0 | 98 | // sh_name |
michael@0 | 99 | .D32(string_label) |
michael@0 | 100 | // sh_type |
michael@0 | 101 | .D32(type) |
michael@0 | 102 | // sh_flags |
michael@0 | 103 | .Append(endianness(), addr_size_, flags) |
michael@0 | 104 | // sh_addr |
michael@0 | 105 | .Append(endianness(), addr_size_, addr) |
michael@0 | 106 | // sh_offset |
michael@0 | 107 | .Append(endianness(), addr_size_, offset_label) |
michael@0 | 108 | // sh_size |
michael@0 | 109 | .Append(endianness(), addr_size_, size) |
michael@0 | 110 | // sh_link |
michael@0 | 111 | .D32(link) |
michael@0 | 112 | // sh_info |
michael@0 | 113 | .D32(0) |
michael@0 | 114 | // sh_addralign |
michael@0 | 115 | .Append(endianness(), addr_size_, 0) |
michael@0 | 116 | // sh_entsize |
michael@0 | 117 | .Append(endianness(), addr_size_, entsize); |
michael@0 | 118 | |
michael@0 | 119 | sections_.push_back(ElfSection(section, type, addr, offset, offset_label, |
michael@0 | 120 | size)); |
michael@0 | 121 | return index; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | void ELF::AppendSection(ElfSection §ion) { |
michael@0 | 125 | // NULL and NOBITS sections have no content, so they |
michael@0 | 126 | // don't need to be written to the file. |
michael@0 | 127 | if (section.type_ == SHT_NULL) { |
michael@0 | 128 | section.offset_label_ = 0; |
michael@0 | 129 | } else if (section.type_ == SHT_NOBITS) { |
michael@0 | 130 | section.offset_label_ = section.offset_; |
michael@0 | 131 | } else { |
michael@0 | 132 | Mark(§ion.offset_label_); |
michael@0 | 133 | Append(section); |
michael@0 | 134 | Align(4); |
michael@0 | 135 | } |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | void ELF::AddSegment(int start, int end, uint32_t type, uint32_t flags) { |
michael@0 | 139 | assert(start > 0); |
michael@0 | 140 | assert(size_t(start) < sections_.size()); |
michael@0 | 141 | assert(end > 0); |
michael@0 | 142 | assert(size_t(end) < sections_.size()); |
michael@0 | 143 | ++program_count_; |
michael@0 | 144 | |
michael@0 | 145 | // p_type |
michael@0 | 146 | program_header_table_.D32(type); |
michael@0 | 147 | |
michael@0 | 148 | if (addr_size_ == 8) { |
michael@0 | 149 | // p_flags |
michael@0 | 150 | program_header_table_.D32(flags); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | size_t filesz = 0; |
michael@0 | 154 | size_t memsz = 0; |
michael@0 | 155 | bool prev_was_nobits = false; |
michael@0 | 156 | for (int i = start; i <= end; ++i) { |
michael@0 | 157 | size_t size = sections_[i].size_; |
michael@0 | 158 | if (sections_[i].type_ != SHT_NOBITS) { |
michael@0 | 159 | assert(!prev_was_nobits); |
michael@0 | 160 | // non SHT_NOBITS sections are 4-byte aligned (see AddSection) |
michael@0 | 161 | size = (size + 3) & ~3; |
michael@0 | 162 | filesz += size; |
michael@0 | 163 | } else { |
michael@0 | 164 | prev_was_nobits = true; |
michael@0 | 165 | } |
michael@0 | 166 | memsz += size; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | program_header_table_ |
michael@0 | 170 | // p_offset |
michael@0 | 171 | .Append(endianness(), addr_size_, sections_[start].offset_label_) |
michael@0 | 172 | // p_vaddr |
michael@0 | 173 | .Append(endianness(), addr_size_, sections_[start].addr_) |
michael@0 | 174 | // p_paddr |
michael@0 | 175 | .Append(endianness(), addr_size_, sections_[start].addr_) |
michael@0 | 176 | // p_filesz |
michael@0 | 177 | .Append(endianness(), addr_size_, filesz) |
michael@0 | 178 | // p_memsz |
michael@0 | 179 | .Append(endianness(), addr_size_, memsz); |
michael@0 | 180 | |
michael@0 | 181 | if (addr_size_ == 4) { |
michael@0 | 182 | // p_flags |
michael@0 | 183 | program_header_table_.D32(flags); |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | // p_align |
michael@0 | 187 | program_header_table_.Append(endianness(), addr_size_, 0); |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | void ELF::Finish() { |
michael@0 | 191 | // Add the section header string table at the end. |
michael@0 | 192 | section_header_string_index_ = section_count_; |
michael@0 | 193 | //printf(".shstrtab size: %ld\n", section_header_strings_.Size()); |
michael@0 | 194 | AddSection(".shstrtab", section_header_strings_, SHT_STRTAB); |
michael@0 | 195 | //printf("section_count_: %ld, sections_.size(): %ld\n", |
michael@0 | 196 | // section_count_, sections_.size()); |
michael@0 | 197 | if (program_count_) { |
michael@0 | 198 | Mark(&program_header_label_); |
michael@0 | 199 | Append(program_header_table_); |
michael@0 | 200 | } else { |
michael@0 | 201 | program_header_label_ = 0; |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | for (vector<ElfSection>::iterator it = sections_.begin(); |
michael@0 | 205 | it < sections_.end(); ++it) { |
michael@0 | 206 | AppendSection(*it); |
michael@0 | 207 | } |
michael@0 | 208 | section_count_label_ = section_count_; |
michael@0 | 209 | program_count_label_ = program_count_; |
michael@0 | 210 | |
michael@0 | 211 | // Section header table starts here. |
michael@0 | 212 | Mark(§ion_header_label_); |
michael@0 | 213 | Append(section_header_table_); |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | SymbolTable::SymbolTable(Endianness endianness, |
michael@0 | 217 | size_t addr_size, |
michael@0 | 218 | StringTable& table) : Section(endianness), |
michael@0 | 219 | addr_size_(addr_size), |
michael@0 | 220 | table_(table) { |
michael@0 | 221 | assert(addr_size_ == 4 || addr_size_ == 8); |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | void SymbolTable::AddSymbol(const string& name, uint32_t value, |
michael@0 | 225 | uint32_t size, unsigned info, uint16_t shndx) { |
michael@0 | 226 | assert(addr_size_ == 4); |
michael@0 | 227 | D32(table_.Add(name)); |
michael@0 | 228 | D32(value); |
michael@0 | 229 | D32(size); |
michael@0 | 230 | D8(info); |
michael@0 | 231 | D8(0); // other |
michael@0 | 232 | D16(shndx); |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | void SymbolTable::AddSymbol(const string& name, uint64_t value, |
michael@0 | 236 | uint64_t size, unsigned info, uint16_t shndx) { |
michael@0 | 237 | assert(addr_size_ == 8); |
michael@0 | 238 | D32(table_.Add(name)); |
michael@0 | 239 | D8(info); |
michael@0 | 240 | D8(0); // other |
michael@0 | 241 | D16(shndx); |
michael@0 | 242 | D64(value); |
michael@0 | 243 | D64(size); |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | void Notes::AddNote(int type, const string &name, const uint8_t* desc_bytes, |
michael@0 | 247 | size_t desc_size) { |
michael@0 | 248 | // Elf32_Nhdr and Elf64_Nhdr are exactly the same. |
michael@0 | 249 | Elf32_Nhdr note_header; |
michael@0 | 250 | memset(¬e_header, 0, sizeof(note_header)); |
michael@0 | 251 | note_header.n_namesz = name.length() + 1; |
michael@0 | 252 | note_header.n_descsz = desc_size; |
michael@0 | 253 | note_header.n_type = type; |
michael@0 | 254 | |
michael@0 | 255 | Append(reinterpret_cast<const uint8_t*>(¬e_header), |
michael@0 | 256 | sizeof(note_header)); |
michael@0 | 257 | AppendCString(name); |
michael@0 | 258 | Align(4); |
michael@0 | 259 | Append(desc_bytes, desc_size); |
michael@0 | 260 | Align(4); |
michael@0 | 261 | } |
michael@0 | 262 | |
michael@0 | 263 | } // namespace synth_elf |
michael@0 | 264 | } // namespace google_breakpad |