Skip to content
This repository
Browse code

Add read/write mode to ElfFile

Summary:
- Add a mode to ElfFile that allows opening the file for read/write
access via PROT_WRITE.

Test Plan:
- Used it in some other code

Reviewed By: simpkins@fb.com

FB internal diff: D740184
  • Loading branch information...
commit 37107ac7392880d73f45068a4f2b8c9f8f2fa157 1 parent ae95c1d
Peter Griess pgriess authored oyamauchi committed
11 folly/experimental/symbolizer/Elf.cpp
@@ -40,8 +40,8 @@ ElfFile::ElfFile()
40 40 baseAddress_(0) {
41 41 }
42 42
43   -ElfFile::ElfFile(const char* name)
44   - : fd_(open(name, O_RDONLY)),
  43 +ElfFile::ElfFile(const char* name, bool readOnly)
  44 + : fd_(open(name, (readOnly) ? O_RDONLY : O_RDWR)),
45 45 file_(static_cast<char*>(MAP_FAILED)),
46 46 length_(0),
47 47 baseAddress_(0) {
@@ -56,8 +56,11 @@ ElfFile::ElfFile(const char* name)
56 56 }
57 57
58 58 length_ = st.st_size;
59   - file_ = static_cast<char*>(
60   - mmap(nullptr, length_, PROT_READ, MAP_SHARED, fd_, 0));
  59 + int prot = PROT_READ;
  60 + if (!readOnly) {
  61 + prot |= PROT_WRITE;
  62 + }
  63 + file_ = static_cast<char*>(mmap(nullptr, length_, prot, MAP_SHARED, fd_, 0));
61 64 if (file_ == MAP_FAILED) {
62 65 folly::throwSystemError("mmap");
63 66 }
2  folly/experimental/symbolizer/Elf.h
@@ -50,7 +50,7 @@ inline void enforce(bool v, Args... args) {
50 50 class ElfFile {
51 51 public:
52 52 ElfFile();
53   - explicit ElfFile(const char* name);
  53 + explicit ElfFile(const char* name, bool readOnly=true);
54 54 ~ElfFile();
55 55
56 56 ElfFile(ElfFile&& other);

0 comments on commit 37107ac

Please sign in to comment.
Something went wrong with that request. Please try again.