145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/*
245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ELF object format helpers - x86:x86
345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *  Copyright (C) 2004-2007  Michael Urman
545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Redistribution and use in source and binary forms, with or without
745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * modification, are permitted provided that the following conditions
845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * are met:
945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 1. Redistributions of source code must retain the above copyright
1045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    notice, this list of conditions and the following disclaimer.
1145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 2. Redistributions in binary form must reproduce the above copyright
1245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    notice, this list of conditions and the following disclaimer in the
1345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    documentation and/or other materials provided with the distribution.
1445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
1545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
1645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
1945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * POSSIBILITY OF SUCH DAMAGE.
2645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */
2745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
2845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <util.h>
2945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <libyasm.h>
3145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define YASM_OBJFMT_ELF_INTERNAL
3245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "elf.h"
3345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "elf-machine.h"
3445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
35d65182f1818d1c19e6f3866ab6e68a262fad5185hbono@chromium.orgstatic elf_machine_ssym elf_x86_x86_ssyms[] = {
3645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    {"plt",         ELF_SSYM_SYM_RELATIVE,  R_386_PLT32,        32},
3745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    {"gotoff",      0,                      R_386_GOTOFF,       32},
3845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /* special one for NASM */
3945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    {"gotpc",       ELF_SSYM_CURPOS_ADJUST, R_386_GOTPC,        32},
4045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    {"tlsgd",       ELF_SSYM_SYM_RELATIVE|ELF_SSYM_THREAD_LOCAL,
4145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                    R_386_TLS_GD,       32},
4245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    {"tlsldm",      ELF_SSYM_SYM_RELATIVE|ELF_SSYM_THREAD_LOCAL,
4345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                    R_386_TLS_LDM,      32},
4445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    {"gottpoff",    ELF_SSYM_SYM_RELATIVE|ELF_SSYM_THREAD_LOCAL,
4545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                    R_386_TLS_IE_32,    32},
4645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    {"tpoff",       ELF_SSYM_SYM_RELATIVE|ELF_SSYM_THREAD_LOCAL,
4745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                    R_386_TLS_LE_32,    32},
4845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    {"ntpoff",      ELF_SSYM_SYM_RELATIVE|ELF_SSYM_THREAD_LOCAL,
4945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                    R_386_TLS_LE,       32},
5045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    {"dtpoff",      ELF_SSYM_SYM_RELATIVE|ELF_SSYM_THREAD_LOCAL,
5145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                    R_386_TLS_LDO_32,   32},
5245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    {"gotntpoff",   ELF_SSYM_SYM_RELATIVE|ELF_SSYM_THREAD_LOCAL,
5345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                    R_386_TLS_GOTIE,    32},
5445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    {"indntpoff",   ELF_SSYM_SYM_RELATIVE|ELF_SSYM_THREAD_LOCAL,
5545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                    R_386_TLS_IE,       32},
56a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org    {"got",         ELF_SSYM_SYM_RELATIVE,  R_386_GOT32,        32},
57a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org    {"tlsdesc",     ELF_SSYM_SYM_RELATIVE|ELF_SSYM_THREAD_LOCAL,
58a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org                    R_386_TLS_GOTDESC,  32},
59a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org    {"tlscall",     ELF_SSYM_SYM_RELATIVE|ELF_SSYM_THREAD_LOCAL,
60a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org                    R_386_TLS_DESC_CALL,    32}
6145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org};
6245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
6345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic int
64a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.orgelf_x86_x86_accepts_reloc(size_t val, yasm_symrec *wrt)
6545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
6645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (wrt) {
67a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org        const elf_machine_ssym *ssym = (elf_machine_ssym *)
68a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org            yasm_symrec_get_data(wrt, &elf_ssym_symrec_data);
69a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org        if (!ssym || val != ssym->size)
70a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org            return 0;
71a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org        return 1;
7245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
7345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return (val&(val-1)) ? 0 : ((val & (8|16|32)) != 0);
7445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
7545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
7645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void
7745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgelf_x86_x86_write_symtab_entry(unsigned char *bufp,
7845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                               elf_symtab_entry *entry,
7945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                               yasm_intnum *value_intn,
8045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                               yasm_intnum *size_intn)
8145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
8245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, entry->name ? entry->name->index : 0);
8345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32I_L(bufp, value_intn);
8445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32I_L(bufp, size_intn);
8545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
8645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_8(bufp, ELF32_ST_INFO(entry->bind, entry->type));
8745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_8(bufp, ELF32_ST_OTHER(entry->vis));
8845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (entry->sect) {
8945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        elf_secthead *shead =
9045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            yasm_section_get_data(entry->sect, &elf_section_data);
9145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        if (!shead)
9245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            yasm_internal_error(N_("symbol references section without data"));
9345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        YASM_WRITE_16_L(bufp, shead->index);
9445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    } else {
9545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        YASM_WRITE_16_L(bufp, entry->index);
9645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
9745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
9845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
9945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void
10045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgelf_x86_x86_write_secthead(unsigned char *bufp, elf_secthead *shead)
10145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
10245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, shead->name ? shead->name->index : 0);
10345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, shead->type);
10445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, shead->flags);
10545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, 0); /* vmem address */
10645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
10745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, shead->offset);
10845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32I_L(bufp, shead->size);
10945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, shead->link);
11045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, shead->info);
11145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
11245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, shead->align);
11345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, shead->entsize);
11445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
11545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
11645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
11745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void
11845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgelf_x86_x86_write_secthead_rel(unsigned char *bufp,
11945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                               elf_secthead *shead,
12045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                               elf_section_index symtab_idx,
12145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                               elf_section_index sindex)
12245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
12345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, shead->rel_name ? shead->rel_name->index : 0);
12445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, SHT_REL);
12545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, 0);
12645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, 0);
12745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
12845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, shead->rel_offset);
12945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, RELOC32_SIZE * shead->nreloc);/* size */
13045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, symtab_idx);          /* link: symtab index */
13145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, shead->index);        /* info: relocated's index */
13245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
13345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, RELOC32_ALIGN);       /* align */
13445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, RELOC32_SIZE);        /* entity size */
13545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
13645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
13745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void
138a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.orgelf_x86_x86_handle_reloc_addend(yasm_intnum *intn,
139a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org                                elf_reloc_entry *reloc,
140a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org                                unsigned long offset)
14145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
142a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org    if (!reloc->wrt && reloc->is_GOT_sym && reloc->valsize == 32 && offset != 0)
143a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org    {
144a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org        yasm_intnum *off_intn = yasm_intnum_create_uint(offset);
145a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org        yasm_intnum_calc(intn, YASM_EXPR_ADD, off_intn);
146a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org        yasm_intnum_destroy(off_intn);
147a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org    }
14845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return; /* .rel: Leave addend in intn */
14945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
15045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
15145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic unsigned int
152a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.orgelf_x86_x86_map_reloc_info_to_type(elf_reloc_entry *reloc)
15345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
15445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (reloc->wrt) {
155a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org        const elf_machine_ssym *ssym = (elf_machine_ssym *)
156a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org            yasm_symrec_get_data(reloc->wrt, &elf_ssym_symrec_data);
157a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org        if (!ssym || reloc->valsize != ssym->size)
158a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org            yasm_internal_error(N_("Unsupported WRT"));
159a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org
160a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org        /* Force TLS type; this is required by the linker. */
161a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org        if (ssym->sym_rel & ELF_SSYM_THREAD_LOCAL) {
162a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org            elf_symtab_entry *esym;
163a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org
164a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org            esym = yasm_symrec_get_data(reloc->reloc.sym, &elf_symrec_data);
165a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org            if (esym)
166a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org                esym->type = STT_TLS;
16745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        }
168a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org        return (unsigned char) ssym->reloc;
169a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org    } else if (reloc->is_GOT_sym && reloc->valsize == 32) {
170a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org        return (unsigned char) R_386_GOTPC;
17145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    } else if (reloc->rtype_rel) {
17245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        switch (reloc->valsize) {
17345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            case 8: return (unsigned char) R_386_PC8;
17445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            case 16: return (unsigned char) R_386_PC16;
17545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            case 32: return (unsigned char) R_386_PC32;
17645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            default: yasm_internal_error(N_("Unsupported relocation size"));
17745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        }
17845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    } else {
17945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        switch (reloc->valsize) {
18045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            case 8: return (unsigned char) R_386_8;
18145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            case 16: return (unsigned char) R_386_16;
18245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            case 32: return (unsigned char) R_386_32;
18345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            default: yasm_internal_error(N_("Unsupported relocation size"));
18445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        }
18545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
18645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return 0;
18745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
18845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
18945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void
19045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgelf_x86_x86_write_reloc(unsigned char *bufp, elf_reloc_entry *reloc,
19145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                        unsigned int r_type, unsigned int r_sym)
19245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
19345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32I_L(bufp, reloc->reloc.addr);
19445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, ELF32_R_INFO((unsigned long)r_sym, (unsigned char)r_type));
19545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
19645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
19745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void
19845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgelf_x86_x86_write_proghead(unsigned char **bufpp,
19945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                           elf_offset secthead_addr,
20045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                           unsigned long secthead_count,
20145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                           elf_section_index shstrtab_index)
20245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
20345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    unsigned char *bufp = *bufpp;
20445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    unsigned char *buf = bufp-4;
20545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_8(bufp, ELFCLASS32);         /* elf class */
20645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_8(bufp, ELFDATA2LSB);        /* data encoding :: MSB? */
20745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_8(bufp, EV_CURRENT);         /* elf version */
20845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    while (bufp-buf < EI_NIDENT)            /* e_ident padding */
20945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        YASM_WRITE_8(bufp, 0);
21045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
21145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_16_L(bufp, ET_REL);          /* e_type - object file */
21245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_16_L(bufp, EM_386);          /* e_machine - or others */
21345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, EV_CURRENT);      /* elf version */
21445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, 0);           /* e_entry exection startaddr */
21545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, 0);           /* e_phoff program header off */
21645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, secthead_addr);   /* e_shoff section header off */
21745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_32_L(bufp, 0);               /* e_flags also by arch */
21845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_16_L(bufp, EHDR32_SIZE);     /* e_ehsize */
21945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_16_L(bufp, 0);               /* e_phentsize */
22045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_16_L(bufp, 0);               /* e_phnum */
22145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_16_L(bufp, SHDR32_SIZE);     /* e_shentsize */
22245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_16_L(bufp, secthead_count);  /* e_shnum */
22345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YASM_WRITE_16_L(bufp, shstrtab_index);  /* e_shstrndx */
22445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    *bufpp = bufp;
22545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
22645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
22745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgconst elf_machine_handler
22845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgelf_machine_handler_x86_x86 = {
22945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    "x86", "x86", ".rel",
23045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    SYMTAB32_SIZE, SYMTAB32_ALIGN, RELOC32_SIZE, SHDR32_SIZE, EHDR32_SIZE,
23145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    elf_x86_x86_accepts_reloc,
23245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    elf_x86_x86_write_symtab_entry,
23345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    elf_x86_x86_write_secthead,
23445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    elf_x86_x86_write_secthead_rel,
23545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    elf_x86_x86_handle_reloc_addend,
23645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    elf_x86_x86_map_reloc_info_to_type,
23745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    elf_x86_x86_write_reloc,
23845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    elf_x86_x86_write_proghead,
23945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    elf_x86_x86_ssyms,
24045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    sizeof(elf_x86_x86_ssyms)/sizeof(elf_x86_x86_ssyms[0]),
24145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    32
24245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org};
243