1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CRAZY_LINKER_PROC_MAPS_H
6#define CRAZY_LINKER_PROC_MAPS_H
7
8// Helper classes and functions to extract useful information out of
9// /proc/self/maps.
10
11#include <stdint.h>
12#include <sys/mman.h>  // for PROT_READ etc...
13
14namespace crazy {
15
16class ProcMapsInternal;
17
18class ProcMaps {
19 public:
20  // Used to open /proc/self/maps.
21  // There is no error reporting. If the file can't be opened, then
22  // GetNextEntry() will return false on the first call.
23  ProcMaps();
24
25  // Used to open /proc/$PID/maps.
26  // There is also no error reporting.
27  explicit ProcMaps(pid_t pid);
28
29  ~ProcMaps();
30
31  // Small structure to model an entry.
32  struct Entry {
33    size_t vma_start;
34    size_t vma_end;
35    int prot_flags;
36    size_t load_offset;
37    const char* path;  // can be NULL, not always zero-terminated.
38    size_t path_len;   // 0 if |path| is NULL.
39  };
40
41  void Rewind();
42
43  // Get next entry in maps, return NULL on failure.
44  // On success, return true and set |entry| to valid values.
45  // Note that the |entry->path| field can be NULL or will point to
46  // an address which content may change on the next call to this method.
47  bool GetNextEntry(Entry* entry);
48
49  int GetProtectionFlagsForAddress(void* address);
50
51 private:
52  ProcMapsInternal* internal_;
53};
54
55// Find which loaded ELF binary contains |address|.
56// On success, returns true and sets |*load_address| to its load address,
57// and fills |path_buffer| with the path to the corresponding file.
58bool FindElfBinaryForAddress(void* address,
59                             uintptr_t* load_address,
60                             char* path_buffer,
61                             size_t path_buffer_len);
62
63// Returns the current protection bit flags for the page holding a given
64// |address|. On success, returns true and sets |*prot_flags|.
65bool FindProtectionFlagsForAddress(void* address, int* prot_flags);
66
67// Return the load address of a given ELF binary.
68// If |file_name| contains a slash, this will try to perform an
69// exact match with the content of /proc/self/maps. Otherwise,
70// it will be taken as a base name, and the load address of the first
71// matching entry will be returned.
72// On success, returns true and sets |*load_address| to the load address,
73// and |*load_offset| to the load offset.
74bool FindLoadAddressForFile(const char* file_name,
75                            uintptr_t* load_address,
76                            uintptr_t* load_offset);
77
78}  // namespace crazy
79
80#endif  // CRAZY_LINKER_PROC_MAPS_H
81