Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "elf_stripper.h" |
| 18 | |
| 19 | #include <vector> |
| 20 | |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 21 | #include "UniquePtr.h" |
| 22 | #include "base/logging.h" |
| 23 | #include "elf_file.h" |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame^] | 24 | #include "elf_utils.h" |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 25 | #include "utils.h" |
| 26 | |
| 27 | namespace art { |
| 28 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 29 | bool ElfStripper::Strip(File* file, std::string* error_msg) { |
| 30 | UniquePtr<ElfFile> elf_file(ElfFile::Open(file, true, false, error_msg)); |
| 31 | if (elf_file.get() == nullptr) { |
| 32 | return false; |
| 33 | } |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 34 | |
| 35 | // ELF files produced by MCLinker look roughly like this |
| 36 | // |
| 37 | // +------------+ |
| 38 | // | Elf32_Ehdr | contains number of Elf32_Shdr and offset to first |
| 39 | // +------------+ |
| 40 | // | Elf32_Phdr | program headers |
| 41 | // | Elf32_Phdr | |
| 42 | // | ... | |
| 43 | // | Elf32_Phdr | |
| 44 | // +------------+ |
| 45 | // | section | mixture of needed and unneeded sections |
| 46 | // +------------+ |
| 47 | // | section | |
| 48 | // +------------+ |
| 49 | // | ... | |
| 50 | // +------------+ |
| 51 | // | section | |
| 52 | // +------------+ |
| 53 | // | Elf32_Shdr | section headers |
| 54 | // | Elf32_Shdr | |
| 55 | // | ... | contains offset to section start |
| 56 | // | Elf32_Shdr | |
| 57 | // +------------+ |
| 58 | // |
| 59 | // To strip: |
| 60 | // - leave the Elf32_Ehdr and Elf32_Phdr values in place. |
| 61 | // - walk the sections making a new set of Elf32_Shdr section headers for what we want to keep |
| 62 | // - move the sections are keeping up to fill in gaps of sections we want to strip |
| 63 | // - write new Elf32_Shdr section headers to end of file, updating Elf32_Ehdr |
| 64 | // - truncate rest of file |
| 65 | // |
| 66 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame^] | 67 | std::vector<Elf32_Shdr> section_headers; |
| 68 | std::vector<Elf32_Word> section_headers_original_indexes; |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 69 | section_headers.reserve(elf_file->GetSectionHeaderNum()); |
| 70 | |
| 71 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame^] | 72 | Elf32_Shdr& string_section = elf_file->GetSectionNameStringSection(); |
| 73 | for (Elf32_Word i = 0; i < elf_file->GetSectionHeaderNum(); i++) { |
| 74 | Elf32_Shdr& sh = elf_file->GetSectionHeader(i); |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 75 | const char* name = elf_file->GetString(string_section, sh.sh_name); |
| 76 | if (name == NULL) { |
| 77 | CHECK_EQ(0U, i); |
| 78 | section_headers.push_back(sh); |
| 79 | section_headers_original_indexes.push_back(0); |
| 80 | continue; |
| 81 | } |
| 82 | if (StartsWith(name, ".debug") |
| 83 | || (strcmp(name, ".strtab") == 0) |
| 84 | || (strcmp(name, ".symtab") == 0)) { |
| 85 | continue; |
| 86 | } |
| 87 | section_headers.push_back(sh); |
| 88 | section_headers_original_indexes.push_back(i); |
| 89 | } |
| 90 | CHECK_NE(0U, section_headers.size()); |
| 91 | CHECK_EQ(section_headers.size(), section_headers_original_indexes.size()); |
| 92 | |
| 93 | // section 0 is the NULL section, sections start at offset of first section |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame^] | 94 | Elf32_Off offset = elf_file->GetSectionHeader(1).sh_offset; |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 95 | for (size_t i = 1; i < section_headers.size(); i++) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame^] | 96 | Elf32_Shdr& new_sh = section_headers[i]; |
| 97 | Elf32_Shdr& old_sh = elf_file->GetSectionHeader(section_headers_original_indexes[i]); |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 98 | CHECK_EQ(new_sh.sh_name, old_sh.sh_name); |
| 99 | if (old_sh.sh_addralign > 1) { |
| 100 | offset = RoundUp(offset, old_sh.sh_addralign); |
| 101 | } |
| 102 | if (old_sh.sh_offset == offset) { |
| 103 | // already in place |
| 104 | offset += old_sh.sh_size; |
| 105 | continue; |
| 106 | } |
| 107 | // shift section earlier |
| 108 | memmove(elf_file->Begin() + offset, |
| 109 | elf_file->Begin() + old_sh.sh_offset, |
| 110 | old_sh.sh_size); |
| 111 | new_sh.sh_offset = offset; |
| 112 | offset += old_sh.sh_size; |
| 113 | } |
| 114 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame^] | 115 | Elf32_Off shoff = offset; |
| 116 | size_t section_headers_size_in_bytes = section_headers.size() * sizeof(Elf32_Shdr); |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 117 | memcpy(elf_file->Begin() + offset, §ion_headers[0], section_headers_size_in_bytes); |
| 118 | offset += section_headers_size_in_bytes; |
| 119 | |
| 120 | elf_file->GetHeader().e_shnum = section_headers.size(); |
| 121 | elf_file->GetHeader().e_shoff = shoff; |
| 122 | int result = ftruncate(file->Fd(), offset); |
| 123 | if (result != 0) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 124 | *error_msg = StringPrintf("Failed to truncate while stripping ELF file: '%s': %s", |
| 125 | file->GetPath().c_str(), strerror(errno)); |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 126 | return false; |
| 127 | } |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | } // namespace art |