-
Notifications
You must be signed in to change notification settings - Fork 0
/
elftoolchain.c
2907 lines (2419 loc) · 55.2 KB
/
elftoolchain.c
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
/*
* MIT License
*
* Copyright (c) 2018 XMM SWAP LTD
*/
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <lua.h>
#include <lauxlib.h>
#include <libelf.h>
#include <gelf.h>
#define MT_PREFIX "elftoolchain."
#define ELF_MT MT_PREFIX "Elf"
#define ELF_ARHDR_MT MT_PREFIX "Elf_Arhdr"
#define ELF_SCN_MT MT_PREFIX "Elf_Scn"
#define ELF_DATA_MT MT_PREFIX "Elf_Data"
#define ELF_CAP_MT MT_PREFIX "GElf_Cap"
#define ELF_DYN_MT MT_PREFIX "GElf_Dyn"
#define ELF_EHDR_MT MT_PREFIX "GElf_Ehdr"
#define ELF_MOVE_MT MT_PREFIX "GElf_Move"
#define ELF_PHDR_MT MT_PREFIX "GElf_Phdr"
#define ELF_RELA_MT MT_PREFIX "GElf_Rela"
#define ELF_REL_MT MT_PREFIX "GElf_Rel"
#define ELF_SHDR_MT MT_PREFIX "GElf_Shdr"
#define ELF_SYMINFO_MT MT_PREFIX "GElf_Syminfo"
#define ELF_SYM_MT MT_PREFIX "GElf_Sym"
/* XXX
* #define ELF_ARSYM_MT MT_PREFIX "Elf_Arsym"
*
* Elf32_Lib Elf32_Nhdr Elf32_RegInfo Elf32_Section
* Elf32_Verdaux Elf32_Verdef Elf32_Vernaux Elf32_Verneed Elf32_Versym
*
* Convert flags to Lua.
* Implement updates: __newindex, gelf_update_xxx() etc.
*
*/
struct udataElf {
Elf *elf;
void *mem; /* XXX implement */
int fd;
};
struct udataElfArhdr {
Elf_Arhdr *arhdr;
};
struct udataElfScn {
Elf_Scn *scn;
};
struct udataElfData {
Elf_Data *data;
Elf_Scn *scn;
};
struct KV {
lua_Integer key;
const char *val;
};
/*
* All public fields of Elf_Arhdr struct.
*/
static const char *arhdr_fields[] = {
"gid",
"uid",
"date",
"mode",
"name",
"size",
"rawname",
/* ar_flags is is not part of public API. */
NULL
};
/*
* All public fields of GElf_Ehdr struct.
*/
static const char *ehdr_fields[] = {
"type",
"class",
"entry",
"flags",
"ident",
"phnum",
"phoff",
"shnum",
"shoff",
"ehsize",
"machine",
"version",
"shstrndx",
"phentsize",
"shentsize",
NULL
};
/*
* All public fields of GElf_Move struct.
*/
static const char *move_fields[] = {
"info",
"value",
"repeat",
"stride",
"poffset",
NULL
};
/*
* All public fields of GElf_Phdr struct.
*/
static const char *phdr_fields[] = {
"type",
"align",
"flags",
"memsz",
"paddr",
"vaddr",
"filesz",
"offset",
NULL
};
/*
* All public fields of GElf_Shdr struct.
*/
static const char *shdr_fields[] = {
"addr",
"info",
"link",
"name",
"size",
"type",
"flags",
"offset",
"entsize",
"addralign",
NULL
};
/*
* All public fields of GElf_Cap struct.
*/
static const char *cap_fields[] = {
"ptr",
"tag",
"val",
NULL
};
/*
* All public fields of GElf_Dyn struct.
*/
static const char *dyn_fields[] = {
"ptr",
"tag",
"val",
NULL
};
/*
* All public fields of GElf_Rela struct.
*/
static const char *rela_fields[] = {
"sym",
"type",
"offset",
"addend",
NULL
};
/*
* All public fields of GElf_Rel struct.
*/
static const char *rel_fields[] = {
"sym",
"type",
"offset",
NULL
};
/*
* All public fields of GElf_Syminfo struct.
*/
static const char *syminfo_fields[] = {
"flags",
"boundto",
NULL
};
/*
* All public fields of GElf_Sym struct.
*/
static const char *sym_fields[] = {
"bind", "type", /* info - type / binding attrs */
"name",
"size",
"other",
"shndx",
"value",
"visibility",
NULL
};
/*
* XXX
* ET_LOOS-ET_HIOS Operating system specific range
* ET_LOPROC-ET_HIPROC Processor-specific range
*/
/* e_type values. */
static const struct KV et_constants[] = {
{ ET_NONE, "NONE" },
{ ET_REL, "REL" },
{ ET_EXEC, "EXEC" },
{ ET_DYN, "DYN" },
{ ET_CORE, "CORE" },
{ 0, NULL }
};
/* e_machine values. */
static const struct KV em_constants[] = {
{ EM_NONE, "NONE" },
{ EM_M32, "M32" },
{ EM_SPARC, "SPARC" },
{ EM_386, "386" },
{ EM_68K, "68K" },
{ EM_88K, "88K" },
{ EM_486, "486" },
{ EM_IAMCU, "IAMCU" },
{ EM_860, "860" },
{ EM_MIPS, "MIPS" },
{ EM_S370, "S370" },
{ EM_MIPS_RS3_LE, "MIPS_RS3_LE" },
{ EM_RS6000, "RS6000" },
{ EM_PARISC, "PARISC" },
{ EM_NCUBE, "NCUBE" },
{ EM_VPP500, "VPP500" },
{ EM_SPARC32PLUS, "SPARC32PLUS" },
{ EM_960, "960" },
{ EM_PPC, "PPC" },
{ EM_PPC64, "PPC64" },
{ EM_S390, "S390" },
{ EM_V800, "V800" },
{ EM_FR20, "FR20" },
{ EM_RH32, "RH32" },
{ EM_RCE, "RCE" },
{ EM_ARM, "ARM" },
{ EM_ALPHA, "ALPHA" },
{ EM_SH, "SH" },
{ EM_SPARCV9, "SPARCV9" },
{ EM_TRICORE, "TRICORE" },
{ EM_ARC, "ARC" },
{ EM_H8_300, "H8_300" },
{ EM_H8_300H, "H8_300H" },
{ EM_H8S, "H8S" },
{ EM_H8_500, "H8_500" },
{ EM_IA_64, "IA_64" },
{ EM_MIPS_X, "MIPS_X" },
{ EM_COLDFIRE, "COLDFIRE" },
{ EM_68HC12, "68HC12" },
{ EM_MMA, "MMA" },
{ EM_PCP, "PCP" },
{ EM_NCPU, "NCPU" },
{ EM_NDR1, "NDR1" },
{ EM_STARCORE, "STARCORE" },
{ EM_ME16, "ME16" },
{ EM_ST100, "ST100" },
{ EM_TINYJ, "TINYJ" },
{ EM_X86_64, "X86_64" },
{ EM_PDSP, "PDSP" },
{ EM_PDP10, "PDP10" },
{ EM_PDP11, "PDP11" },
{ EM_FX66, "FX66" },
{ EM_ST9PLUS, "ST9PLUS" },
{ EM_ST7, "ST7" },
{ EM_68HC16, "68HC16" },
{ EM_68HC11, "68HC11" },
{ EM_68HC08, "68HC08" },
{ EM_68HC05, "68HC05" },
{ EM_SVX, "SVX" },
{ EM_ST19, "ST19" },
{ EM_VAX, "VAX" },
{ EM_CRIS, "CRIS" },
{ EM_JAVELIN, "JAVELIN" },
{ EM_FIREPATH, "FIREPATH" },
{ EM_ZSP, "ZSP" },
{ EM_MMIX, "MMIX" },
{ EM_HUANY, "HUANY" },
{ EM_PRISM, "PRISM" },
{ EM_AVR, "AVR" },
{ EM_FR30, "FR30" },
{ EM_D10V, "D10V" },
{ EM_D30V, "D30V" },
{ EM_V850, "V850" },
{ EM_M32R, "M32R" },
{ EM_MN10300, "MN10300" },
{ EM_MN10200, "MN10200" },
{ EM_PJ, "PJ" },
{ EM_OR1K, "OR1K" },
{ EM_OPENRISC, "OPENRISC" },
{ EM_ARC_A5, "ARC_A5" },
{ EM_XTENSA, "XTENSA" },
{ EM_VIDEOCORE, "VIDEOCORE" },
{ EM_TMM_GPP, "TMM_GPP" },
{ EM_NS32K, "NS32K" },
{ EM_TPC, "TPC" },
{ EM_SNP1K, "SNP1K" },
{ EM_ST200, "ST200" },
{ EM_IP2K, "IP2K" },
{ EM_MAX, "MAX" },
{ EM_CR, "CR" },
{ EM_F2MC16, "F2MC16" },
{ EM_MSP430, "MSP430" },
{ EM_BLACKFIN, "BLACKFIN" },
{ EM_SE_C33, "SE_C33" },
{ EM_SEP, "SEP" },
{ EM_ARCA, "ARCA" },
{ EM_UNICORE, "UNICORE" },
{ EM_ALTERA_NIOS2, "ALTERA_NIOS2" },
{ EM_AARCH64, "AARCH64" },
{ EM_AVR32, "AVR32" },
{ EM_TILE64, "TILE64" },
{ EM_TILEPRO, "TILEPRO" },
{ EM_MICROBLAZE, "MICROBLAZE" },
{ EM_TILEGX, "TILEGX" },
{ EM_Z80, "Z80" },
{ EM_RISCV, "RISCV" },
{ EM_ALPHA_EXP, "ALPHA_EXP" },
{ 0, NULL }
};
/*
* XXX
* DT_LOOS-DT_HIOS Operating system specific range
* DT_LOPROC-DT_HIPROC Processor-specific range
*/
/* d_tag */
static const struct KV dt_constants[] = {
{ DT_NULL, "NULL" },
{ DT_NEEDED, "NEEDED" },
{ DT_PLTRELSZ, "PLTRELSZ" },
{ DT_PLTGOT, "PLTGOT" },
{ DT_HASH, "HASH" },
{ DT_STRTAB, "STRTAB" },
{ DT_SYMTAB, "SYMTAB" },
{ DT_RELA, "RELA" },
{ DT_RELASZ, "RELASZ" },
{ DT_RELAENT, "RELAENT" },
{ DT_STRSZ, "STRSZ" },
{ DT_SYMENT, "SYMENT" },
{ DT_INIT, "INIT" },
{ DT_FINI, "FINI" },
{ DT_SONAME, "SONAME" },
{ DT_RPATH, "RPATH" },
{ DT_SYMBOLIC, "SYMBOLIC" },
{ DT_REL, "REL" },
{ DT_RELSZ, "RELSZ" },
{ DT_RELENT, "RELENT" },
{ DT_PLTREL, "PLTREL" },
{ DT_DEBUG, "DEBUG" },
{ DT_TEXTREL, "TEXTREL" },
{ DT_JMPREL, "JMPREL" },
{ DT_BIND_NOW, "BIND_NOW" },
{ DT_INIT_ARRAY, "INIT_ARRAY" },
{ DT_FINI_ARRAY, "FINI_ARRAY" },
{ DT_INIT_ARRAYSZ, "INIT_ARRAYSZ" },
{ DT_FINI_ARRAYSZ, "FINI_ARRAYSZ" },
{ DT_RUNPATH, "RUNPATH" },
{ DT_FLAGS, "FLAGS" },
{ DT_ENCODING, "ENCODING" },
{ DT_PREINIT_ARRAY, "PREINIT_ARRAY" },
{ DT_PREINIT_ARRAYSZ, "PREINIT_ARRAYSZ" },
{ DT_VERSYM, "VERSYM" },
{ DT_FLAGS_1, "FLAGS_1" },
{ DT_VERDEF, "VERDEF" },
{ DT_VERDEFNUM, "VERDEFNUM" },
{ DT_VERNEED, "VERNEED" },
{ DT_VERNEEDNUM, "VERNEEDNUM" },
{ 0, NULL }
};
/*
* XXX
* PT_LOOS-PT_HIOS Operating system specific range
* PT_LOPROC-PT_HIPROC Processor-specific range
*/
/* p_type values. */
static const struct KV pt_constants[] = {
{ PT_NULL, "NULL" },
{ PT_LOAD, "LOAD" },
{ PT_DYNAMIC, "DYNAMIC" },
{ PT_INTERP, "INTERP" },
{ PT_NOTE, "NOTE" },
{ PT_SHLIB, "SHLIB" },
{ PT_PHDR, "PHDR" },
{ PT_TLS, "TLS" },
{ PT_GNU_EH_FRAME, "GNU_EH_FRAME" },
{ PT_GNU_STACK, "GNU_STACK" },
{ PT_GNU_RELRO, "GNU_RELRO" },
{ PT_MIPS_REGINFO, "MIPS_REGINFO" },
{ PT_MIPS_ABIFLAGS, "MIPS_ABIFLAGS" },
{ 0, NULL }
};
/*
* XXX
* SHT_LOOS-SHT_HIOS Operating system specific range
* SHT_LOPROC-SHT_HIPROC Processor-specific range
* SHT_LOSUNW-SHT_HISUNW
*/
/* sh_type values. */
static const struct KV sht_constants[] = {
{ SHT_NULL, "NULL" },
{ SHT_PROGBITS, "PROGBITS" },
{ SHT_SYMTAB, "SYMTAB" },
{ SHT_STRTAB, "STRTAB" },
{ SHT_RELA, "RELA" },
{ SHT_HASH, "HASH" },
{ SHT_DYNAMIC, "DYNAMIC" },
{ SHT_NOTE, "NOTE" },
{ SHT_NOBITS, "NOBITS" },
{ SHT_REL, "REL" },
{ SHT_SHLIB, "SHLIB" },
{ SHT_DYNSYM, "DYNSYM" },
{ SHT_INIT_ARRAY, "INIT_ARRAY" },
{ SHT_FINI_ARRAY, "FINI_ARRAY" },
{ SHT_PREINIT_ARRAY, "PREINIT_ARRAY" },
{ SHT_GROUP, "GROUP" },
{ SHT_SYMTAB_SHNDX, "SYMTAB_SHNDX" },
{ SHT_GNU_INCREMENTAL_INPUTS, "GNU_INCREMENTAL_INPUTS" },
{ SHT_SUNW_dof, "SUNW_dof" },
{ SHT_GNU_ATTRIBUTES, "GNU_ATTRIBUTES" },
{ SHT_SUNW_cap, "SUNW_cap" },
{ SHT_SUNW_SIGNATURE, "SUNW_SIGNATURE" },
{ SHT_GNU_HASH, "GNU_HASH" },
{ SHT_GNU_LIBLIST, "GNU_LIBLIST" },
{ SHT_SUNW_move, "SUNW_move" },
{ SHT_SUNW_COMDAT, "SUNW_COMDAT" },
{ SHT_SUNW_syminfo, "SUNW_syminfo" },
{ SHT_SUNW_verdef, "SUNW_verdef" },
{ SHT_GNU_verdef, "GNU_verdef" },
{ SHT_SUNW_verneed, "SUNW_verneed" },
{ SHT_GNU_verneed, "GNU_verneed" },
{ SHT_SUNW_versym, "SUNW_versym" },
{ SHT_GNU_versym, "GNU_versym" },
{ SHT_AMD64_UNWIND, "AMD64_UNWIND" },
{ SHT_ARM_EXIDX, "ARM_EXIDX" },
{ SHT_ARM_PREEMPTMAP, "ARM_PREEMPTMAP" },
{ SHT_ARM_ATTRIBUTES, "ARM_ATTRIBUTES" },
{ SHT_ARM_DEBUGOVERLAY, "ARM_DEBUGOVERLAY" },
{ SHT_ARM_OVERLAYSECTION, "ARM_OVERLAYSECTION" },
{ SHT_MIPS_REGINFO, "MIPS_REGINFO" },
{ SHT_MIPS_OPTIONS, "MIPS_OPTIONS" },
{ SHT_MIPS_DWARF, "MIPS_DWARF" },
{ 0, NULL }
};
/*
* XXX
* STB_LOOS-STB_HIOS Operating system specific range
* STB_LOPROC-STB_HIPROC Processor-specific range
*/
/* st_info: Symbol Bindings */
static const struct KV stb_constants[] = {
{ STB_LOCAL, "LOCAL" },
{ STB_GLOBAL, "GLOBAL" },
{ STB_WEAK, "WEAK" },
{ 0, NULL }
};
/*
* XXX
* STT_LOOS-STT_HIOS Operating system specific range
* STT_LOPROC-STT_HIPROC Processor-specific range
*/
/* st_info values. */
static const struct KV stt_constants[] = {
{ STT_NOTYPE, "NOTYPE" },
{ STT_OBJECT, "OBJECT" },
{ STT_FUNC, "FUNC" },
{ STT_SECTION, "SECTION" },
{ STT_FILE, "FILE" },
{ STT_COMMON, "COMMON" },
{ STT_TLS, "TLS" },
{ STT_GNU_IFUNC, "GNU_IFUNC" },
{ 0, NULL }
};
/* st_other: Visibility Types */
static const struct KV stv_constants[] = {
{ STV_DEFAULT, "DEFAULT" },
{ STV_INTERNAL, "INTERNAL" },
{ STV_HIDDEN, "HIDDEN" },
{ STV_PROTECTED, "PROTECTED" },
{ STV_EXPORTED, "EXPORTED" },
{ STV_SINGLETON, "SINGLETON" },
{ STV_ELIMINATE, "ELIMINATE" },
{ 0, NULL }
};
static int
l_next_field(lua_State *L)
{
/* Make sure there is a slot for lua_next() to pop. */
lua_settop(L, 2);
if (lua_next(L, 1) == 0)
return 0;
lua_pop(L, 1);
return 1;
}
static const struct KV *
find_constant(const char *str, const struct KV constants[])
{
size_t i;
for (i = 0; constants[i].val != NULL; i++) {
if (strcmp(str, constants[i].val) == 0)
return &constants[i];
}
return NULL;
}
/*
* [-0, +2-1, -]
* Push a string constant if key is found in constants, push key otherwise.
*/
static void
push_constant(lua_State *L, lua_Integer key, const struct KV constants[])
{
int newtop;
newtop = lua_gettop(L) + 1;
lua_rawgetp(L, LUA_REGISTRYINDEX, constants);
if (lua_rawgeti(L, -1, key) == LUA_TNIL)
lua_pushinteger(L, key);
lua_replace(L, newtop);
lua_settop(L, newtop);
}
static int
error_closed_elf(lua_State *L, int arg)
{
const char *errmsg = "Elf object is closed";
if (arg < 0)
return luaL_error(L, errmsg);
else
return luaL_argerror(L, arg, errmsg);
}
static struct udataElf *
check_elf_udata(lua_State *L, int arg, int err_arg)
{
struct udataElf *ud;
ud = luaL_checkudata(L, arg, ELF_MT);
if (ud->elf == NULL)
error_closed_elf(L, err_arg);
return ud;
}
static struct udataElfArhdr *
check_elf_arhdr_udata(lua_State *L, int arg)
{
struct udataElfArhdr *ud;
ud = luaL_checkudata(L, arg, ELF_ARHDR_MT);
assert(ud->arhdr != NULL);
return ud;
}
static struct udataElfScn *
test_elf_scn_udata(lua_State *L, int arg)
{
struct udataElfScn *ud;
ud = luaL_testudata(L, arg, ELF_SCN_MT);
assert(ud == NULL || ud->scn != NULL);
return ud;
}
static struct udataElfScn *
check_elf_scn_udata(lua_State *L, int arg, int err_arg)
{
struct udataElfScn *ud;
ud = luaL_checkudata(L, arg, ELF_SCN_MT);
assert(ud->scn != NULL);
return ud;
}
static struct udataElfData *
test_elf_data_udata(lua_State *L, int arg)
{
return luaL_testudata(L, arg, ELF_DATA_MT);
}
static struct udataElfData *
check_elf_data_udata(lua_State *L, int arg, bool push_uservalue)
{
struct udataElfData *ud;
ud = luaL_checkudata(L, arg, ELF_DATA_MT);
if (ud == NULL)
return ud;
lua_getuservalue(L, arg);
check_elf_udata(L, -1, -1);
if (!push_uservalue)
lua_pop(L, 1);
return ud;
}
static GElf_Ehdr *
push_gelf_ehdr_udata(lua_State *L)
{
GElf_Ehdr *ud;
ud = lua_newuserdata(L, sizeof(*ud));
memset(ud, 0, sizeof(*ud));
luaL_getmetatable(L, ELF_EHDR_MT);
lua_setmetatable(L, -2);
return ud;
}
static GElf_Ehdr *
check_gelf_ehdr_udata(lua_State *L, int arg)
{
return luaL_checkudata(L, arg, ELF_EHDR_MT);
}
static GElf_Move *
push_gelf_move_udata(lua_State *L)
{
GElf_Move *ud;
ud = lua_newuserdata(L, sizeof(*ud));
memset(ud, 0, sizeof(*ud));
luaL_getmetatable(L, ELF_MOVE_MT);
lua_setmetatable(L, -2);
return ud;
}
static GElf_Move *
check_gelf_move_udata(lua_State *L, int arg)
{
return luaL_checkudata(L, arg, ELF_MOVE_MT);
}
static GElf_Phdr *
push_gelf_phdr_udata(lua_State *L)
{
GElf_Phdr *ud;
ud = lua_newuserdata(L, sizeof(*ud));
memset(ud, 0, sizeof(*ud));
luaL_getmetatable(L, ELF_PHDR_MT);
lua_setmetatable(L, -2);
return ud;
}
static GElf_Phdr *
check_gelf_phdr_udata(lua_State *L, int arg)
{
return luaL_checkudata(L, arg, ELF_PHDR_MT);
}
static GElf_Shdr *
push_gelf_shdr_udata(lua_State *L)
{
GElf_Shdr *ud;
ud = lua_newuserdata(L, sizeof(*ud));
memset(ud, 0, sizeof(*ud));
luaL_getmetatable(L, ELF_SHDR_MT);
lua_setmetatable(L, -2);
return ud;
}
static GElf_Shdr *
test_gelf_shdr_udata(lua_State *L, int arg)
{
return luaL_testudata(L, arg, ELF_SHDR_MT);
}
static GElf_Shdr *
check_gelf_shdr_udata(lua_State *L, int arg)
{
return luaL_checkudata(L, arg, ELF_SHDR_MT);
}
static GElf_Cap *
push_gelf_cap_udata(lua_State *L)
{
GElf_Cap *ud;
ud = lua_newuserdata(L, sizeof(*ud));
memset(ud, 0, sizeof(*ud));
luaL_getmetatable(L, ELF_CAP_MT);
lua_setmetatable(L, -2);
return ud;
}
static GElf_Cap *
check_gelf_cap_udata(lua_State *L, int arg)
{
return luaL_checkudata(L, arg, ELF_CAP_MT);
}
static GElf_Dyn *
push_gelf_dyn_udata(lua_State *L)
{
GElf_Dyn *ud;
ud = lua_newuserdata(L, sizeof(*ud));
memset(ud, 0, sizeof(*ud));
luaL_getmetatable(L, ELF_DYN_MT);
lua_setmetatable(L, -2);
return ud;
}
static GElf_Dyn *
test_gelf_dyn_udata(lua_State *L, int arg)
{
return luaL_testudata(L, arg, ELF_DYN_MT);
}
static GElf_Dyn *
check_gelf_dyn_udata(lua_State *L, int arg)
{
return luaL_checkudata(L, arg, ELF_DYN_MT);
}
static GElf_Rela *
push_gelf_rela_udata(lua_State *L)
{
GElf_Rela *ud;
ud = lua_newuserdata(L, sizeof(*ud));
memset(ud, 0, sizeof(*ud));
luaL_getmetatable(L, ELF_RELA_MT);
lua_setmetatable(L, -2);
return ud;
}
static GElf_Rela *
check_gelf_rela_udata(lua_State *L, int arg)
{
return luaL_checkudata(L, arg, ELF_RELA_MT);
}
static GElf_Rel *
push_gelf_rel_udata(lua_State *L)
{
GElf_Rel *ud;
ud = lua_newuserdata(L, sizeof(*ud));
memset(ud, 0, sizeof(*ud));
luaL_getmetatable(L, ELF_REL_MT);
lua_setmetatable(L, -2);
return ud;
}
static GElf_Rel *
check_gelf_rel_udata(lua_State *L, int arg)
{
return luaL_checkudata(L, arg, ELF_REL_MT);
}
static GElf_Syminfo *
push_gelf_syminfo_udata(lua_State *L)
{
GElf_Syminfo *ud;
ud = lua_newuserdata(L, sizeof(*ud));
memset(ud, 0, sizeof(*ud));
luaL_getmetatable(L, ELF_SYMINFO_MT);
lua_setmetatable(L, -2);
return ud;
}
static GElf_Syminfo *
check_gelf_syminfo_udata(lua_State *L, int arg)
{
return luaL_checkudata(L, arg, ELF_SYMINFO_MT);
}
static GElf_Sym *
push_gelf_sym_udata(lua_State *L)
{
GElf_Sym *ud;
ud = lua_newuserdata(L, sizeof(*ud));
memset(ud, 0, sizeof(*ud));
luaL_getmetatable(L, ELF_SYM_MT);
lua_setmetatable(L, -2);
return ud;
}
static GElf_Sym *
test_gelf_sym_udata(lua_State *L, int arg)
{
return luaL_testudata(L, arg, ELF_SYM_MT);
}
static GElf_Sym *
check_gelf_sym_udata(lua_State *L, int arg)
{
return luaL_checkudata(L, arg, ELF_SYM_MT);
}
static int
push_err_results(lua_State *L, int err, const char *errmsg)
{
if (errmsg == NULL)
errmsg = elf_errmsg(err);
lua_pushnil(L);
lua_pushstring(L, errmsg);
lua_pushinteger(L, err); /* XXX Convert to a string. */
return 3;
}
static int
l_elf_begin(lua_State *L)
{
struct udataElf *ud;
const char *filename;
filename = luaL_checkstring(L, 1);
ud = lua_newuserdata(L, sizeof(*ud));
memset(ud, 0, sizeof(*ud));
ud->fd = -1;
luaL_getmetatable(L, ELF_MT);
lua_setmetatable(L, -2);
if ((ud->fd = open(filename, O_RDONLY | O_CLOEXEC)) == -1) {
int err = errno;
/* XXX This function is not for reporting C errors. */
return push_err_results(L, err, (const char *)strerror(err));
}
if ((ud->elf = elf_begin(ud->fd, ELF_C_READ, NULL)) == NULL)
return push_err_results(L, elf_errno(), NULL);
return 1;
}
static int
l_elf_checksum(lua_State *L)
{
struct udataElf *ud;
long checksum;
ud = check_elf_udata(L, 1, 1);
checksum = gelf_checksum(ud->elf);
if (checksum == 0) {
int err = elf_errno();
if (err != ELF_E_NONE)
return push_err_results(L, err, NULL);
}
lua_pushinteger(L, checksum);
return 1;
}
static int
l_elf_getbase(lua_State *L)
{
struct udataElf *ud;
off_t base;
ud = check_elf_udata(L, 1, 1);
base = elf_getbase(ud->elf);
if (base == -1)
return push_err_results(L, elf_errno(), NULL);
lua_pushinteger(L, base);
return 1;
}
static int
l_elf_kind(lua_State *L)
{
struct udataElf *ud;
Elf_Kind kind;
ud = check_elf_udata(L, 1, 1);
kind = elf_kind(ud->elf);
switch (kind) {
case ELF_K_AR:
lua_pushstring(L, "AR");
return 1;
case ELF_K_ELF:
lua_pushstring(L, "ELF");
return 1;
case ELF_K_NONE:
lua_pushstring(L, "NONE");
return 1;
default:
lua_pushnil(L);
lua_pushfstring(L, "unsupported elf kind %d", (int)kind);
return 2;
}
}
static int
l_elf_hash(lua_State *L)
{
const char *str;
str = luaL_checkstring(L, 1);
lua_pushinteger(L, elf_hash(str));
return 1;
}
static int
l_elf_getarhdr(lua_State *L)
{
struct udataElf *elf;
struct udataElfArhdr *ud;
Elf_Arhdr *arhdr;
elf = check_elf_udata(L, 1, 1);
if ((arhdr = elf_getarhdr(elf->elf)) == NULL)
return push_err_results(L, elf_errno(), NULL);
ud = lua_newuserdata(L, sizeof(*ud));
memset(ud, 0, sizeof(*ud));
luaL_getmetatable(L, ELF_ARHDR_MT);