Move elf definitions to a header file
They're going to be used by a unit test in the future.
Bug: 200136875
Change-Id: Id89370ad46af33dc42c8c3f541be2d7e0adf8565
diff --git a/BUILD b/BUILD
index 3124f4b..310f730 100644
--- a/BUILD
+++ b/BUILD
@@ -791,6 +791,7 @@
"src/profiling/symbolizer/breakpad_parser.h",
"src/profiling/symbolizer/breakpad_symbolizer.cc",
"src/profiling/symbolizer/breakpad_symbolizer.h",
+ "src/profiling/symbolizer/elf.h",
"src/profiling/symbolizer/filesystem.h",
"src/profiling/symbolizer/filesystem_posix.cc",
"src/profiling/symbolizer/filesystem_windows.cc",
diff --git a/src/profiling/symbolizer/BUILD.gn b/src/profiling/symbolizer/BUILD.gn
index 03dfe0a..f16352a 100644
--- a/src/profiling/symbolizer/BUILD.gn
+++ b/src/profiling/symbolizer/BUILD.gn
@@ -23,6 +23,7 @@
"breakpad_parser.h",
"breakpad_symbolizer.cc",
"breakpad_symbolizer.h",
+ "elf.h",
"filesystem.h",
"filesystem_posix.cc",
"filesystem_windows.cc",
diff --git a/src/profiling/symbolizer/elf.h b/src/profiling/symbolizer/elf.h
new file mode 100644
index 0000000..e323d46
--- /dev/null
+++ b/src/profiling/symbolizer/elf.h
@@ -0,0 +1,167 @@
+/*
+ * Copyright (C) 2021 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.
+ */
+
+#ifndef SRC_PROFILING_SYMBOLIZER_ELF_H_
+#define SRC_PROFILING_SYMBOLIZER_ELF_H_
+
+#include <cinttypes>
+
+#include "perfetto/base/build_config.h"
+
+// We cannot just include elf.h, as that only exists on Linux, and we want to
+// allow symbolization on other platforms as well. As we only need a small
+// subset, it is easiest to define the constants and structs ourselves.
+
+namespace perfetto {
+namespace profiling {
+
+constexpr auto PT_LOAD = 1;
+constexpr auto PF_X = 1;
+constexpr auto SHT_NOTE = 7;
+constexpr auto NT_GNU_BUILD_ID = 3;
+constexpr auto ELFCLASS32 = 1;
+constexpr auto ELFCLASS64 = 2;
+constexpr auto ELFMAG0 = 0x7f;
+constexpr auto ELFMAG1 = 'E';
+constexpr auto ELFMAG2 = 'L';
+constexpr auto ELFMAG3 = 'F';
+constexpr auto EI_MAG0 = 0;
+constexpr auto EI_MAG1 = 1;
+constexpr auto EI_MAG2 = 2;
+constexpr auto EI_MAG3 = 3;
+constexpr auto EI_CLASS = 4;
+
+struct Elf32 {
+ using Addr = uint32_t;
+ using Half = uint16_t;
+ using Off = uint32_t;
+ using Sword = int32_t;
+ using Word = uint32_t;
+ struct Ehdr {
+ unsigned char e_ident[16];
+ Half e_type;
+ Half e_machine;
+ Word e_version;
+ Addr e_entry;
+ Off e_phoff;
+ Off e_shoff;
+ Word e_flags;
+ Half e_ehsize;
+ Half e_phentsize;
+ Half e_phnum;
+ Half e_shentsize;
+ Half e_shnum;
+ Half e_shstrndx;
+ };
+ struct Shdr {
+ Word sh_name;
+ Word sh_type;
+ Word sh_flags;
+ Addr sh_addr;
+ Off sh_offset;
+ Word sh_size;
+ Word sh_link;
+ Word sh_info;
+ Word sh_addralign;
+ Word sh_entsize;
+ };
+ struct Nhdr {
+ Word n_namesz;
+ Word n_descsz;
+ Word n_type;
+ };
+ struct Phdr {
+ uint32_t p_type;
+ Off p_offset;
+ Addr p_vaddr;
+ Addr p_paddr;
+ uint32_t p_filesz;
+ uint32_t p_memsz;
+ uint32_t p_flags;
+ uint32_t p_align;
+ };
+};
+
+struct Elf64 {
+ using Addr = uint64_t;
+ using Half = uint16_t;
+ using SHalf = int16_t;
+ using Off = uint64_t;
+ using Sword = int32_t;
+ using Word = uint32_t;
+ using Xword = uint64_t;
+ using Sxword = int64_t;
+ struct Ehdr {
+ unsigned char e_ident[16];
+ Half e_type;
+ Half e_machine;
+ Word e_version;
+ Addr e_entry;
+ Off e_phoff;
+ Off e_shoff;
+ Word e_flags;
+ Half e_ehsize;
+ Half e_phentsize;
+ Half e_phnum;
+ Half e_shentsize;
+ Half e_shnum;
+ Half e_shstrndx;
+ };
+ struct Shdr {
+ Word sh_name;
+ Word sh_type;
+ Xword sh_flags;
+ Addr sh_addr;
+ Off sh_offset;
+ Xword sh_size;
+ Word sh_link;
+ Word sh_info;
+ Xword sh_addralign;
+ Xword sh_entsize;
+ };
+ struct Nhdr {
+ Word n_namesz;
+ Word n_descsz;
+ Word n_type;
+ };
+ struct Phdr {
+ uint32_t p_type;
+ uint32_t p_flags;
+ Off p_offset;
+ Addr p_vaddr;
+ Addr p_paddr;
+ uint64_t p_filesz;
+ uint64_t p_memsz;
+ uint64_t p_align;
+ };
+};
+
+template <typename E>
+typename E::Shdr* GetShdr(void* mem, const typename E::Ehdr* ehdr, size_t i) {
+ return reinterpret_cast<typename E::Shdr*>(
+ static_cast<char*>(mem) + ehdr->e_shoff + i * sizeof(typename E::Shdr));
+}
+
+template <typename E>
+typename E::Phdr* GetPhdr(void* mem, const typename E::Ehdr* ehdr, size_t i) {
+ return reinterpret_cast<typename E::Phdr*>(
+ static_cast<char*>(mem) + ehdr->e_phoff + i * sizeof(typename E::Phdr));
+}
+
+} // namespace profiling
+} // namespace perfetto
+
+#endif // SRC_PROFILING_SYMBOLIZER_ELF_H_
diff --git a/src/profiling/symbolizer/local_symbolizer.cc b/src/profiling/symbolizer/local_symbolizer.cc
index 8dfb9c9..3375837 100644
--- a/src/profiling/symbolizer/local_symbolizer.cc
+++ b/src/profiling/symbolizer/local_symbolizer.cc
@@ -31,6 +31,7 @@
#include "perfetto/ext/base/optional.h"
#include "perfetto/ext/base/scoped_file.h"
#include "perfetto/ext/base/string_utils.h"
+#include "src/profiling/symbolizer/elf.h"
#include "src/profiling/symbolizer/filesystem.h"
#include "src/profiling/symbolizer/scoped_read_mmap.h"
@@ -116,142 +117,6 @@
}
namespace {
-// We cannot just include elf.h, as that only exists on Linux, and we want to
-// allow symbolization on other platforms as well. As we only need a small
-// subset, it is easiest to define the constants and structs ourselves.
-constexpr auto PT_LOAD = 1;
-constexpr auto PF_X = 1;
-constexpr auto SHT_NOTE = 7;
-constexpr auto NT_GNU_BUILD_ID = 3;
-constexpr auto ELFCLASS32 = 1;
-constexpr auto ELFCLASS64 = 2;
-constexpr auto ELFMAG0 = 0x7f;
-constexpr auto ELFMAG1 = 'E';
-constexpr auto ELFMAG2 = 'L';
-constexpr auto ELFMAG3 = 'F';
-constexpr auto EI_MAG0 = 0;
-constexpr auto EI_MAG1 = 1;
-constexpr auto EI_MAG2 = 2;
-constexpr auto EI_MAG3 = 3;
-constexpr auto EI_CLASS = 4;
-
-struct Elf32 {
- using Addr = uint32_t;
- using Half = uint16_t;
- using Off = uint32_t;
- using Sword = int32_t;
- using Word = uint32_t;
- struct Ehdr {
- unsigned char e_ident[16];
- Half e_type;
- Half e_machine;
- Word e_version;
- Addr e_entry;
- Off e_phoff;
- Off e_shoff;
- Word e_flags;
- Half e_ehsize;
- Half e_phentsize;
- Half e_phnum;
- Half e_shentsize;
- Half e_shnum;
- Half e_shstrndx;
- };
- struct Shdr {
- Word sh_name;
- Word sh_type;
- Word sh_flags;
- Addr sh_addr;
- Off sh_offset;
- Word sh_size;
- Word sh_link;
- Word sh_info;
- Word sh_addralign;
- Word sh_entsize;
- };
- struct Nhdr {
- Word n_namesz;
- Word n_descsz;
- Word n_type;
- };
- struct Phdr {
- uint32_t p_type;
- Off p_offset;
- Addr p_vaddr;
- Addr p_paddr;
- uint32_t p_filesz;
- uint32_t p_memsz;
- uint32_t p_flags;
- uint32_t p_align;
- };
-};
-
-struct Elf64 {
- using Addr = uint64_t;
- using Half = uint16_t;
- using SHalf = int16_t;
- using Off = uint64_t;
- using Sword = int32_t;
- using Word = uint32_t;
- using Xword = uint64_t;
- using Sxword = int64_t;
- struct Ehdr {
- unsigned char e_ident[16];
- Half e_type;
- Half e_machine;
- Word e_version;
- Addr e_entry;
- Off e_phoff;
- Off e_shoff;
- Word e_flags;
- Half e_ehsize;
- Half e_phentsize;
- Half e_phnum;
- Half e_shentsize;
- Half e_shnum;
- Half e_shstrndx;
- };
- struct Shdr {
- Word sh_name;
- Word sh_type;
- Xword sh_flags;
- Addr sh_addr;
- Off sh_offset;
- Xword sh_size;
- Word sh_link;
- Word sh_info;
- Xword sh_addralign;
- Xword sh_entsize;
- };
- struct Nhdr {
- Word n_namesz;
- Word n_descsz;
- Word n_type;
- };
- struct Phdr {
- uint32_t p_type;
- uint32_t p_flags;
- Off p_offset;
- Addr p_vaddr;
- Addr p_paddr;
- uint64_t p_filesz;
- uint64_t p_memsz;
- uint64_t p_align;
- };
-};
-
-template <typename E>
-typename E::Shdr* GetShdr(void* mem, const typename E::Ehdr* ehdr, size_t i) {
- return reinterpret_cast<typename E::Shdr*>(
- static_cast<char*>(mem) + ehdr->e_shoff + i * sizeof(typename E::Shdr));
-}
-
-template <typename E>
-typename E::Phdr* GetPhdr(void* mem, const typename E::Ehdr* ehdr, size_t i) {
- return reinterpret_cast<typename E::Phdr*>(
- static_cast<char*>(mem) + ehdr->e_phoff + i * sizeof(typename E::Phdr));
-}
-
bool InRange(const void* base,
size_t total_size,
const void* ptr,