| 1 | /*- |
|---|
| 2 | * Copyright (c) 2011,2012 Kai Wang |
|---|
| 3 | * All rights reserved. |
|---|
| 4 | * |
|---|
| 5 | * Redistribution and use in source and binary forms, with or without |
|---|
| 6 | * modification, are permitted provided that the following conditions |
|---|
| 7 | * are met: |
|---|
| 8 | * 1. Redistributions of source code must retain the above copyright |
|---|
| 9 | * notice, this list of conditions and the following disclaimer. |
|---|
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer in the |
|---|
| 12 | * documentation and/or other materials provided with the distribution. |
|---|
| 13 | * |
|---|
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
|---|
| 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|---|
| 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
|---|
| 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|---|
| 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|---|
| 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|---|
| 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|---|
| 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|---|
| 24 | * SUCH DAMAGE. |
|---|
| 25 | * |
|---|
| 26 | * $Id$ |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | enum ld_output_element_type { |
|---|
| 30 | OET_ASSERT, |
|---|
| 31 | OET_ASSIGN, |
|---|
| 32 | OET_DATA, |
|---|
| 33 | OET_ENTRY, |
|---|
| 34 | OET_INPUT_SECTION_LIST, |
|---|
| 35 | OET_KEYWORD, |
|---|
| 36 | OET_OUTPUT_SECTION, |
|---|
| 37 | OET_OVERLAY, |
|---|
| 38 | OET_DATA_BUFFER, |
|---|
| 39 | }; |
|---|
| 40 | |
|---|
| 41 | struct ld_output_element { |
|---|
| 42 | enum ld_output_element_type oe_type; /* output element type */ |
|---|
| 43 | uint64_t oe_off; /* output element offset */ |
|---|
| 44 | void *oe_entry; /* output element */ |
|---|
| 45 | void *oe_islist; /* input section list */ |
|---|
| 46 | unsigned char oe_insec; /* element inside SECTIONS */ |
|---|
| 47 | STAILQ_ENTRY(ld_output_element) oe_next; /* next element */ |
|---|
| 48 | }; |
|---|
| 49 | |
|---|
| 50 | STAILQ_HEAD(ld_output_element_head, ld_output_element); |
|---|
| 51 | |
|---|
| 52 | struct ld_output_data_buffer { |
|---|
| 53 | uint8_t *odb_buf; /* point to data */ |
|---|
| 54 | uint64_t odb_size; /* buffer size */ |
|---|
| 55 | uint64_t odb_off; /* relative offset in output section */ |
|---|
| 56 | uint64_t odb_align; /* buffer alignment */ |
|---|
| 57 | uint64_t odb_type; /* buffer data type */ |
|---|
| 58 | }; |
|---|
| 59 | |
|---|
| 60 | struct ld_output_section { |
|---|
| 61 | Elf_Scn *os_scn; /* output section descriptor */ |
|---|
| 62 | char *os_name; /* output section name */ |
|---|
| 63 | uint64_t os_addr; /* output section vma */ |
|---|
| 64 | uint64_t os_lma; /* output section lma */ |
|---|
| 65 | uint64_t os_off; /* output section offset */ |
|---|
| 66 | uint64_t os_size; /* output section size */ |
|---|
| 67 | uint64_t os_align; /* output section alignment */ |
|---|
| 68 | uint64_t os_flags; /* output section flags */ |
|---|
| 69 | uint64_t os_type; /* output section type */ |
|---|
| 70 | uint64_t os_entsize; /* output seciton entry size */ |
|---|
| 71 | unsigned os_empty; /* output section is empty */ |
|---|
| 72 | struct ld_script_sections_output *os_ldso; |
|---|
| 73 | /* output section descriptor */ |
|---|
| 74 | struct ld_output_element *os_pe; /* parent element */ |
|---|
| 75 | struct ld_output_element_head os_e; /* list of child elements */ |
|---|
| 76 | STAILQ_ENTRY(ld_output_section) os_next; /* next output section */ |
|---|
| 77 | UT_hash_handle hh; /* hash handle */ |
|---|
| 78 | }; |
|---|
| 79 | |
|---|
| 80 | STAILQ_HEAD(ld_output_section_head, ld_output_section); |
|---|
| 81 | |
|---|
| 82 | struct ld_output { |
|---|
| 83 | int lo_fd; /* output file descriptor */ |
|---|
| 84 | Elf *lo_elf; /* output ELF descriptor */ |
|---|
| 85 | int lo_ec; /* output object elf class */ |
|---|
| 86 | int lo_endian; /* outout object endianess */ |
|---|
| 87 | int lo_osabi; /* output object osabi */ |
|---|
| 88 | unsigned lo_phdr_num; /* num of phdrs */ |
|---|
| 89 | unsigned lo_phdr_note; /* create PT_NOTE */ |
|---|
| 90 | struct ld_output_element_head lo_oelist; /* output element list */ |
|---|
| 91 | struct ld_output_section_head lo_oslist; /* output section list */ |
|---|
| 92 | struct ld_output_section *lo_ostbl; /* output section hash table */ |
|---|
| 93 | }; |
|---|
| 94 | |
|---|
| 95 | struct ld_output_section *ld_output_alloc_section(struct ld *, const char *, |
|---|
| 96 | struct ld_output_section *); |
|---|
| 97 | void ld_output_create(struct ld *); |
|---|
| 98 | struct ld_output_element *ld_output_create_element(struct ld *, |
|---|
| 99 | struct ld_output_element_head *, enum ld_output_element_type, void *, |
|---|
| 100 | struct ld_output_element *); |
|---|
| 101 | void ld_output_format(struct ld *, char *, char *, char *); |
|---|
| 102 | void ld_output_init(struct ld *); |
|---|
| 103 | void ld_output_write(struct ld *); |
|---|