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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
|
/*
* fast ELF file accessor
* Copyright (C) 2018-2020 David Lamparter for NetDEF, Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* Note: this wrapper is intended to be used as build-time helper. While
* it should be generally correct and proper, there may be the occasional
* memory leak or SEGV for things that haven't been well-tested.
* _
* / \ This code is NOT SUITABLE FOR UNTRUSTED ELF FILES. It's used
* / ! \ in FRR to read files created by its own build. Don't take it out
* /_____\ of FRR and use it to parse random ELF files you found somewhere.
*
* If you're working with this code (or even reading it), you really need to
* read a bunch of the ELF specs. There's no way around it, things in here
* just represent pieces of ELF pretty much 1:1. Also, readelf & objdump are
* your friends.
*
* Required reading:
* https://refspecs.linuxfoundation.org/elf/elf.pdf
* https://refspecs.linuxfoundation.org/elf/x86_64-SysV-psABI.pdf
* Recommended reading:
* https://github.com/ARM-software/abi-aa/releases/download/2020Q4/aaelf64.pdf
*
* The core ELF spec is *not* enough, you should read at least one of the
* processor specific (psABI) docs. They define what & how relocations work.
* Luckily we don't need to care about the processor specifics since this only
* does data relocations, but without looking at the psABI, some things aren't
* quite clear.
*/
/* the API of this module roughly follows a very small subset of the one
* provided by the python elfutils package, which unfortunately is painfully
* slow.
*/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "structmember.h"
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#if defined(__sun__) && (__SIZEOF_POINTER__ == 4)
/* Solaris libelf bails otherwise ... */
#undef _FILE_OFFSET_BITS
#define _FILE_OFFSET_BITS 32
#endif
#include <elf.h>
#include <libelf.h>
#include <gelf.h>
#include "typesafe.h"
#include "jhash.h"
#include "clippy.h"
static bool debug;
#define debugf(...) \
do { \
if (debug) \
fprintf(stderr, __VA_ARGS__); \
} while (0)
/* Exceptions */
static PyObject *ELFFormatError;
static PyObject *ELFAccessError;
/* most objects can only be created as return values from one of the methods */
static PyObject *refuse_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyErr_SetString(PyExc_ValueError,
"cannot create instances of this type");
return NULL;
}
struct elfreloc;
struct elfsect;
PREDECL_HASH(elfrelocs);
/* ELFFile and ELFSection intentionally share some behaviour, particularly
* subscript[123:456] access to file data. This is because relocatables
* (.o files) do things section-based, but linked executables/libraries do
* things file-based. Having the two behave similar allows simplifying the
* Python code.
*/
/* class ELFFile:
*
* overall entry point, instantiated by reading in an ELF file
*/
struct elffile {
PyObject_HEAD
char *filename;
char *mmap, *mmend;
size_t len;
Elf *elf;
/* note from here on there are several instances of
*
* GElf_Something *x, _x;
*
* this is a pattern used by libelf's generic ELF routines; the _x
* field is used to create a copy of the ELF structure from the file
* with 32/64bit and endianness adjusted.
*/
GElf_Ehdr *ehdr, _ehdr;
Elf_Scn *symtab;
size_t nsym, symstridx;
Elf_Data *symdata;
PyObject **sects;
size_t n_sect;
struct elfrelocs_head dynrelocs;
int elfclass;
bool bigendian;
bool has_symbols;
};
/* class ELFSection:
*
* note that executables and shared libraries can have their section headers
* removed, though in practice this is only used as an obfuscation technique.
*/
struct elfsect {
PyObject_HEAD
const char *name;
struct elffile *ef;
GElf_Shdr _shdr, *shdr;
Elf_Scn *scn;
unsigned long idx, len;
struct elfrelocs_head relocs;
};
/* class ELFReloc:
*
* note: relocations in object files (.o) are section-based while relocations
* in executables and shared libraries are file-based.
*
* Whenever accessing something that is a pointer in the ELF file, the Python
* code needs to check for a relocation; if the pointer is pointing to some
* unresolved symbol the file will generally contain 0 bytes. The relocation
* will tell what the pointer is actually pointing to.
*
* This represents both static (.o file) and dynamic (.so/exec) relocations.
*/
struct elfreloc {
PyObject_HEAD
struct elfrelocs_item elfrelocs_item;
struct elfsect *es;
struct elffile *ef;
/* there's also old-fashioned GElf_Rel; we're converting that to
* GElf_Rela in elfsect_add_relocations()
*/
GElf_Rela _rela, *rela;
GElf_Sym _sym, *sym;
size_t symidx;
const char *symname;
/* documented below in python docstrings */
bool symvalid, unresolved, relative;
unsigned long long st_value;
};
static int elfreloc_cmp(const struct elfreloc *a, const struct elfreloc *b);
static uint32_t elfreloc_hash(const struct elfreloc *reloc);
DECLARE_HASH(elfrelocs, struct elfreloc, elfrelocs_item,
elfreloc_cmp, elfreloc_hash);
static Elf_Scn *elf_find_addr(struct elffile *ef, uint64_t addr, size_t *idx);
static PyObject *elffile_secbyidx(struct elffile *w, Elf_Scn *scn, size_t idx);
static PyObject *elfreloc_getsection(PyObject *self, PyObject *args);
static PyObject *elfreloc_getaddend(PyObject *obj, void *closure);
/* --- end of declarations -------------------------------------------------- */
/*
* class ELFReloc:
*/
static const char elfreloc_doc[] =
"Represents an ELF relocation record\n"
"\n"
"(struct elfreloc * in elf_py.c)";
#define member(name, type, doc) \
{ \
(char *)#name, type, offsetof(struct elfreloc, name), READONLY,\
(char *)doc "\n\n(\"" #name "\", " #type " in elf_py.c)" \
}
static PyMemberDef members_elfreloc[] = {
member(symname, T_STRING,
"Name of symbol this relocation refers to.\n"
"\n"
"Will frequently be `None` in executables and shared libraries."
),
member(symvalid, T_BOOL,
"Target symbol has a valid type, i.e. not STT_NOTYPE"),
member(unresolved, T_BOOL,
"Target symbol refers to an existing section"),
member(relative, T_BOOL,
"Relocation is a REL (not RELA) record and thus relative."),
member(st_value, T_ULONGLONG,
"Target symbol's value, if known\n\n"
"Will be zero for unresolved/external symbols."),
{}
};
#undef member
static PyGetSetDef getset_elfreloc[] = {
{ .name = (char *)"r_addend", .get = elfreloc_getaddend, .doc =
(char *)"Relocation addend value"},
{}
};
static PyMethodDef methods_elfreloc[] = {
{"getsection", elfreloc_getsection, METH_VARARGS,
"Find relocation target's ELF section\n\n"
"Args: address of relocatee (TODO: fix/remove?)\n"
"Returns: ELFSection or None\n\n"
"Not possible if section headers have been stripped."},
{}
};
static int elfreloc_cmp(const struct elfreloc *a, const struct elfreloc *b)
{
if (a->rela->r_offset < b->rela->r_offset)
return -1;
if (a->rela->r_offset > b->rela->r_offset)
return 1;
return 0;
}
static uint32_t elfreloc_hash(const struct elfreloc *reloc)
{
return jhash(&reloc->rela->r_offset, sizeof(reloc->rela->r_offset),
0xc9a2b7f4);
}
static struct elfreloc *elfrelocs_get(struct elfrelocs_head *head,
GElf_Addr offset)
{
struct elfreloc dummy;
dummy.rela = &dummy._rela;
dummy.rela->r_offset = offset;
return elfrelocs_find(head, &dummy);
}
static PyObject *elfreloc_getsection(PyObject *self, PyObject *args)
{
struct elfreloc *w = (struct elfreloc *)self;
long data;
if (!PyArg_ParseTuple(args, "k", &data))
return NULL;
if (!w->es)
Py_RETURN_NONE;
if (w->symidx == 0) {
size_t idx = 0;
Elf_Scn *scn;
data = (w->relative ? data : 0) + w->rela->r_addend;
scn = elf_find_addr(w->es->ef, data, &idx);
if (!scn)
Py_RETURN_NONE;
return elffile_secbyidx(w->es->ef, scn, idx);
}
return elffile_secbyidx(w->es->ef, NULL, w->sym->st_shndx);
}
static PyObject *elfreloc_getaddend(PyObject *obj, void *closure)
{
struct elfreloc *w = (struct elfreloc *)obj;
return Py_BuildValue("K", (unsigned long long)w->rela->r_addend);
}
static PyObject *elfreloc_repr(PyObject *arg)
{
struct elfreloc *w = (struct elfreloc *)arg;
return PyUnicode_FromFormat("<ELFReloc @%lu %s+%lu>",
(unsigned long)w->rela->r_offset,
(w->symname && w->symname[0]) ? w->symname
: "[0]",
(unsigned long)w->rela->r_addend);
}
static void elfreloc_free(void *arg)
{
struct elfreloc *w = arg;
(void)w;
}
static PyTypeObject typeobj_elfreloc = {
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "_clippy.ELFReloc",
.tp_basicsize = sizeof(struct elfreloc),
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = elfreloc_doc,
.tp_new = refuse_new,
.tp_free = elfreloc_free,
.tp_repr = elfreloc_repr,
.tp_members = members_elfreloc,
.tp_methods = methods_elfreloc,
.tp_getset = getset_elfreloc,
};
/*
* class ELFSection:
*/
static const char elfsect_doc[] =
"Represents an ELF section\n"
"\n"
"To access section contents, use subscript notation, e.g.\n"
" section[123:456]\n"
"To read null terminated C strings, replace the end with str:\n"
" section[123:str]\n\n"
"(struct elfsect * in elf_py.c)";
static PyObject *elfsect_getaddr(PyObject *self, void *closure);
#define member(name, type, doc) \
{ \
(char *)#name, type, offsetof(struct elfsect, name), READONLY, \
(char *)doc "\n\n(\"" #name "\", " #type " in elf_py.c)" \
}
static PyMemberDef members_elfsect[] = {
member(name, T_STRING,
"Section name, e.g. \".text\""),
member(idx, T_ULONG,
"Section index in file"),
member(len, T_ULONG,
"Section length in bytes"),
{},
};
#undef member
static PyGetSetDef getset_elfsect[] = {
{ .name = (char *)"sh_addr", .get = elfsect_getaddr, .doc =
(char *)"Section virtual address (mapped program view)"},
{}
};
static PyObject *elfsect_getaddr(PyObject *self, void *closure)
{
struct elfsect *w = (struct elfsect *)self;
return Py_BuildValue("K", (unsigned long long)w->shdr->sh_addr);
}
static PyObject *elfsect_getreloc(PyObject *self, PyObject *args)
{
struct elfsect *w = (struct elfsect *)self;
struct elfreloc *relw;
unsigned long offs;
PyObject *ret;
if (!PyArg_ParseTuple(args, "k", &offs))
return NULL;
relw = elfrelocs_get(&w->relocs, offs + w->shdr->sh_addr);
if (!relw)
Py_RETURN_NONE;
ret = (PyObject *)relw;
Py_INCREF(ret);
return ret;
}
static PyMethodDef methods_elfsect[] = {
{"getreloc", elfsect_getreloc, METH_VARARGS,
"Check for / get relocation at offset into section\n\n"
"Args: byte offset into section to check\n"
"Returns: ELFReloc or None"},
{}
};
static PyObject *elfsect_subscript(PyObject *self, PyObject *key)
{
Py_ssize_t start, stop, step, sllen;
struct elfsect *w = (struct elfsect *)self;
PySliceObject *slice;
unsigned long offs, len = ~0UL;
if (!PySlice_Check(key)) {
PyErr_SetString(PyExc_IndexError,
"ELFSection subscript must be slice");
return NULL;
}
slice = (PySliceObject *)key;
if (PyLong_Check(slice->stop)) {
if (PySlice_GetIndicesEx(key, w->shdr->sh_size,
&start, &stop, &step, &sllen))
return NULL;
if (step != 1) {
PyErr_SetString(PyExc_IndexError,
"ELFSection subscript slice step must be 1");
return NULL;
}
if ((GElf_Xword)stop > w->shdr->sh_size) {
PyErr_Format(ELFAccessError,
"access (%lu) beyond end of section %lu/%s (%lu)",
stop, w->idx, w->name, w->shdr->sh_size);
return NULL;
}
offs = start;
len = sllen;
} else {
if (slice->stop != (void *)&PyUnicode_Type
|| !PyLong_Check(slice->start)) {
PyErr_SetString(PyExc_IndexError, "invalid slice");
return NULL;
}
offs = PyLong_AsUnsignedLongLong(slice->start);
len = ~0UL;
}
offs += w->shdr->sh_offset;
if (offs > w->ef->len) {
PyErr_Format(ELFAccessError,
"access (%lu) beyond end of file (%lu)",
offs, w->ef->len);
return NULL;
}
if (len == ~0UL)
len = strnlen(w->ef->mmap + offs, w->ef->len - offs);
Py_ssize_t pylen = len;
#if PY_MAJOR_VERSION >= 3
return Py_BuildValue("y#", w->ef->mmap + offs, pylen);
#else
return Py_BuildValue("s#", w->ef->mmap + offs, pylen);
#endif
}
static PyMappingMethods mp_elfsect = {
.mp_subscript = elfsect_subscript,
};
static void elfsect_free(void *arg)
{
struct elfsect *w = arg;
(void)w;
}
static PyObject *elfsect_repr(PyObject *arg)
{
struct elfsect *w = (struct elfsect *)arg;
return PyUnicode_FromFormat("<ELFSection %s>", w->name);
}
static PyTypeObject typeobj_elfsect = {
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "_clippy.ELFSection",
.tp_basicsize = sizeof(struct elfsect),
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = elfsect_doc,
.tp_new = refuse_new,
.tp_free = elfsect_free,
.tp_repr = elfsect_repr,
.tp_as_mapping = &mp_elfsect,
.tp_members = members_elfsect,
.tp_methods = methods_elfsect,
.tp_getset = getset_elfsect,
};
static void elfsect_add_relocations(struct elfsect *w, Elf_Scn *rel,
GElf_Shdr *relhdr)
{
size_t i, entries;
Elf_Scn *symtab = elf_getscn(w->ef->elf, relhdr->sh_link);
GElf_Shdr _symhdr, *symhdr = gelf_getshdr(symtab, &_symhdr);
Elf_Data *symdata = elf_getdata(symtab, NULL);
Elf_Data *reldata = elf_getdata(rel, NULL);
entries = relhdr->sh_size / relhdr->sh_entsize;
for (i = 0; i < entries; i++) {
struct elfreloc *relw;
size_t symidx;
GElf_Rela *rela;
GElf_Sym *sym;
relw = (struct elfreloc *)typeobj_elfreloc.tp_alloc(
&typeobj_elfreloc, 0);
relw->es = w;
if (relhdr->sh_type == SHT_REL) {
GElf_Rel _rel, *rel;
rel = gelf_getrel(reldata, i, &_rel);
relw->rela = &relw->_rela;
relw->rela->r_offset = rel->r_offset;
relw->rela->r_info = rel->r_info;
relw->rela->r_addend = 0;
relw->relative = true;
} else
relw->rela = gelf_getrela(reldata, i, &relw->_rela);
rela = relw->rela;
if (rela->r_offset < w->shdr->sh_addr
|| rela->r_offset >= w->shdr->sh_addr + w->shdr->sh_size)
continue;
symidx = relw->symidx = GELF_R_SYM(rela->r_info);
sym = relw->sym = gelf_getsym(symdata, symidx, &relw->_sym);
if (sym) {
relw->symname = elf_strptr(w->ef->elf, symhdr->sh_link,
sym->st_name);
relw->symvalid = GELF_ST_TYPE(sym->st_info)
!= STT_NOTYPE;
relw->unresolved = sym->st_shndx == SHN_UNDEF;
relw->st_value = sym->st_value;
} else {
relw->symname = NULL;
relw->symvalid = false;
relw->unresolved = false;
relw->st_value = 0;
}
debugf("reloc @ %016llx sym %5llu %016llx %s\n",
(long long)rela->r_offset, (unsigned long long)symidx,
(long long)rela->r_addend, relw->symname);
elfrelocs_add(&w->relocs, relw);
}
}
/*
* bindings & loading code between ELFFile and ELFSection
*/
static PyObject *elfsect_wrap(struct elffile *ef, Elf_Scn *scn, size_t idx,
const char *name)
{
struct elfsect *w;
size_t i;
w = (struct elfsect *)typeobj_elfsect.tp_alloc(&typeobj_elfsect, 0);
if (!w)
return NULL;
w->name = name;
w->ef = ef;
w->scn = scn;
w->shdr = gelf_getshdr(scn, &w->_shdr);
w->len = w->shdr->sh_size;
w->idx = idx;
elfrelocs_init(&w->relocs);
for (i = 0; i < ef->ehdr->e_shnum; i++) {
Elf_Scn *scn = elf_getscn(ef->elf, i);
GElf_Shdr _shdr, *shdr = gelf_getshdr(scn, &_shdr);
if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL)
continue;
if (shdr->sh_info && shdr->sh_info != idx)
continue;
elfsect_add_relocations(w, scn, shdr);
}
return (PyObject *)w;
}
static Elf_Scn *elf_find_section(struct elffile *ef, const char *name,
size_t *idx)
{
size_t i;
const char *secname;
for (i = 0; i < ef->ehdr->e_shnum; i++) {
Elf_Scn *scn = elf_getscn(ef->elf, i);
GElf_Shdr _shdr, *shdr = gelf_getshdr(scn, &_shdr);
secname = elf_strptr(ef->elf, ef->ehdr->e_shstrndx,
shdr->sh_name);
if (strcmp(secname, name))
continue;
if (idx)
*idx = i;
return scn;
}
return NULL;
}
static Elf_Scn *elf_find_addr(struct elffile *ef, uint64_t addr, size_t *idx)
{
size_t i;
for (i = 0; i < ef->ehdr->e_shnum; i++) {
Elf_Scn *scn = elf_getscn(ef->elf, i);
GElf_Shdr _shdr, *shdr = gelf_getshdr(scn, &_shdr);
if (addr < shdr->sh_addr ||
addr >= shdr->sh_addr + shdr->sh_size)
continue;
if (idx)
*idx = i;
return scn;
}
return NULL;
}
/*
* class ELFFile:
*/
static const char elffile_doc[] =
"Represents an ELF file\n"
"\n"
"Args: filename to load\n"
"\n"
"To access raw file contents, use subscript notation, e.g.\n"
" file[123:456]\n"
"To read null terminated C strings, replace the end with str:\n"
" file[123:str]\n\n"
"(struct elffile * in elf_py.c)";
#define member(name, type, doc) \
{ \
(char *)#name, type, offsetof(struct elffile, name), READONLY, \
(char *)doc "\n\n(\"" #name "\", " #type " in elf_py.c)" \
}
static PyMemberDef members_elffile[] = {
member(filename, T_STRING,
"Original file name as given when opening"),
member(elfclass, T_INT,
"ELF class (architecture bit size)\n\n"
"Either 32 or 64, straight integer."),
member(bigendian, T_BOOL,
"ELF file is big-endian\n\n"
"All internal ELF structures are automatically converted."),
member(has_symbols, T_BOOL,
"A symbol section is present\n\n"
"Note: only refers to .symtab/SHT_SYMTAB section, not DT_SYMTAB"
),
{},
};
#undef member
static PyObject *elffile_secbyidx(struct elffile *w, Elf_Scn *scn, size_t idx)
{
const char *name;
PyObject *ret;
if (!scn)
scn = elf_getscn(w->elf, idx);
if (!scn || idx >= w->n_sect)
Py_RETURN_NONE;
if (!w->sects[idx]) {
GElf_Shdr _shdr, *shdr = gelf_getshdr(scn, &_shdr);
name = elf_strptr(w->elf, w->ehdr->e_shstrndx, shdr->sh_name);
w->sects[idx] = elfsect_wrap(w, scn, idx, name);
}
ret = w->sects[idx];
Py_INCREF(ret);
return ret;
}
static PyObject *elffile_get_section(PyObject *self, PyObject *args)
{
const char *name;
struct elffile *w = (struct elffile *)self;
Elf_Scn *scn;
size_t idx = 0;
if (!PyArg_ParseTuple(args, "s", &name))
return NULL;
scn = elf_find_section(w, name, &idx);
return elffile_secbyidx(w, scn, idx);
}
static PyObject *elffile_get_section_addr(PyObject *self, PyObject *args)
{
unsigned long long addr;
struct elffile *w = (struct elffile *)self;
Elf_Scn *scn;
size_t idx = 0;
if (!PyArg_ParseTuple(args, "K", &addr))
return NULL;
scn = elf_find_addr(w, addr, &idx);
return elffile_secbyidx(w, scn, idx);
}
static PyObject *elffile_get_section_idx(PyObject *self, PyObject *args)
{
unsigned long long idx;
struct elffile *w = (struct elffile *)self;
if (!PyArg_ParseTuple(args, "K", &idx))
return NULL;
return elffile_secbyidx(w, NULL, idx);
}
static PyObject *elffile_get_symbol(PyObject *self, PyObject *args)
{
const char *name, *symname;
struct elffile *w = (struct elffile *)self;
GElf_Sym _sym, *sym;
size_t i;
if (!PyArg_ParseTuple(args, "s", &name))
return NULL;
for (i = 0; i < w->nsym; i++) {
sym = gelf_getsym(w->symdata, i, &_sym);
if (sym->st_name == 0)
continue;
symname = elf_strptr(w->elf, w->symstridx, sym->st_name);
if (strcmp(symname, name))
continue;
PyObject *pysect;
Elf_Scn *scn = elf_getscn(w->elf, sym->st_shndx);
if (scn)
pysect = elffile_secbyidx(w, scn, sym->st_shndx);
else {
pysect = Py_None;
Py_INCREF(pysect);
}
return Py_BuildValue("sKN", symname,
(unsigned long long)sym->st_value, pysect);
}
Py_RETURN_NONE;
}
static PyObject *elffile_getreloc(PyObject *self, PyObject *args)
{
struct elffile *w = (struct elffile *)self;
struct elfreloc *relw;
unsigned long offs;
PyObject *ret;
if (!PyArg_ParseTuple(args, "k", &offs))
return NULL;
relw = elfrelocs_get(&w->dynrelocs, offs);
if (!relw)
Py_RETURN_NONE;
ret = (PyObject *)relw;
Py_INCREF(ret);
return ret;
}
static PyObject *elffile_find_note(PyObject *self, PyObject *args)
{
#if defined(HAVE_GELF_GETNOTE) && defined(HAVE_ELF_GETDATA_RAWCHUNK)
const char *owner;
const uint8_t *ids;
GElf_Word id;
struct elffile *w = (struct elffile *)self;
size_t i;
if (!PyArg_ParseTuple(args, "ss", &owner, &ids))
return NULL;
if (strlen((char *)ids) != 4) {
PyErr_SetString(PyExc_ValueError,
"ELF note ID must be exactly 4-byte string");
return NULL;
}
if (w->bigendian)
id = (ids[0] << 24) | (ids[1] << 16) | (ids[2] << 8) | ids[3];
else
id = (ids[3] << 24) | (ids[2] << 16) | (ids[1] << 8) | ids[0];
for (i = 0; i < w->ehdr->e_phnum; i++) {
GElf_Phdr _phdr, *phdr = gelf_getphdr(w->elf, i, &_phdr);
Elf_Data *notedata;
size_t offset;
if (phdr->p_type != PT_NOTE)
continue;
notedata = elf_getdata_rawchunk(w->elf, phdr->p_offset,
phdr->p_filesz, ELF_T_NHDR);
GElf_Nhdr nhdr[1];
size_t nameoffs, dataoffs;
offset = 0;
while ((offset = gelf_getnote(notedata, offset, nhdr,
&nameoffs, &dataoffs))) {
if (phdr->p_offset + nameoffs >= w->len)
continue;
const char *name = w->mmap + phdr->p_offset + nameoffs;
if (strcmp(name, owner))
continue;
if (id != nhdr->n_type)
continue;
PyObject *s, *e;
s = PyLong_FromUnsignedLongLong(
phdr->p_vaddr + dataoffs);
e = PyLong_FromUnsignedLongLong(
phdr->p_vaddr + dataoffs + nhdr->n_descsz);
return PySlice_New(s, e, NULL);
}
}
#endif
Py_RETURN_NONE;
}
static bool elffile_virt2file(struct elffile *w, GElf_Addr virt,
GElf_Addr *offs)
{
*offs = 0;
for (size_t i = 0; i < w->ehdr->e_phnum; i++) {
GElf_Phdr _phdr, *phdr = gelf_getphdr(w->elf, i, &_phdr);
if (phdr->p_type != PT_LOAD)
continue;
if (virt < phdr->p_vaddr
|| virt >= phdr->p_vaddr + phdr->p_memsz)
continue;
if (virt >= phdr->p_vaddr + phdr->p_filesz)
return false;
*offs = virt - phdr->p_vaddr + phdr->p_offset;
return true;
}
return false;
}
static PyObject *elffile_subscript(PyObject *self, PyObject *key)
{
Py_ssize_t start, stop, step;
PySliceObject *slice;
struct elffile *w = (struct elffile *)self;
bool str = false;
if (!PySlice_Check(key)) {
PyErr_SetString(PyExc_IndexError,
"ELFFile subscript must be slice");
return NULL;
}
slice = (PySliceObject *)key;
stop = -1;
step = 1;
if (PyLong_Check(slice->stop)) {
start = PyLong_AsSsize_t(slice->start);
if (PyErr_Occurred())
return NULL;
if (slice->stop != Py_None) {
stop = PyLong_AsSsize_t(slice->stop);
if (PyErr_Occurred())
return NULL;
}
if (slice->step != Py_None) {
step = PyLong_AsSsize_t(slice->step);
if (PyErr_Occurred())
return NULL;
}
} else {
if (slice->stop != (void *)&PyUnicode_Type
|| !PyLong_Check(slice->start)) {
PyErr_SetString(PyExc_IndexError, "invalid slice");
return NULL;
}
str = true;
start = PyLong_AsUnsignedLongLong(slice->start);
}
if (step != 1) {
PyErr_SetString(PyExc_IndexError,
"ELFFile subscript slice step must be 1");
return NULL;
}
GElf_Addr xstart = start, xstop = stop;
for (size_t i = 0; i < w->ehdr->e_phnum; i++) {
GElf_Phdr _phdr, *phdr = gelf_getphdr(w->elf, i, &_phdr);
if (phdr->p_type != PT_LOAD)
continue;
if (xstart < phdr->p_vaddr
|| xstart >= phdr->p_vaddr + phdr->p_memsz)
continue;
if (!str && (xstop < phdr->p_vaddr
|| xstop > phdr->p_vaddr + phdr->p_memsz)) {
PyErr_Format(ELFAccessError,
"access (%llu) beyond end of program header (%llu)",
(long long)xstop,
(long long)(phdr->p_vaddr +
phdr->p_memsz));
return NULL;
}
xstart = xstart - phdr->p_vaddr + phdr->p_offset;
if (str)
xstop = strlen(w->mmap + xstart);
else
xstop = xstop - phdr->p_vaddr + phdr->p_offset;
Py_ssize_t pylen = xstop - xstart;
#if PY_MAJOR_VERSION >= 3
return Py_BuildValue("y#", w->mmap + xstart, pylen);
#else
return Py_BuildValue("s#", w->mmap + xstart, pylen);
#endif
};
return PyErr_Format(ELFAccessError,
"virtual address (%llu) not found in program headers",
(long long)start);
}
static PyMethodDef methods_elffile[] = {
{"find_note", elffile_find_note, METH_VARARGS,
"find specific note entry"},
{"getreloc", elffile_getreloc, METH_VARARGS,
"find relocation"},
{"get_symbol", elffile_get_symbol, METH_VARARGS,
"find symbol by name"},
{"get_section", elffile_get_section, METH_VARARGS,
"find section by name"},
{"get_section_addr", elffile_get_section_addr, METH_VARARGS,
"find section by address"},
{"get_section_idx", elffile_get_section_idx, METH_VARARGS,
"find section by index"},
{}
};
static PyObject *elffile_load(PyTypeObject *type, PyObject *args,
PyObject *kwds);
static void elffile_free(void *arg)
{
struct elffile *w = arg;
elf_end(w->elf);
munmap(w->mmap, w->len);
free(w->filename);
}
static PyMappingMethods mp_elffile = {
.mp_subscript = elffile_subscript,
};
static PyTypeObject typeobj_elffile = {
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "_clippy.ELFFile",
.tp_basicsize = sizeof(struct elffile),
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = elffile_doc,
.tp_new = elffile_load,
.tp_free = elffile_free,
.tp_as_mapping = &mp_elffile,
.tp_members = members_elffile,
.tp_methods = methods_elffile,
};
static char *elfdata_strptr(Elf_Data *data, size_t offset)
{
char *p;
if (offset >= data->d_size)
return NULL;
p = (char *)data->d_buf + offset;
if (strnlen(p, data->d_size - offset) >= data->d_size - offset)
return NULL;
return p;
}
static void elffile_add_dynreloc(struct elffile *w, Elf_Data *reldata,
size_t entries, Elf_Data *symdata,
Elf_Data *strdata, Elf_Type typ)
{
size_t i;
for (i = 0; i < entries; i++) {
struct elfreloc *relw;
size_t symidx;
GElf_Rela *rela;
GElf_Sym *sym;
GElf_Addr rel_offs = 0;
relw = (struct elfreloc *)typeobj_elfreloc.tp_alloc(
&typeobj_elfreloc, 0);
relw->ef = w;
if (typ == ELF_T_REL) {
GElf_Rel _rel, *rel;
GElf_Addr offs;
rel = gelf_getrel(reldata, i, &_rel);
relw->rela = &relw->_rela;
relw->rela->r_offset = rel->r_offset;
relw->rela->r_info = rel->r_info;
relw->rela->r_addend = 0;
relw->relative = true;
/* REL uses the pointer contents itself instead of the
* RELA addend field :( ... theoretically this could
* be some weird platform specific encoding, but since
* we only care about data relocations it should
* always be a pointer...
*/
if (elffile_virt2file(w, rel->r_offset, &offs)) {
Elf_Data *ptr, *conv;
GElf_Addr tmp;
Elf_Data mem = {
.d_buf = (void *)&tmp,
.d_type = ELF_T_ADDR,
.d_version = EV_CURRENT,
.d_size = sizeof(tmp),
.d_off = 0,
.d_align = 0,
};
ptr = elf_getdata_rawchunk(w->elf, offs,
w->elfclass / 8,
ELF_T_ADDR);
conv = gelf_xlatetom(w->elf, &mem, ptr,
w->mmap[EI_DATA]);
if (conv) {
memcpy(&rel_offs, conv->d_buf,
conv->d_size);
relw->relative = false;
relw->rela->r_addend = rel_offs;
}
}
} else
relw->rela = gelf_getrela(reldata, i, &relw->_rela);
rela = relw->rela;
symidx = relw->symidx = GELF_R_SYM(rela->r_info);
sym = relw->sym = gelf_getsym(symdata, symidx, &relw->_sym);
if (sym) {
relw->symname = elfdata_strptr(strdata, sym->st_name);
relw->symvalid = GELF_ST_TYPE(sym->st_info)
!= STT_NOTYPE;
relw->unresolved = sym->st_shndx == SHN_UNDEF;
relw->st_value = sym->st_value;
} else {
relw->symname = NULL;
relw->symvalid = false;
relw->unresolved = false;
relw->st_value = 0;
}
if (typ == ELF_T_RELA)
debugf("dynrela @ %016llx sym %5llu %016llx %s\n",
(long long)rela->r_offset,
(unsigned long long)symidx,
(long long)rela->r_addend, relw->symname);
else
debugf("dynrel @ %016llx sym %5llu (%016llx) %s\n",
(long long)rela->r_offset,
(unsigned long long)symidx,
(unsigned long long)rel_offs, relw->symname);
elfrelocs_add(&w->dynrelocs, relw);
}
}
/* primary (only, really) entry point to anything in this module */
static PyObject *elffile_load(PyTypeObject *type, PyObject *args,
PyObject *kwds)
{
const char *filename;
static const char * const kwnames[] = {"filename", NULL};
struct elffile *w;
struct stat st;
int fd, err;
w = (struct elffile *)typeobj_elffile.tp_alloc(&typeobj_elffile, 0);
if (!w)
return NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", (char **)kwnames,
&filename))
return NULL;
w->filename = strdup(filename);
fd = open(filename, O_RDONLY | O_NOCTTY);
if (fd < 0 || fstat(fd, &st)) {
PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
close(fd);
goto out;
}
w->len = st.st_size;
w->mmap = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (!w->mmap) {
PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
close(fd);
goto out;
}
close(fd);
w->mmend = w->mmap + st.st_size;
if (w->len < EI_NIDENT || memcmp(w->mmap, ELFMAG, SELFMAG)) {
PyErr_SetString(ELFFormatError, "invalid ELF signature");
goto out;
}
switch (w->mmap[EI_CLASS]) {
case ELFCLASS32:
w->elfclass = 32;
break;
case ELFCLASS64:
w->elfclass = 64;
break;
default:
PyErr_SetString(ELFFormatError, "invalid ELF class");
goto out;
}
switch (w->mmap[EI_DATA]) {
case ELFDATA2LSB:
w->bigendian = false;
break;
case ELFDATA2MSB:
w->bigendian = true;
break;
default:
PyErr_SetString(ELFFormatError, "invalid ELF byte order");
goto out;
}
w->elf = elf_memory(w->mmap, w->len);
if (!w->elf)
goto out_elferr;
w->ehdr = gelf_getehdr(w->elf, &w->_ehdr);
if (!w->ehdr)
goto out_elferr;
for (size_t i = 0; i < w->ehdr->e_shnum; i++) {
Elf_Scn *scn = elf_getscn(w->elf, i);
GElf_Shdr _shdr, *shdr = gelf_getshdr(scn, &_shdr);
if (shdr->sh_type == SHT_SYMTAB) {
w->symtab = scn;
w->nsym = shdr->sh_size / shdr->sh_entsize;
w->symdata = elf_getdata(scn, NULL);
w->symstridx = shdr->sh_link;
break;
}
}
w->has_symbols = w->symtab && w->symstridx;
elfrelocs_init(&w->dynrelocs);
#ifdef HAVE_ELF_GETDATA_RAWCHUNK
for (size_t i = 0; i < w->ehdr->e_phnum; i++) {
GElf_Phdr _phdr, *phdr = gelf_getphdr(w->elf, i, &_phdr);
if (phdr->p_type != PT_DYNAMIC)
continue;
Elf_Data *dyndata = elf_getdata_rawchunk(w->elf,
phdr->p_offset, phdr->p_filesz, ELF_T_DYN);
GElf_Addr dynrela = 0, dynrel = 0, symtab = 0, strtab = 0;
size_t dynrelasz = 0, dynrelaent = 0;
size_t dynrelsz = 0, dynrelent = 0;
size_t strsz = 0;
GElf_Dyn _dyn, *dyn;
for (size_t j = 0;; j++) {
dyn = gelf_getdyn(dyndata, j, &_dyn);
if (dyn->d_tag == DT_NULL)
break;
switch (dyn->d_tag) {
case DT_SYMTAB:
symtab = dyn->d_un.d_ptr;
break;
case DT_STRTAB:
strtab = dyn->d_un.d_ptr;
break;
case DT_STRSZ:
strsz = dyn->d_un.d_val;
break;
case DT_RELA:
dynrela = dyn->d_un.d_ptr;
break;
case DT_RELASZ:
dynrelasz = dyn->d_un.d_val;
break;
case DT_RELAENT:
dynrelaent = dyn->d_un.d_val;
break;
case DT_REL:
dynrel = dyn->d_un.d_ptr;
break;
case DT_RELSZ:
dynrelsz = dyn->d_un.d_val;
break;
case DT_RELENT:
dynrelent = dyn->d_un.d_val;
break;
}
}
GElf_Addr offset;
Elf_Data *symdata = NULL, *strdata = NULL;
if (elffile_virt2file(w, symtab, &offset))
symdata = elf_getdata_rawchunk(w->elf, offset,
w->len - offset,
ELF_T_SYM);
if (elffile_virt2file(w, strtab, &offset))
strdata = elf_getdata_rawchunk(w->elf, offset,
strsz, ELF_T_BYTE);
size_t c;
if (dynrela && dynrelasz && dynrelaent
&& elffile_virt2file(w, dynrela, &offset)) {
Elf_Data *reladata = NULL;
debugf("dynrela @%llx/%llx+%llx\n", (long long)dynrela,
(long long)offset, (long long)dynrelasz);
reladata = elf_getdata_rawchunk(w->elf, offset,
dynrelasz, ELF_T_RELA);
c = dynrelasz / dynrelaent;
elffile_add_dynreloc(w, reladata, c, symdata, strdata,
ELF_T_RELA);
}
if (dynrel && dynrelsz && dynrelent
&& elffile_virt2file(w, dynrel, &offset)) {
Elf_Data *reldata = NULL;
debugf("dynrel @%llx/%llx+%llx\n", (long long)dynrel,
(long long)offset, (long long)dynrelsz);
reldata = elf_getdata_rawchunk(w->elf, offset, dynrelsz,
ELF_T_REL);
c = dynrelsz / dynrelent;
elffile_add_dynreloc(w, reldata, c, symdata, strdata,
ELF_T_REL);
}
}
#endif
w->sects = calloc(sizeof(PyObject *), w->ehdr->e_shnum);
w->n_sect = w->ehdr->e_shnum;
return (PyObject *)w;
out_elferr:
err = elf_errno();
PyErr_Format(ELFFormatError, "libelf error %d: %s",
err, elf_errmsg(err));
out:
if (w->elf)
elf_end(w->elf);
free(w->filename);
return NULL;
}
static PyObject *elfpy_debug(PyObject *self, PyObject *args)
{
int arg;
if (!PyArg_ParseTuple(args, "p", &arg))
return NULL;
debug = arg;
Py_RETURN_NONE;
}
static PyMethodDef methods_elfpy[] = {
{"elfpy_debug", elfpy_debug, METH_VARARGS, "switch debuging on/off"},
{}
};
bool elf_py_init(PyObject *pymod)
{
if (PyType_Ready(&typeobj_elffile) < 0)
return false;
if (PyType_Ready(&typeobj_elfsect) < 0)
return false;
if (PyType_Ready(&typeobj_elfreloc) < 0)
return false;
if (elf_version(EV_CURRENT) == EV_NONE)
return false;
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 5
PyModule_AddFunctions(pymod, methods_elfpy);
#else
(void)methods_elfpy;
#endif
ELFFormatError = PyErr_NewException("_clippy.ELFFormatError",
PyExc_ValueError, NULL);
PyModule_AddObject(pymod, "ELFFormatError", ELFFormatError);
ELFAccessError = PyErr_NewException("_clippy.ELFAccessError",
PyExc_IndexError, NULL);
PyModule_AddObject(pymod, "ELFAccessError", ELFAccessError);
Py_INCREF(&typeobj_elffile);
PyModule_AddObject(pymod, "ELFFile", (PyObject *)&typeobj_elffile);
Py_INCREF(&typeobj_elfsect);
PyModule_AddObject(pymod, "ELFSection", (PyObject *)&typeobj_elfsect);
Py_INCREF(&typeobj_elfreloc);
PyModule_AddObject(pymod, "ELFReloc", (PyObject *)&typeobj_elfreloc);
return true;
}
|