File: ElfTestUtils.cpp

package info (click to toggle)
android-platform-system-core 1%3A10.0.0%2Br36-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 123,760 kB
  • sloc: cpp: 197,034; ansic: 18,211; asm: 3,606; sh: 3,180; python: 2,671; java: 693; xml: 266; makefile: 237
file content (142 lines) | stat: -rw-r--r-- 4,511 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <elf.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <unistd.h>

#include <string>
#include <vector>

#include <gtest/gtest.h>

#include "ElfTestUtils.h"

namespace unwindstack {

template <typename Ehdr>
void TestInitEhdr(Ehdr* ehdr, uint32_t elf_class, uint32_t machine_type) {
  memset(ehdr, 0, sizeof(Ehdr));
  memcpy(&ehdr->e_ident[0], ELFMAG, SELFMAG);
  ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
  ehdr->e_ident[EI_VERSION] = EV_CURRENT;
  ehdr->e_ident[EI_OSABI] = ELFOSABI_SYSV;
  ehdr->e_ident[EI_CLASS] = elf_class;
  ehdr->e_type = ET_DYN;
  ehdr->e_machine = machine_type;
  ehdr->e_version = EV_CURRENT;
  ehdr->e_ehsize = sizeof(Ehdr);
}

std::string TestGetFileDirectory() {
  std::string exec(testing::internal::GetArgvs()[0]);
  auto const value = exec.find_last_of('/');
  if (value == std::string::npos) {
    return "tests/files/";
  }
  return exec.substr(0, value + 1) + "tests/files/";
}

template <typename Ehdr, typename Shdr>
void TestInitGnuDebugdata(uint32_t elf_class, uint32_t machine, bool init_gnu_debugdata,
                          TestCopyFuncType copy_func) {
  Ehdr ehdr;

  TestInitEhdr(&ehdr, elf_class, machine);

  uint64_t offset = sizeof(Ehdr);
  ehdr.e_shoff = offset;
  ehdr.e_shnum = 3;
  ehdr.e_shentsize = sizeof(Shdr);
  ehdr.e_shstrndx = 2;
  copy_func(0, &ehdr, sizeof(ehdr));

  Shdr shdr;
  memset(&shdr, 0, sizeof(shdr));
  shdr.sh_type = SHT_NULL;
  copy_func(offset, &shdr, sizeof(shdr));
  offset += ehdr.e_shentsize;

  // Skip this header, it will contain the gnu_debugdata information.
  uint64_t gnu_offset = offset;
  offset += ehdr.e_shentsize;

  uint64_t symtab_offset = sizeof(ehdr) + ehdr.e_shnum * ehdr.e_shentsize;
  memset(&shdr, 0, sizeof(shdr));
  shdr.sh_name = 1;
  shdr.sh_type = SHT_STRTAB;
  shdr.sh_offset = symtab_offset;
  shdr.sh_size = 0x100;
  copy_func(offset, &shdr, sizeof(shdr));

  char value = '\0';
  uint64_t symname_offset = symtab_offset;
  copy_func(symname_offset, &value, 1);
  symname_offset++;
  std::string name(".shstrtab");
  copy_func(symname_offset, name.c_str(), name.size() + 1);
  symname_offset += name.size() + 1;
  name = ".gnu_debugdata";
  copy_func(symname_offset, name.c_str(), name.size() + 1);

  ssize_t bytes = 0x100;
  offset = symtab_offset + 0x100;
  if (init_gnu_debugdata) {
    // Read in the compressed elf data and copy it in.
    name = TestGetFileDirectory();
    if (elf_class == ELFCLASS32) {
      name += "elf32.xz";
    } else {
      name += "elf64.xz";
    }
    int fd = TEMP_FAILURE_RETRY(open(name.c_str(), O_RDONLY));
    ASSERT_NE(-1, fd) << "Cannot open " + name;
    // Assumes the file is less than 1024 bytes.
    std::vector<uint8_t> buf(1024);
    bytes = TEMP_FAILURE_RETRY(read(fd, buf.data(), buf.size()));
    ASSERT_GT(bytes, 0);
    // Make sure the file isn't too big.
    ASSERT_NE(static_cast<size_t>(bytes), buf.size())
        << "File " + name + " is too big, increase buffer size.";
    close(fd);
    buf.resize(bytes);
    copy_func(offset, buf.data(), buf.size());
  }

  memset(&shdr, 0, sizeof(shdr));
  shdr.sh_type = SHT_PROGBITS;
  shdr.sh_name = symname_offset - symtab_offset;
  shdr.sh_addr = offset;
  shdr.sh_offset = offset;
  shdr.sh_size = bytes;
  copy_func(gnu_offset, &shdr, sizeof(shdr));
}

template void TestInitEhdr<Elf32_Ehdr>(Elf32_Ehdr*, uint32_t, uint32_t);
template void TestInitEhdr<Elf64_Ehdr>(Elf64_Ehdr*, uint32_t, uint32_t);

template void TestInitGnuDebugdata<Elf32_Ehdr, Elf32_Shdr>(uint32_t, uint32_t, bool,
                                                           TestCopyFuncType);
template void TestInitGnuDebugdata<Elf64_Ehdr, Elf64_Shdr>(uint32_t, uint32_t, bool,
                                                           TestCopyFuncType);

}  // namespace unwindstack