aboutsummaryrefslogtreecommitdiff
path: root/modules/objfmts/elf/elf.c
blob: addec913dcf750018c3cef680f7a25aabc75ff95 (plain)
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
/*
 * ELF object format helpers
 *
 *  Copyright (C) 2003-2007  Michael Urman
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <util.h>

#include <libyasm.h>
#define YASM_OBJFMT_ELF_INTERNAL
#include "elf.h"
#include "elf-machine.h"

static void elf_section_data_destroy(void *data);
static void elf_secthead_print(void *data, FILE *f, int indent_level);

const yasm_assoc_data_callback elf_section_data = {
    elf_section_data_destroy,
    elf_secthead_print
};

static void elf_symrec_data_destroy(/*@only@*/ void *d);
static void elf_symtab_entry_print(void *data, FILE *f, int indent_level);
static void elf_ssym_symtab_entry_print(void *data, FILE *f, int indent_level);

const yasm_assoc_data_callback elf_symrec_data = {
    elf_symrec_data_destroy,
    elf_symtab_entry_print
};

const yasm_assoc_data_callback elf_ssym_symrec_data = {
    elf_symrec_data_destroy,
    elf_ssym_symtab_entry_print
};

extern elf_machine_handler
    elf_machine_handler_x86_x86,
    elf_machine_handler_x86_amd64,
    elf_machine_handler_x86_x32;

static const elf_machine_handler *elf_machine_handlers[] =
{
    &elf_machine_handler_x86_x86,
    &elf_machine_handler_x86_amd64,
    &elf_machine_handler_x86_x32,
    NULL
};
static const elf_machine_handler elf_null_machine = {0, 0, 0, 0, 0, 0, 0, 0,
                                                     0, 0, 0, 0, 0, 0, 0, 0,
                                                     0, 0, 0};
static elf_machine_handler const *elf_march = &elf_null_machine;
static yasm_symrec **elf_ssyms;

const elf_machine_handler *
elf_set_arch(yasm_arch *arch, yasm_symtab *symtab, int bits_pref)
{
    const char *machine = yasm_arch_get_machine(arch);
    int i;

    for (i=0, elf_march = elf_machine_handlers[0];
         elf_march != NULL;
         elf_march = elf_machine_handlers[++i])
    {
        if (yasm__strcasecmp(yasm_arch_keyword(arch), elf_march->arch)==0)
            if (yasm__strcasecmp(machine, elf_march->machine)==0) {
                if (bits_pref == 0 || bits_pref == elf_march->bits)
                    break;
            } else if (bits_pref == elf_march->bits
                       && yasm__strcasecmp(machine, "amd64") == 0
                       && yasm__strcasecmp(elf_march->machine, "x32") == 0)
                break;
    }

    if (elf_march && elf_march->num_ssyms > 0)
    {
        /* Allocate "special" syms */
        elf_ssyms =
            yasm_xmalloc(elf_march->num_ssyms * sizeof(yasm_symrec *));
        for (i=0; (unsigned int)i<elf_march->num_ssyms; i++)
        {
            /* FIXME: misuse of NULL bytecode */
            elf_ssyms[i] = yasm_symtab_define_label(symtab,
                                                    elf_march->ssyms[i].name,
                                                    NULL, 0, 0);
            yasm_symrec_add_data(elf_ssyms[i], &elf_ssym_symrec_data,
                                 (void*)&elf_march->ssyms[i]);
        }
    }

    return elf_march;
}

yasm_symrec *
elf_get_special_sym(const char *name, const char *parser)
{
    int i;
    for (i=0; (unsigned int)i<elf_march->num_ssyms; i++) {
        if (yasm__strcasecmp(name, elf_march->ssyms[i].name) == 0)
            return elf_ssyms[i];
    }
    return NULL;
}

/* reloc functions */
int elf_ssym_has_flag(yasm_symrec *wrt, int flag);

int
elf_is_wrt_sym_relative(yasm_symrec *wrt)
{
    return elf_ssym_has_flag(wrt, ELF_SSYM_SYM_RELATIVE);
}

int
elf_is_wrt_pos_adjusted(yasm_symrec *wrt)
{
    return elf_ssym_has_flag(wrt, ELF_SSYM_CURPOS_ADJUST);
}

int
elf_ssym_has_flag(yasm_symrec *wrt, int flag)
{
    int i;
    for (i=0; (unsigned int)i<elf_march->num_ssyms; i++) {
        if (elf_ssyms[i] == wrt)
            return (elf_march->ssyms[i].sym_rel & flag) != 0;
    }
    return 0;
}

/* takes ownership of addr */
elf_reloc_entry *
elf_reloc_entry_create(yasm_symrec *sym,
                       yasm_symrec *wrt,
                       yasm_intnum *addr,
                       int rel,
                       size_t valsize,
                       int is_GOT_sym)
{
    elf_reloc_entry *entry;

    if (!elf_march->accepts_reloc)
        yasm_internal_error(N_("Unsupported machine for ELF output"));

    if (!elf_march->accepts_reloc(valsize, wrt))
    {
        if (addr)
            yasm_intnum_destroy(addr);
        return NULL;
    }

    if (sym == NULL)
        yasm_internal_error("sym is null");

    entry = yasm_xmalloc(sizeof(elf_reloc_entry));
    entry->reloc.sym = sym;
    entry->reloc.addr = addr;
    entry->rtype_rel = rel;
    entry->valsize = valsize;
    entry->addend = NULL;
    entry->wrt = wrt;
    entry->is_GOT_sym = is_GOT_sym;

    return entry;
}

void
elf_reloc_entry_destroy(void *entry)
{
    if (((elf_reloc_entry*)entry)->addend)
        yasm_intnum_destroy(((elf_reloc_entry*)entry)->addend);
    yasm_xfree(entry);
}

/* strtab functions */
elf_strtab_entry *
elf_strtab_entry_create(const char *str)
{
    elf_strtab_entry *entry = yasm_xmalloc(sizeof(elf_strtab_entry));
    entry->str = yasm__xstrdup(str);
    entry->index = 0;
    return entry;
}

void
elf_strtab_entry_set_str(elf_strtab_entry *entry, const char *str)
{
    elf_strtab_entry *last;
    if (entry->str)
        yasm_xfree(entry->str);
    entry->str = yasm__xstrdup(str);

    /* Update all following indices since string length probably changes */
    last = entry;
    entry = STAILQ_NEXT(last, qlink);
    while (entry) {
        entry->index = last->index + (unsigned long)strlen(last->str) + 1;
        last = entry;
        entry = STAILQ_NEXT(last, qlink);
    }
}

elf_strtab_head *
elf_strtab_create()
{
    elf_strtab_head *strtab = yasm_xmalloc(sizeof(elf_strtab_head));
    elf_strtab_entry *entry = yasm_xmalloc(sizeof(elf_strtab_entry));

    STAILQ_INIT(strtab);
    entry->index = 0;
    entry->str = yasm__xstrdup("");

    STAILQ_INSERT_TAIL(strtab, entry, qlink);
    return strtab;
}

elf_strtab_entry *
elf_strtab_append_str(elf_strtab_head *strtab, const char *str)
{
    elf_strtab_entry *last, *entry;

    if (strtab == NULL)
        yasm_internal_error("strtab is null");
    if (STAILQ_EMPTY(strtab))
        yasm_internal_error("strtab is missing initial dummy entry");

    last = STAILQ_LAST(strtab, elf_strtab_entry, qlink);

    entry = elf_strtab_entry_create(str);
    entry->index = last->index + (unsigned long)strlen(last->str) + 1;

    STAILQ_INSERT_TAIL(strtab, entry, qlink);
    return entry;
}

void
elf_strtab_destroy(elf_strtab_head *strtab)
{
    elf_strtab_entry *s1, *s2;

    if (strtab == NULL)
        yasm_internal_error("strtab is null");
    if (STAILQ_EMPTY(strtab))
        yasm_internal_error("strtab is missing initial dummy entry");

    s1 = STAILQ_FIRST(strtab);
    while (s1 != NULL) {
        s2 = STAILQ_NEXT(s1, qlink);
        yasm_xfree(s1->str);
        yasm_xfree(s1);
        s1 = s2;
    }
    yasm_xfree(strtab);
}

unsigned long
elf_strtab_output_to_file(FILE *f, elf_strtab_head *strtab)
{
    unsigned long size = 0;
    elf_strtab_entry *entry;

    if (strtab == NULL)
        yasm_internal_error("strtab is null");

    /* consider optimizing tables here */
    STAILQ_FOREACH(entry, strtab, qlink) {
        size_t len = 1 + strlen(entry->str);
        fwrite(entry->str, len, 1, f);
        size += (unsigned long)len;
    }
    return size;
}



/* symtab functions */
elf_symtab_entry *
elf_symtab_entry_create(elf_strtab_entry *name,
                        yasm_symrec *sym)
{
    elf_symtab_entry *entry = yasm_xmalloc(sizeof(elf_symtab_entry));
    entry->in_table = 0;
    entry->sym = sym;
    entry->sect = NULL;
    entry->name = name;
    entry->value = 0;

    entry->xsize = NULL;
    entry->size = 0;
    entry->index = 0;
    entry->bind = 0;
    entry->type = STT_NOTYPE;
    entry->vis = STV_DEFAULT;

    return entry;
}

static void
elf_symtab_entry_destroy(elf_symtab_entry *entry)
{
    if (entry == NULL)
        yasm_internal_error("symtab entry is null");

    yasm_xfree(entry);
}

static void
elf_symrec_data_destroy(void *data)
{
    /* do nothing, as this stuff is in the symtab anyway...  this speaks of bad
     * design/use or this stuff, i fear */

    /* watch for double-free here ... */
    /*elf_symtab_entry_destroy((elf_symtab_entry *)data);*/
}

static void
elf_symtab_entry_print(void *data, FILE *f, int indent_level)
{
    elf_symtab_entry *entry = data;
    if (entry == NULL)
        yasm_internal_error("symtab entry is null");

    fprintf(f, "%*sbind=", indent_level, "");
    switch (entry->bind) {
        case STB_LOCAL:         fprintf(f, "local\n");  break;
        case STB_GLOBAL:        fprintf(f, "global\n"); break;
        case STB_WEAK:          fprintf(f, "weak\n");   break;
        default:                fprintf(f, "undef\n");  break;
    }
    fprintf(f, "%*stype=", indent_level, "");
    switch (entry->type) {
        case STT_NOTYPE:        fprintf(f, "notype\n"); break;
        case STT_OBJECT:        fprintf(f, "object\n"); break;
        case STT_FUNC:          fprintf(f, "func\n");   break;
        case STT_SECTION:       fprintf(f, "section\n");break;
        case STT_FILE:          fprintf(f, "file\n");   break;
        default:                fprintf(f, "undef\n");  break;
    }
    fprintf(f, "%*ssize=", indent_level, "");
    if (entry->xsize)
        yasm_expr_print(entry->xsize, f);
    else
        fprintf(f, "%ld", entry->size);
    fprintf(f, "\n");
}

static void
elf_ssym_symtab_entry_print(void *data, FILE *f, int indent_level)
{
    /* TODO */
}

elf_symtab_head *
elf_symtab_create()
{
    elf_symtab_head *symtab = yasm_xmalloc(sizeof(elf_symtab_head));
    elf_symtab_entry *entry = yasm_xmalloc(sizeof(elf_symtab_entry));

    STAILQ_INIT(symtab);
    entry->in_table = 1;
    entry->sym = NULL;
    entry->sect = NULL;
    entry->name = NULL;
    entry->value = 0;
    entry->xsize = NULL;
    entry->size = 0;
    entry->index = SHN_UNDEF;
    entry->bind = STB_LOCAL;
    entry->type = STT_NOTYPE;
    entry->vis = STV_DEFAULT;
    entry->symindex = 0;
    STAILQ_INSERT_TAIL(symtab, entry, qlink);
    return symtab;
}

void
elf_symtab_append_entry(elf_symtab_head *symtab, elf_symtab_entry *entry)
{
    if (symtab == NULL)
        yasm_internal_error("symtab is null");
    if (entry == NULL)
        yasm_internal_error("symtab entry is null");
    if (STAILQ_EMPTY(symtab))
        yasm_internal_error(N_("symtab is missing initial dummy entry"));

    STAILQ_INSERT_TAIL(symtab, entry, qlink);
    entry->in_table = 1;
}

void
elf_symtab_insert_local_sym(elf_symtab_head *symtab, elf_symtab_entry *entry)
{
    elf_symtab_entry *after = STAILQ_FIRST(symtab);
    elf_symtab_entry *before = NULL;

    while (after && (after->bind == STB_LOCAL)) {
        before = after;
        if (before->type == STT_FILE) break;
        after = STAILQ_NEXT(after, qlink);
    }
    STAILQ_INSERT_AFTER(symtab, before, entry, qlink);
    entry->in_table = 1;
}

void
elf_symtab_destroy(elf_symtab_head *symtab)
{
    elf_symtab_entry *s1, *s2;

    if (symtab == NULL)
        yasm_internal_error("symtab is null");
    if (STAILQ_EMPTY(symtab))
        yasm_internal_error(N_("symtab is missing initial dummy entry"));

    s1 = STAILQ_FIRST(symtab);
    while (s1 != NULL) {
        s2 = STAILQ_NEXT(s1, qlink);
        elf_symtab_entry_destroy(s1);
        s1 = s2;
    }
    yasm_xfree(symtab);
}

unsigned long
elf_symtab_assign_indices(elf_symtab_head *symtab)
{
    elf_symtab_entry *entry, *prev=NULL;
    unsigned long last_local=0;

    if (symtab == NULL)
        yasm_internal_error("symtab is null");
    if (STAILQ_EMPTY(symtab))
        yasm_internal_error(N_("symtab is missing initial dummy entry"));

    STAILQ_FOREACH(entry, symtab, qlink) {
        if (prev)
            entry->symindex = prev->symindex + 1;
        if (entry->bind == STB_LOCAL)
            last_local = entry->symindex;
        prev = entry;
    }
    return last_local + 1;
}

unsigned long
elf_symtab_write_to_file(FILE *f, elf_symtab_head *symtab,
                         yasm_errwarns *errwarns)
{
    unsigned char buf[SYMTAB_MAXSIZE], *bufp;
    elf_symtab_entry *entry, *prev;
    unsigned long size = 0;

    if (!symtab)
        yasm_internal_error(N_("symtab is null"));

    prev = NULL;
    STAILQ_FOREACH(entry, symtab, qlink) {

        yasm_intnum *size_intn=NULL, *value_intn=NULL;
        bufp = buf;

        /* get size (if specified); expr overrides stored integer */
        if (entry->xsize) {
            size_intn = yasm_intnum_copy(
                yasm_expr_get_intnum(&entry->xsize, 1));
            if (!size_intn) {
                yasm_error_set(YASM_ERROR_VALUE,
                               N_("size specifier not an integer expression"));
                yasm_errwarn_propagate(errwarns, entry->xsize->line);
            }
        }
        else
            size_intn = yasm_intnum_create_uint(entry->size);

        /* get EQU value for constants */
        if (entry->sym) {
            const yasm_expr *equ_expr_c;
            equ_expr_c = yasm_symrec_get_equ(entry->sym);

            if (equ_expr_c != NULL) {
                const yasm_intnum *equ_intn;
                yasm_expr *equ_expr = yasm_expr_copy(equ_expr_c);
                equ_intn = yasm_expr_get_intnum(&equ_expr, 1);

                if (equ_intn == NULL) {
                    yasm_error_set(YASM_ERROR_VALUE,
                                   N_("EQU value not an integer expression"));
                    yasm_errwarn_propagate(errwarns, equ_expr->line);
                } else
                    value_intn = yasm_intnum_copy(equ_intn);
                entry->index = SHN_ABS;
                yasm_expr_destroy(equ_expr);
            }
        }
        if (value_intn == NULL)
            value_intn = yasm_intnum_create_uint(entry->value);

        /* If symbol is in a TLS section, force its type to TLS. */
        if (entry->sym) {
            yasm_bytecode *precbc;
            yasm_section *sect;
            elf_secthead *shead;
            if (yasm_symrec_get_label(entry->sym, &precbc) &&
                (sect = yasm_bc_get_section(precbc)) &&
                (shead = yasm_section_get_data(sect, &elf_section_data)) &&
                shead->flags & SHF_TLS) {
                entry->type = STT_TLS;
            }
        }

        if (!elf_march->write_symtab_entry || !elf_march->symtab_entry_size)
            yasm_internal_error(N_("Unsupported machine for ELF output"));
        elf_march->write_symtab_entry(bufp, entry, value_intn, size_intn);
        fwrite(buf, elf_march->symtab_entry_size, 1, f);
        size += elf_march->symtab_entry_size;

        yasm_intnum_destroy(size_intn);
        yasm_intnum_destroy(value_intn);

        prev = entry;
    }
    return size;
}

void elf_symtab_set_nonzero(elf_symtab_entry *entry,
                            yasm_section *sect,
                            elf_section_index sectidx,
                            elf_symbol_binding bind,
                            elf_symbol_type type,
                            yasm_expr *xsize,
                            elf_address *value)
{
    if (!entry)
        yasm_internal_error("NULL entry");
    if (sect) entry->sect = sect;
    if (sectidx) entry->index = sectidx;
    if (bind) entry->bind = bind;
    if (type) entry->type = type;
    if (xsize) entry->xsize = xsize;
    if (value) entry->value = *value;
}

void
elf_sym_set_visibility(elf_symtab_entry *entry,
                       elf_symbol_vis    vis)
{
    entry->vis = ELF_ST_VISIBILITY(vis);
}                            

void
elf_sym_set_type(elf_symtab_entry *entry,
                 elf_symbol_type   type)
{
    entry->type = type;
}                            

void
elf_sym_set_size(elf_symtab_entry *entry,
                 struct yasm_expr *size)
{
    if (entry->xsize)
        yasm_expr_destroy(entry->xsize);
    entry->xsize = size;
}                            

int
elf_sym_in_table(elf_symtab_entry *entry)
{
    return entry->in_table;
}

elf_secthead *
elf_secthead_create(elf_strtab_entry    *name,
                    elf_section_type     type,
                    elf_section_flags    flags,
                    elf_address          offset,
                    elf_size             size)
{
    elf_secthead *esd = yasm_xmalloc(sizeof(elf_secthead));

    esd->type = type;
    esd->flags = flags;
    esd->offset = offset;
    esd->size = yasm_intnum_create_uint(size);
    esd->link = 0;
    esd->info = 0;
    esd->align = 0;
    esd->entsize = 0;
    esd->index = 0;

    esd->sym = NULL;
    esd->name = name;
    esd->index = 0;
    esd->rel_name = NULL;
    esd->rel_index = 0;
    esd->rel_offset = 0;
    esd->nreloc = 0;

    if (name && (strcmp(name->str, ".symtab") == 0)) {
        if (!elf_march->symtab_entry_size || !elf_march->symtab_entry_align)
            yasm_internal_error(N_("unsupported ELF format"));
        esd->entsize = elf_march->symtab_entry_size;
        esd->align = elf_march->symtab_entry_align;
    }

    return esd;
}

void
elf_secthead_destroy(elf_secthead *shead)
{
    if (shead == NULL)
        yasm_internal_error(N_("shead is null"));

    yasm_intnum_destroy(shead->size);

    yasm_xfree(shead);
}

static void
elf_section_data_destroy(void *data)
{
    elf_secthead_destroy((elf_secthead *)data);
}

static void
elf_secthead_print(void *data, FILE *f, int indent_level)
{
    elf_secthead *sect = data;
    fprintf(f, "%*sname=%s\n", indent_level, "",
            sect->name ? sect->name->str : "<undef>");
    fprintf(f, "%*ssym=\n", indent_level, "");
    yasm_symrec_print(sect->sym, f, indent_level+1);
    fprintf(f, "%*sindex=0x%x\n", indent_level, "", sect->index);
    fprintf(f, "%*sflags=", indent_level, "");
    if (sect->flags & SHF_WRITE)
        fprintf(f, "WRITE ");
    if (sect->flags & SHF_ALLOC)
        fprintf(f, "ALLOC ");
    if (sect->flags & SHF_EXECINSTR)
        fprintf(f, "EXEC ");
    /*if (sect->flags & SHF_MASKPROC)
        fprintf(f, "PROC-SPECIFIC"); */
    fprintf(f, "%*soffset=0x%lx\n", indent_level, "", sect->offset);
    fprintf(f, "%*ssize=0x%lx\n", indent_level, "",
            yasm_intnum_get_uint(sect->size));
    fprintf(f, "%*slink=0x%x\n", indent_level, "", sect->link);
    fprintf(f, "%*salign=%lu\n", indent_level, "", sect->align);
    fprintf(f, "%*snreloc=%ld\n", indent_level, "", sect->nreloc);
}

unsigned long
elf_secthead_write_to_file(FILE *f, elf_secthead *shead,
                           elf_section_index sindex)
{
    unsigned char buf[SHDR_MAXSIZE], *bufp = buf;
    shead->index = sindex;

    if (shead == NULL)
        yasm_internal_error("shead is null");

    if (!elf_march->write_secthead || !elf_march->secthead_size)
        yasm_internal_error(N_("Unsupported machine for ELF output"));
    elf_march->write_secthead(bufp, shead);
    if (fwrite(buf, elf_march->secthead_size, 1, f))
        return elf_march->secthead_size;
    yasm_internal_error(N_("Failed to write an elf section header"));
    return 0;
}

void
elf_secthead_append_reloc(yasm_section *sect, elf_secthead *shead,
                          elf_reloc_entry *reloc)
{
    if (sect == NULL)
        yasm_internal_error("sect is null");
    if (shead == NULL)
        yasm_internal_error("shead is null");
    if (reloc == NULL)
        yasm_internal_error("reloc is null");

    shead->nreloc++;
    yasm_section_add_reloc(sect, (yasm_reloc *)reloc, elf_reloc_entry_destroy);
}

char *
elf_secthead_name_reloc_section(const char *basesect)
{
    if (!elf_march->reloc_section_prefix)
    {
        yasm_internal_error(N_("Unsupported machine for ELF output"));
        return NULL;
    }
    else
    {
        size_t prepend_length = strlen(elf_march->reloc_section_prefix);
        char *sectname = yasm_xmalloc(prepend_length + strlen(basesect) + 1);
        strcpy(sectname, elf_march->reloc_section_prefix);
        strcat(sectname, basesect);
        return sectname;
    }
}

void
elf_handle_reloc_addend(yasm_intnum *intn,
                        elf_reloc_entry *reloc,
                        unsigned long offset)
{
    if (!elf_march->handle_reloc_addend)
        yasm_internal_error(N_("Unsupported machine for ELF output"));
    elf_march->handle_reloc_addend(intn, reloc, offset);
}

unsigned long
elf_secthead_write_rel_to_file(FILE *f, elf_section_index symtab_idx,
                               yasm_section *sect, elf_secthead *shead,
                               elf_section_index sindex)
{
    unsigned char buf[SHDR_MAXSIZE], *bufp = buf;

    if (shead == NULL)
        yasm_internal_error("shead is null");

    if (!yasm_section_relocs_first(sect))
        return 0;       /* no relocations, no .rel.* section header */

    shead->rel_index = sindex;

    if (!elf_march->write_secthead_rel || !elf_march->secthead_size)
        yasm_internal_error(N_("Unsupported machine for ELF output"));
    elf_march->write_secthead_rel(bufp, shead, symtab_idx, sindex);
    if (fwrite(buf, elf_march->secthead_size, 1, f))
        return elf_march->secthead_size;
    yasm_internal_error(N_("Failed to write an elf section header"));
    return 0;
}

unsigned long
elf_secthead_write_relocs_to_file(FILE *f, yasm_section *sect,
                                  elf_secthead *shead, yasm_errwarns *errwarns)
{
    elf_reloc_entry *reloc;
    unsigned char buf[RELOC_MAXSIZE], *bufp;
    unsigned long size = 0;
    long pos;

    if (shead == NULL)
        yasm_internal_error("shead is null");

    reloc = (elf_reloc_entry *)yasm_section_relocs_first(sect);
    if (!reloc)
        return 0;

    /* first align section to multiple of 4 */
    pos = ftell(f);
    if (pos == -1) {
        yasm_error_set(YASM_ERROR_IO,
                       N_("couldn't read position on output stream"));
        yasm_errwarn_propagate(errwarns, 0);
    }
    pos = (pos + 3) & ~3;
    if (fseek(f, pos, SEEK_SET) < 0) {
        yasm_error_set(YASM_ERROR_IO, N_("couldn't seek on output stream"));
        yasm_errwarn_propagate(errwarns, 0);
    }
    shead->rel_offset = (unsigned long)pos;


    while (reloc) {
        yasm_sym_vis vis;
        unsigned int r_type=0, r_sym;
        elf_symtab_entry *esym;

        esym = yasm_symrec_get_data(reloc->reloc.sym, &elf_symrec_data);
        if (esym)
            r_sym = esym->symindex;
        else
            r_sym = STN_UNDEF;

        vis = yasm_symrec_get_visibility(reloc->reloc.sym);
        if (!elf_march->map_reloc_info_to_type)
            yasm_internal_error(N_("Unsupported arch/machine for elf output"));
        r_type = elf_march->map_reloc_info_to_type(reloc);

        bufp = buf;
        if (!elf_march->write_reloc || !elf_march->reloc_entry_size)
            yasm_internal_error(N_("Unsupported arch/machine for elf output"));
        elf_march->write_reloc(bufp, reloc, r_type, r_sym);
        fwrite(buf, elf_march->reloc_entry_size, 1, f);
        size += elf_march->reloc_entry_size;

        reloc = (elf_reloc_entry *)
            yasm_section_reloc_next((yasm_reloc *)reloc);
    }
    return size;
}

elf_section_type
elf_secthead_get_type(elf_secthead *shead)
{
    return shead->type;
}

void
elf_secthead_set_typeflags(elf_secthead *shead, elf_section_type type,
                           elf_section_flags flags)
{
    shead->type = type;
    shead->flags = flags;
}

int
elf_secthead_is_empty(elf_secthead *shead)
{
    return yasm_intnum_is_zero(shead->size);
}

yasm_symrec *
elf_secthead_get_sym(elf_secthead *shead)
{
    return shead->sym;
}

elf_section_index
elf_secthead_get_index(elf_secthead *shead)
{
    return shead->index;
}

unsigned long
elf_secthead_get_align(const elf_secthead *shead)
{
    return shead->align;
}

unsigned long
elf_secthead_set_align(elf_secthead *shead, unsigned long align)
{
    return shead->align = align;
}

elf_section_info
elf_secthead_set_info(elf_secthead *shead, elf_section_info info)
{
    return shead->info = info;
}

elf_section_index
elf_secthead_set_index(elf_secthead *shead, elf_section_index sectidx)
{
    return shead->index = sectidx;
}

elf_section_index
elf_secthead_set_link(elf_secthead *shead, elf_section_index link)
{
    return shead->link = link;
}

elf_section_index
elf_secthead_set_rel_index(elf_secthead *shead, elf_section_index sectidx)
{
    return shead->rel_index = sectidx;
}

elf_strtab_entry *
elf_secthead_set_rel_name(elf_secthead *shead, elf_strtab_entry *entry)
{
    return shead->rel_name = entry;
}

elf_size
elf_secthead_set_entsize(elf_secthead *shead, elf_size size)
{
    return shead->entsize = size;
}

yasm_symrec *
elf_secthead_set_sym(elf_secthead *shead, yasm_symrec *sym)
{
    return shead->sym = sym;
}

void
elf_secthead_add_size(elf_secthead *shead, yasm_intnum *size)
{
    if (size) {
        yasm_intnum_calc(shead->size, YASM_EXPR_ADD, size);
    }
}

long
elf_secthead_set_file_offset(elf_secthead *shead, long pos)
{
    unsigned long align = shead->align;

    if (align == 0 || align == 1) {
        shead->offset = (unsigned long)pos;
        return pos;
    }
    else if (align & (align - 1))
        yasm_internal_error(
            N_("alignment %d for section `%s' is not a power of 2"));
            /*, align, sect->name->str);*/

    shead->offset = (unsigned long)((pos + align - 1) & ~(align - 1));
    return (long)shead->offset;
}

unsigned long
elf_proghead_get_size(void)
{
    if (!elf_march->proghead_size)
        yasm_internal_error(N_("Unsupported ELF format for output"));
    return elf_march->proghead_size;
}

unsigned long
elf_proghead_write_to_file(FILE *f,
                           elf_offset secthead_addr,
                           unsigned long secthead_count,
                           elf_section_index shstrtab_index)
{
    unsigned char buf[EHDR_MAXSIZE], *bufp = buf;

    YASM_WRITE_8(bufp, ELFMAG0);                /* ELF magic number */
    YASM_WRITE_8(bufp, ELFMAG1);
    YASM_WRITE_8(bufp, ELFMAG2);
    YASM_WRITE_8(bufp, ELFMAG3);

    if (!elf_march->write_proghead || !elf_march->proghead_size)
        yasm_internal_error(N_("Unsupported ELF format for output"));
    elf_march->write_proghead(&bufp, secthead_addr, secthead_count, shstrtab_index);

    if (((unsigned)(bufp - buf)) != elf_march->proghead_size)
        yasm_internal_error(N_("ELF program header is not proper length"));

    if (fwrite(buf, elf_march->proghead_size, 1, f))
        return elf_march->proghead_size;

    yasm_internal_error(N_("Failed to write ELF program header"));
    return 0;
}