blob: 0b86ad0711540ad76595bcef0274104d0ef1e0e7 [file] [log] [blame]
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001/*
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
Nicolas Geoffraye84bfb52014-03-10 11:27:57 +000019#include <unistd.h>
20#include <sys/types.h>
Ian Rogers700a4022014-05-19 16:49:03 -070021#include <memory>
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070022#include <vector>
23
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070024#include "base/logging.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070025#include "base/stringprintf.h"
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070026#include "elf_file.h"
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000027#include "elf_utils.h"
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070028#include "utils.h"
29
30namespace art {
31
Ian Rogers8d31bbd2013-10-13 10:44:14 -070032bool ElfStripper::Strip(File* file, std::string* error_msg) {
Ian Rogers700a4022014-05-19 16:49:03 -070033 std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070034 if (elf_file.get() == nullptr) {
35 return false;
36 }
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070037
38 // ELF files produced by MCLinker look roughly like this
39 //
40 // +------------+
41 // | Elf32_Ehdr | contains number of Elf32_Shdr and offset to first
42 // +------------+
43 // | Elf32_Phdr | program headers
44 // | Elf32_Phdr |
45 // | ... |
46 // | Elf32_Phdr |
47 // +------------+
48 // | section | mixture of needed and unneeded sections
49 // +------------+
50 // | section |
51 // +------------+
52 // | ... |
53 // +------------+
54 // | section |
55 // +------------+
56 // | Elf32_Shdr | section headers
57 // | Elf32_Shdr |
58 // | ... | contains offset to section start
59 // | Elf32_Shdr |
60 // +------------+
61 //
62 // To strip:
63 // - leave the Elf32_Ehdr and Elf32_Phdr values in place.
64 // - walk the sections making a new set of Elf32_Shdr section headers for what we want to keep
65 // - move the sections are keeping up to fill in gaps of sections we want to strip
66 // - write new Elf32_Shdr section headers to end of file, updating Elf32_Ehdr
67 // - truncate rest of file
68 //
69
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000070 std::vector<Elf32_Shdr> section_headers;
71 std::vector<Elf32_Word> section_headers_original_indexes;
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070072 section_headers.reserve(elf_file->GetSectionHeaderNum());
73
74
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000075 Elf32_Shdr& string_section = elf_file->GetSectionNameStringSection();
76 for (Elf32_Word i = 0; i < elf_file->GetSectionHeaderNum(); i++) {
77 Elf32_Shdr& sh = elf_file->GetSectionHeader(i);
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070078 const char* name = elf_file->GetString(string_section, sh.sh_name);
79 if (name == NULL) {
80 CHECK_EQ(0U, i);
81 section_headers.push_back(sh);
82 section_headers_original_indexes.push_back(0);
83 continue;
84 }
85 if (StartsWith(name, ".debug")
86 || (strcmp(name, ".strtab") == 0)
87 || (strcmp(name, ".symtab") == 0)) {
88 continue;
89 }
90 section_headers.push_back(sh);
91 section_headers_original_indexes.push_back(i);
92 }
93 CHECK_NE(0U, section_headers.size());
94 CHECK_EQ(section_headers.size(), section_headers_original_indexes.size());
95
96 // section 0 is the NULL section, sections start at offset of first section
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000097 Elf32_Off offset = elf_file->GetSectionHeader(1).sh_offset;
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070098 for (size_t i = 1; i < section_headers.size(); i++) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000099 Elf32_Shdr& new_sh = section_headers[i];
100 Elf32_Shdr& old_sh = elf_file->GetSectionHeader(section_headers_original_indexes[i]);
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700101 CHECK_EQ(new_sh.sh_name, old_sh.sh_name);
102 if (old_sh.sh_addralign > 1) {
103 offset = RoundUp(offset, old_sh.sh_addralign);
104 }
105 if (old_sh.sh_offset == offset) {
106 // already in place
107 offset += old_sh.sh_size;
108 continue;
109 }
110 // shift section earlier
111 memmove(elf_file->Begin() + offset,
112 elf_file->Begin() + old_sh.sh_offset,
113 old_sh.sh_size);
114 new_sh.sh_offset = offset;
115 offset += old_sh.sh_size;
116 }
117
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000118 Elf32_Off shoff = offset;
119 size_t section_headers_size_in_bytes = section_headers.size() * sizeof(Elf32_Shdr);
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700120 memcpy(elf_file->Begin() + offset, &section_headers[0], section_headers_size_in_bytes);
121 offset += section_headers_size_in_bytes;
122
123 elf_file->GetHeader().e_shnum = section_headers.size();
124 elf_file->GetHeader().e_shoff = shoff;
125 int result = ftruncate(file->Fd(), offset);
126 if (result != 0) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700127 *error_msg = StringPrintf("Failed to truncate while stripping ELF file: '%s': %s",
128 file->GetPath().c_str(), strerror(errno));
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700129 return false;
130 }
131 return true;
132}
133
134} // namespace art