1/*
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28#ifndef _KXLD_SECT_H_
29#define _KXLD_SECT_H_
30
31#include <sys/types.h>
32#if KERNEL
33    #include <libkern/kxld_types.h>
34#else
35    #include "kxld_types.h"
36#endif
37
38#include "kxld_array.h"
39
40struct kxld_array;
41struct kxld_relocator;
42struct kxld_reloc;
43struct kxld_seg;
44struct kxld_symtab;
45struct relocation_info;
46struct section;
47struct section_64;
48typedef struct kxld_sect KXLDSect;
49
50struct kxld_sect {
51    char sectname[16];              // The name of the section
52    char segname[16];               // The segment to which the section belongs
53    u_char *data;                   // The start of the section in memory
54    KXLDArray relocs;               // The section's relocation entries
55    kxld_addr_t base_addr;          // The base address of the section
56    kxld_addr_t link_addr;          // The relocated address of the section
57    kxld_size_t size;               // The size of the section
58    u_int sectnum;                  // The number of the section (for relocation)
59    u_int flags;                    // Flags describing the section
60    u_int align;                    // The section's alignment as a power of 2
61    u_int reserved1;                // Dependent on the section type
62    u_int reserved2;                // Dependent on the section type
63    boolean_t allocated;            // This section's data is allocated internally
64};
65
66/*******************************************************************************
67* Constructors and destructors
68*******************************************************************************/
69
70#if KXLD_USER_OR_ILP32
71/* Initializes a section object from a Mach-O section header and modifies the
72 * section offset to point to the next section header.
73 */
74kern_return_t kxld_sect_init_from_macho_32(KXLDSect *sect, u_char *macho,
75    u_long *sect_offset, u_int sectnum, const struct kxld_relocator *relocator)
76    __attribute__((nonnull, visibility("hidden")));
77#endif /* KXLD_USER_OR_ILP32 */
78
79#if KXLD_USER_OR_LP64
80/* Initializes a section object from a Mach-O64 section header and modifies the
81 * section offset to point to the next section header.
82 */
83kern_return_t kxld_sect_init_from_macho_64(KXLDSect *sect, u_char *macho,
84    u_long *sect_offset, u_int sectnum, const struct kxld_relocator *relocator)
85    __attribute__((nonnull, visibility("hidden")));
86#endif /* KXLD_USER_OR_LP64 */
87
88#if KXLD_USER_OR_GOT
89/* Initializes a GOT section from the number of entries that the section should
90 * have.
91 */
92kern_return_t kxld_sect_init_got(KXLDSect *sect, u_int ngots)
93    __attribute__((nonnull, visibility("hidden")));
94#endif /* KXLD_USER_OR_GOT */
95
96#if KXLD_USER_OR_COMMON
97/* Initializes a zerofill section of the specified size and alignment */
98void kxld_sect_init_zerofill(KXLDSect *sect, const char *segname,
99    const char *sectname, kxld_size_t size, u_int align)
100    __attribute__((nonnull, visibility("hidden")));
101#endif /* KXLD_USER_OR_COMMON */
102
103/* Clears the section object */
104void kxld_sect_clear(KXLDSect *sect)
105    __attribute__((nonnull, visibility("hidden")));
106
107/* Denitializes the section object and frees its array of relocs */
108void kxld_sect_deinit(KXLDSect *sect)
109    __attribute__((nonnull, visibility("hidden")));
110
111/*******************************************************************************
112* Accessors
113*******************************************************************************/
114
115/* Gets the number of relocation entries in the section */
116u_int kxld_sect_get_num_relocs(const KXLDSect *sect)
117    __attribute__((pure, nonnull, visibility("hidden")));
118
119/* Returns the address parameter adjusted to the minimum alignment required by
120 * the section.
121 */
122kxld_addr_t kxld_sect_align_address(const KXLDSect *sect, kxld_addr_t address)
123    __attribute__((pure, nonnull, visibility("hidden")));
124
125/* Returns the space required by the exported Mach-O header */
126u_long kxld_sect_get_macho_header_size(boolean_t is_32_bit)
127    __attribute__((const, visibility("hidden")));
128
129/* Returns the space required by the exported Mach-O data */
130u_long kxld_sect_get_macho_data_size(const KXLDSect *sect)
131    __attribute__((pure, nonnull, visibility("hidden")));
132
133#if KXLD_USER_OR_LP64
134/* Returns the number of GOT entries required by relocation entries in the
135 * given section.
136 */
137u_int kxld_sect_get_ngots(const KXLDSect *sect,
138    const struct kxld_relocator *relocator, const struct kxld_symtab *symtab)
139    __attribute__((pure, nonnull, visibility("hidden")));
140#endif /* KXLD_USER_OR_LP64 */
141
142kern_return_t kxld_sect_export_macho_to_file_buffer(const KXLDSect *sect, u_char *buf,
143    u_long *header_offset, u_long header_size, u_long *data_offset,
144    u_long data_size, boolean_t is_32_bit)
145    __attribute__((nonnull, visibility("hidden")));
146
147kern_return_t kxld_sect_export_macho_to_vm(const KXLDSect *sect, u_char *buf,
148    u_long *header_offset, u_long header_size,
149    kxld_addr_t link_addr, u_long data_size,
150    boolean_t is_32_bit)
151    __attribute__((nonnull, visibility("hidden")));
152
153/*******************************************************************************
154* Mutators
155*******************************************************************************/
156
157/* Relocates the section to the given link address */
158void kxld_sect_relocate(KXLDSect *sect, kxld_addr_t link_addr)
159    __attribute__((nonnull, visibility("hidden")));
160
161#if KXLD_USER_OR_COMMON
162/* Adds a number of bytes to the section's size.  Returns the size of the
163 * section before it was grown.
164 */
165kxld_size_t kxld_sect_grow(KXLDSect *sect, kxld_size_t nbytes, u_int align)
166    __attribute__((nonnull, visibility("hidden")));
167#endif /* KXLD_USER_OR_COMMON */
168
169#if KXLD_USER_OR_GOT
170/* Popluates the entries of a GOT section */
171kern_return_t kxld_sect_populate_got(KXLDSect *sect, struct kxld_symtab *symtab,
172    boolean_t swap)
173    __attribute__((nonnull, visibility("hidden")));
174#endif /* KXLD_USER_OR_GOT */
175
176/* Processes all of a section's relocation entries */
177kern_return_t kxld_sect_process_relocs(KXLDSect *sect,
178    struct kxld_relocator *relocator)
179    __attribute__((nonnull, visibility("hidden")));
180
181#endif /* _KXLD_SECT_H_ */
182
183