1 /**
2 * Extract filemap info from a coredump (Linux and similar)
3 */
4 /*
5 This file is part of libunwind.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy of
8 this software and associated documentation files (the "Software"), to deal in
9 the Software without restriction, including without limitation the rights to
10 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
11 of the Software, and to permit persons to whom the Software is furnished to do
12 so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 */
25 #include "_UCD_internal.h"
26
27
28 /**
29 * The format of the NT_FILE note is not well documented, but it goes something
30 * like this.
31 *
32 * The note has a header containing the @i count of the number of file maps, plus a
33 * value of the size of the offset field in each map. Since we don;t care about
34 * the offset field in a core file, there is no further information available on
35 * exactly what the means.
36 *
37 * Following the header are @count mapinfo structures. The mapinfo structure consists of
38 * a start address, and end address, and some wacky offset thing. The start and
39 * end address are the virtual addresses of a LOAD segment that was mapped from
40 * the named file.
41 *
42 * Following the array of mapinfo structures is a block of null-terminated C strings
43 * containing the mapped file names. They are ordered correspondingly to each
44 * entry in the map structure array.
45 */
46 typedef struct {
47 unsigned long count;
48 unsigned long pagesz;
49 } linux_mapinfo_hdr_t;
50
51 typedef struct {
52 unsigned long start;
53 unsigned long end;
54 unsigned long offset;
55 } linux_mapinfo_t;
56
57
58 /**
59 * Map a file note to program headers
60 *
61 * If a NT_FILE note is recognized, parse it and add the resulting backing files
62 * to the program header list.
63 *
64 * Any file names that end in the string "(deleted)" are ignored.
65 */
66 static int
_handle_file_note(uint32_t n_namesz,uint32_t n_descsz,uint32_t n_type,char * name,uint8_t * desc,void * arg)67 _handle_file_note(uint32_t n_namesz, uint32_t n_descsz, uint32_t n_type, char *name, uint8_t *desc, void *arg)
68 {
69 struct UCD_info *ui = (struct UCD_info *)arg;
70 #ifdef NT_FILE
71 if (n_type == NT_FILE)
72 {
73 Debug(0, "found a PT_FILE note\n");
74 static const char * deleted = "(deleted)";
75 size_t deleted_len = strlen(deleted);
76 static const size_t mapinfo_offset = sizeof(linux_mapinfo_hdr_t);
77
78 linux_mapinfo_hdr_t *mapinfo = (linux_mapinfo_hdr_t *)desc;
79 linux_mapinfo_t *maps = (linux_mapinfo_t *)(desc + mapinfo_offset);
80 char *strings = (char *)(desc + mapinfo_offset + sizeof(linux_mapinfo_t)*mapinfo->count);
81 for (unsigned long i = 0; i < mapinfo->count; ++i)
82 {
83 size_t len = strlen(strings);
84 for (unsigned p = 0; p < ui->phdrs_count; ++p)
85 {
86 if (ui->phdrs[p].p_type == PT_LOAD
87 && maps[i].start >= ui->phdrs[p].p_vaddr
88 && maps[i].end <= ui->phdrs[p].p_vaddr + ui->phdrs[p].p_filesz)
89 {
90 if (len > deleted_len && memcmp(strings + len - deleted_len, deleted, deleted_len))
91 {
92 _UCD_add_backing_file_at_segment(ui, p, strings);
93 }
94 break;
95 }
96 }
97 strings += (len + 1);
98 }
99 }
100 #endif
101 return UNW_ESUCCESS;
102 }
103
104
105 /**
106 * Get filemap info from core file (Linux and similar)
107 *
108 * If there is a mapinfo not in the core file, map its contents to the phdrs.
109 *
110 * Since there may or may not be any mapinfo notes it's OK for this function to
111 * fail.
112 */
113 int
_UCD_get_mapinfo(struct UCD_info * ui,coredump_phdr_t * phdrs,unsigned phdr_size)114 _UCD_get_mapinfo(struct UCD_info *ui, coredump_phdr_t *phdrs, unsigned phdr_size)
115 {
116 int ret = UNW_ESUCCESS; /* it's OK if there are no file mappings */
117
118 for (unsigned i = 0; i < phdr_size; ++i)
119 {
120 if (phdrs[i].p_type == PT_NOTE)
121 {
122 uint8_t *segment;
123 size_t segment_size;
124 ret = _UCD_elf_read_segment(ui, &phdrs[i], &segment, &segment_size);
125 if (ret == UNW_ESUCCESS)
126 {
127 _UCD_elf_visit_notes(segment, segment_size, _handle_file_note, ui);
128 free(segment);
129 }
130 }
131 }
132
133 return ret;
134 }
135