378
355
elfsetupplt(void)
382
plt = lookup(".plt", 0);
383
got = lookup(".got.plt", 0);
359
plt = linklookup(ctxt, ".plt", 0);
360
got = linklookup(ctxt, ".got.plt", 0);
384
361
if(plt->size == 0) {
388
addaddrplus(plt, got, 4);
363
adduint8(ctxt, plt, 0xff);
364
adduint8(ctxt, plt, 0x35);
365
addaddrplus(ctxt, plt, got, 4);
393
addaddrplus(plt, got, 8);
368
adduint8(ctxt, plt, 0xff);
369
adduint8(ctxt, plt, 0x25);
370
addaddrplus(ctxt, plt, got, 8);
373
adduint32(ctxt, plt, 0);
398
375
// assume got->size == 0 too
399
addaddrplus(got, lookup(".dynamic", 0), 0);
376
addaddrplus(ctxt, got, linklookup(ctxt, ".dynamic", 0), 0);
377
adduint32(ctxt, got, 0);
378
adduint32(ctxt, got, 0);
383
addpltsym(Link *ctxt, LSym *s)
408
Sym *plt, *got, *rel;
385
LSym *plt, *got, *rel;
416
plt = lookup(".plt", 0);
417
got = lookup(".got.plt", 0);
418
rel = lookup(".rel.plt", 0);
393
plt = linklookup(ctxt, ".plt", 0);
394
got = linklookup(ctxt, ".got.plt", 0);
395
rel = linklookup(ctxt, ".rel.plt", 0);
419
396
if(plt->size == 0)
422
399
// jmpq *got+size
425
addaddrplus(plt, got, got->size);
400
adduint8(ctxt, plt, 0xff);
401
adduint8(ctxt, plt, 0x25);
402
addaddrplus(ctxt, plt, got, got->size);
427
404
// add to got: pointer to current pos in plt
428
addaddrplus(got, plt, plt->size);
405
addaddrplus(ctxt, got, plt, plt->size);
432
adduint32(plt, rel->size);
408
adduint8(ctxt, plt, 0x68);
409
adduint32(ctxt, plt, rel->size);
436
adduint32(plt, -(plt->size+4));
412
adduint8(ctxt, plt, 0xe9);
413
adduint32(ctxt, plt, -(plt->size+4));
439
addaddrplus(rel, got, got->size-4);
440
adduint32(rel, ELF32_R_INFO(s->dynid, R_386_JMP_SLOT));
416
addaddrplus(ctxt, rel, got, got->size-4);
417
adduint32(ctxt, rel, ELF32_R_INFO(s->dynid, R_386_JMP_SLOT));
442
419
s->plt = plt->size - 16;
443
420
} else if(HEADTYPE == Hdarwin) {
444
421
// Same laziness as in 6l.
448
plt = lookup(".plt", 0);
452
adduint32(lookup(".linkedit.plt", 0), s->dynid);
425
plt = linklookup(ctxt, ".plt", 0);
429
adduint32(ctxt, linklookup(ctxt, ".linkedit.plt", 0), s->dynid);
454
431
// jmpq *got+size(IP)
455
432
s->plt = plt->size;
459
addaddrplus(plt, lookup(".got", 0), s->got);
434
adduint8(ctxt, plt, 0xff);
435
adduint8(ctxt, plt, 0x25);
436
addaddrplus(ctxt, plt, linklookup(ctxt, ".got", 0), s->got);
461
438
diag("addpltsym: unsupported binary format");
443
addgotsym(Link *ctxt, LSym *s)
474
got = lookup(".got", 0);
451
got = linklookup(ctxt, ".got", 0);
475
452
s->got = got->size;
453
adduint32(ctxt, got, 0);
479
rel = lookup(".rel", 0);
480
addaddrplus(rel, got, s->got);
481
adduint32(rel, ELF32_R_INFO(s->dynid, R_386_GLOB_DAT));
456
rel = linklookup(ctxt, ".rel", 0);
457
addaddrplus(ctxt, rel, got, s->got);
458
adduint32(ctxt, rel, ELF32_R_INFO(s->dynid, R_386_GLOB_DAT));
482
459
} else if(HEADTYPE == Hdarwin) {
483
adduint32(lookup(".linkedit.got", 0), s->dynid);
460
adduint32(ctxt, linklookup(ctxt, ".linkedit.got", 0), s->dynid);
485
462
diag("addgotsym: unsupported binary format");
467
adddynsym(Link *ctxt, LSym *s)
716
682
switch(HEADTYPE) {
718
case Hgarbunix: /* garbage */
719
lputb(0x160L<<16); /* magic and sections */
720
lputb(0L); /* time and date */
721
lputb(rnd(HEADR+segtext.filelen, 4096)+segdata.filelen);
722
lputb(symsize); /* nsyms */
723
lputb((0x38L<<16)|7L); /* size of optional hdr and flags */
724
lputb((0413<<16)|0437L); /* magic and version */
725
lputb(rnd(HEADR+segtext.filelen, 4096)); /* sizes */
726
lputb(segdata.filelen);
727
lputb(segdata.len - segdata.filelen);
728
lputb(entryvalue()); /* va of entry */
729
lputb(INITTEXT-HEADR); /* va of base of text */
730
lputb(segdata.vaddr); /* va of base of data */
731
lputb(segdata.vaddr+segdata.filelen); /* va of base of bss */
732
lputb(~0L); /* gp reg mask */
737
lputb(~0L); /* gp value ?? */
739
case Hunixcoff: /* unix coff */
743
lputl(0x0004014c); /* 4 sections, magic */
744
lputl(0); /* unix time stamp */
745
lputl(0); /* symbol table */
746
lputl(0); /* nsyms */
747
lputl(0x0003001c); /* flags, sizeof a.out header */
751
lputl(0x10b); /* magic, version stamp */
752
lputl(rnd(segtext.filelen, INITRND)); /* text sizes */
753
lputl(segdata.filelen); /* data sizes */
754
lputl(segdata.len - segdata.filelen); /* bss sizes */
755
lputb(entryvalue()); /* va of entry */
756
lputl(INITTEXT); /* text start */
757
lputl(segdata.vaddr); /* data start */
759
* text section header
762
lputl(HEADR); /* pa */
763
lputl(HEADR); /* va */
764
lputl(segtext.filelen); /* text size */
765
lputl(HEADR); /* file offset */
766
lputl(0); /* relocation */
767
lputl(0); /* line numbers */
768
lputl(0); /* relocation, line numbers */
769
lputl(0x20); /* flags text only */
771
* data section header
774
lputl(segdata.vaddr); /* pa */
775
lputl(segdata.vaddr); /* va */
776
lputl(segdata.filelen); /* data size */
777
lputl(HEADR+segtext.filelen); /* file offset */
778
lputl(0); /* relocation */
779
lputl(0); /* line numbers */
780
lputl(0); /* relocation, line numbers */
781
lputl(0x40); /* flags data only */
786
lputl(segdata.vaddr+segdata.filelen); /* pa */
787
lputl(segdata.vaddr+segdata.filelen); /* va */
788
lputl(segdata.len - segdata.filelen); /* bss size */
789
lputl(0); /* file offset */
790
lputl(0); /* relocation */
791
lputl(0); /* line numbers */
792
lputl(0); /* relocation, line numbers */
793
lputl(0x80); /* flags bss only */
795
* comment section header
800
lputl(symsize+lcsize); /* comment size */
801
lputl(HEADR+segtext.filelen+segdata.filelen); /* file offset */
802
lputl(HEADR+segtext.filelen+segdata.filelen); /* offset of syms */
803
lputl(HEADR+segtext.filelen+segdata.filelen+symsize);/* offset of line numbers */
804
lputl(0); /* relocation, line numbers */
805
lputl(0x200); /* flags comment only */
807
case Hplan9x32: /* plan9 */
684
case Hplan9: /* plan9 */
808
685
magic = 4*11*11+7;
809
686
lputb(magic); /* magic */
810
687
lputb(segtext.filelen); /* sizes */
815
692
lputb(spsize); /* sp offsets */
816
693
lputb(lcsize); /* line offsets */
822
/* fake MS-DOS .EXE */
823
v = rnd(HEADR+segtext.filelen, INITRND)+segdata.filelen;
824
wputl(0x5A4D); /* 'MZ' */
825
wputl(v % 512); /* bytes in last page */
826
wputl(rnd(v, 512)/512); /* total number of pages */
827
wputl(0x0000); /* number of reloc items */
828
v = rnd(HEADR-(INITTEXT & 0xFFFF), 16);
829
wputl(v/16); /* size of header */
830
wputl(0x0000); /* minimum allocation */
831
wputl(0xFFFF); /* maximum allocation */
832
wputl(0x0000); /* initial ss value */
833
wputl(0x0100); /* initial sp value */
834
wputl(0x0000); /* complemented checksum */
836
wputl(v); /* initial ip value (!) */
837
wputl(0x0000); /* initial cs value */
840
wputl(0x003E); /* reloc table offset */
841
wputl(0x0000); /* overlay number */