1/*
  2 * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
  3 * 
  4 * @APPLE_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. Please obtain a copy of the License at
 10 * http://www.opensource.apple.com/apsl/ and read it before using this
 11 * file.
 12 * 
 13 * The Original Code and all software distributed under the License are
 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 18 * Please see the License for the specific language governing rights and
 19 * limitations under the License.
 20 * 
 21 * @APPLE_LICENSE_HEADER_END@
 22 */
 23#ifndef _MACHO_NLIST_H_
 24#define _MACHO_NLIST_H_
 25/*	$NetBSD: nlist.h,v 1.5 1994/10/26 00:56:11 cgd Exp $	*/
 26
 27/*-
 28 * Copyright (c) 1991, 1993
 29 *	The Regents of the University of California.  All rights reserved.
 30 * (c) UNIX System Laboratories, Inc.
 31 * All or some portions of this file are derived from material licensed
 32 * to the University of California by American Telephone and Telegraph
 33 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
 34 * the permission of UNIX System Laboratories, Inc.
 35 *
 36 * Redistribution and use in source and binary forms, with or without
 37 * modification, are permitted provided that the following conditions
 38 * are met:
 39 * 1. Redistributions of source code must retain the above copyright
 40 *    notice, this list of conditions and the following disclaimer.
 41 * 2. Redistributions in binary form must reproduce the above copyright
 42 *    notice, this list of conditions and the following disclaimer in the
 43 *    documentation and/or other materials provided with the distribution.
 44 * 3. All advertising materials mentioning features or use of this software
 45 *    must display the following acknowledgement:
 46 *	This product includes software developed by the University of
 47 *	California, Berkeley and its contributors.
 48 * 4. Neither the name of the University nor the names of its contributors
 49 *    may be used to endorse or promote products derived from this software
 50 *    without specific prior written permission.
 51 *
 52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 55 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 62 * SUCH DAMAGE.
 63 *
 64 *	@(#)nlist.h	8.2 (Berkeley) 1/21/94
 65 */
 66#include <stdint.h>
 67
 68/*
 69 * Format of a symbol table entry of a Mach-O file for 32-bit architectures.
 70 * Modified from the BSD format.  The modifications from the original format
 71 * were changing n_other (an unused field) to n_sect and the addition of the
 72 * N_SECT type.  These modifications are required to support symbols in a larger
 73 * number of sections not just the three sections (text, data and bss) in a BSD
 74 * file.
 75 */
 76struct nlist {
 77	union {
 78#ifndef __LP64__
 79		char *n_name;	/* for use when in-core */
 80#endif
 81		uint32_t n_strx;	/* index into the string table */
 82	} n_un;
 83	uint8_t n_type;		/* type flag, see below */
 84	uint8_t n_sect;		/* section number or NO_SECT */
 85	int16_t n_desc;		/* see <mach-o/stab.h> */
 86	uint32_t n_value;	/* value of this symbol (or stab offset) */
 87};
 88
 89/*
 90 * This is the symbol table entry structure for 64-bit architectures.
 91 */
 92struct nlist_64 {
 93    union {
 94        uint32_t  n_strx; /* index into the string table */
 95    } n_un;
 96    uint8_t n_type;        /* type flag, see below */
 97    uint8_t n_sect;        /* section number or NO_SECT */
 98    uint16_t n_desc;       /* see <mach-o/stab.h> */
 99    uint64_t n_value;      /* value of this symbol (or stab offset) */
100};
101
102/*
103 * Symbols with a index into the string table of zero (n_un.n_strx == 0) are
104 * defined to have a null, "", name.  Therefore all string indexes to non null
105 * names must not have a zero string index.  This is bit historical information
106 * that has never been well documented.
107 */
108
109/*
110 * The n_type field really contains four fields:
111 *	unsigned char N_STAB:3,
112 *		      N_PEXT:1,
113 *		      N_TYPE:3,
114 *		      N_EXT:1;
115 * which are used via the following masks.
116 */
117#define	N_STAB	0xe0  /* if any of these bits set, a symbolic debugging entry */
118#define	N_PEXT	0x10  /* private external symbol bit */
119#define	N_TYPE	0x0e  /* mask for the type bits */
120#define	N_EXT	0x01  /* external symbol bit, set for external symbols */
121
122/*
123 * Only symbolic debugging entries have some of the N_STAB bits set and if any
124 * of these bits are set then it is a symbolic debugging entry (a stab).  In
125 * which case then the values of the n_type field (the entire field) are given
126 * in <mach-o/stab.h>
127 */
128
129/*
130 * Values for N_TYPE bits of the n_type field.
131 */
132#define	N_UNDF	0x0		/* undefined, n_sect == NO_SECT */
133#define	N_ABS	0x2		/* absolute, n_sect == NO_SECT */
134#define	N_SECT	0xe		/* defined in section number n_sect */
135#define	N_PBUD	0xc		/* prebound undefined (defined in a dylib) */
136#define N_INDR	0xa		/* indirect */
137
138/* 
139 * If the type is N_INDR then the symbol is defined to be the same as another
140 * symbol.  In this case the n_value field is an index into the string table
141 * of the other symbol's name.  When the other symbol is defined then they both
142 * take on the defined type and value.
143 */
144
145/*
146 * If the type is N_SECT then the n_sect field contains an ordinal of the
147 * section the symbol is defined in.  The sections are numbered from 1 and 
148 * refer to sections in order they appear in the load commands for the file
149 * they are in.  This means the same ordinal may very well refer to different
150 * sections in different files.
151 *
152 * The n_value field for all symbol table entries (including N_STAB's) gets
153 * updated by the link editor based on the value of it's n_sect field and where
154 * the section n_sect references gets relocated.  If the value of the n_sect 
155 * field is NO_SECT then it's n_value field is not changed by the link editor.
156 */
157#define	NO_SECT		0	/* symbol is not in any section */
158#define MAX_SECT	255	/* 1 thru 255 inclusive */
159
160/*
161 * Common symbols are represented by undefined (N_UNDF) external (N_EXT) types
162 * who's values (n_value) are non-zero.  In which case the value of the n_value
163 * field is the size (in bytes) of the common symbol.  The n_sect field is set
164 * to NO_SECT.  The alignment of a common symbol may be set as a power of 2
165 * between 2^1 and 2^15 as part of the n_desc field using the macros below. If
166 * the alignment is not set (a value of zero) then natural alignment based on
167 * the size is used.
168 */
169#define GET_COMM_ALIGN(n_desc) (((n_desc) >> 8) & 0x0f)
170#define SET_COMM_ALIGN(n_desc,align) \
171    (n_desc) = (((n_desc) & 0xf0ff) | (((align) & 0x0f) << 8))
172
173/*
174 * To support the lazy binding of undefined symbols in the dynamic link-editor,
175 * the undefined symbols in the symbol table (the nlist structures) are marked
176 * with the indication if the undefined reference is a lazy reference or
177 * non-lazy reference.  If both a non-lazy reference and a lazy reference is
178 * made to the same symbol the non-lazy reference takes precedence.  A reference
179 * is lazy only when all references to that symbol are made through a symbol
180 * pointer in a lazy symbol pointer section.
181 *
182 * The implementation of marking nlist structures in the symbol table for
183 * undefined symbols will be to use some of the bits of the n_desc field as a
184 * reference type.  The mask REFERENCE_TYPE will be applied to the n_desc field
185 * of an nlist structure for an undefined symbol to determine the type of
186 * undefined reference (lazy or non-lazy).
187 *
188 * The constants for the REFERENCE FLAGS are propagated to the reference table
189 * in a shared library file.  In that case the constant for a defined symbol,
190 * REFERENCE_FLAG_DEFINED, is also used.
191 */
192/* Reference type bits of the n_desc field of undefined symbols */
193#define REFERENCE_TYPE				0x7
194/* types of references */
195#define REFERENCE_FLAG_UNDEFINED_NON_LAZY		0
196#define REFERENCE_FLAG_UNDEFINED_LAZY			1
197#define REFERENCE_FLAG_DEFINED				2
198#define REFERENCE_FLAG_PRIVATE_DEFINED			3
199#define REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY	4
200#define REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY		5
201
202/*
203 * To simplify stripping of objects that use are used with the dynamic link
204 * editor, the static link editor marks the symbols defined an object that are
205 * referenced by a dynamically bound object (dynamic shared libraries, bundles).
206 * With this marking strip knows not to strip these symbols.
207 */
208#define REFERENCED_DYNAMICALLY	0x0010
209
210/*
211 * For images created by the static link editor with the -twolevel_namespace
212 * option in effect the flags field of the mach header is marked with
213 * MH_TWOLEVEL.  And the binding of the undefined references of the image are
214 * determined by the static link editor.  Which library an undefined symbol is
215 * bound to is recorded by the static linker in the high 8 bits of the n_desc
216 * field using the SET_LIBRARY_ORDINAL macro below.  The ordinal recorded
217 * references the libraries listed in the Mach-O's LC_LOAD_DYLIB,
218 * LC_LOAD_WEAK_DYLIB, LC_REEXPORT_DYLIB, LC_LOAD_UPWARD_DYLIB, and
219 * LC_LAZY_LOAD_DYLIB, etc. load commands in the order they appear in the
220 * headers.   The library ordinals start from 1.
221 * For a dynamic library that is built as a two-level namespace image the
222 * undefined references from module defined in another use the same nlist struct
223 * an in that case SELF_LIBRARY_ORDINAL is used as the library ordinal.  For
224 * defined symbols in all images they also must have the library ordinal set to
225 * SELF_LIBRARY_ORDINAL.  The EXECUTABLE_ORDINAL refers to the executable
226 * image for references from plugins that refer to the executable that loads
227 * them.
228 * 
229 * The DYNAMIC_LOOKUP_ORDINAL is for undefined symbols in a two-level namespace
230 * image that are looked up by the dynamic linker with flat namespace semantics.
231 * This ordinal was added as a feature in Mac OS X 10.3 by reducing the
232 * value of MAX_LIBRARY_ORDINAL by one.  So it is legal for existing binaries
233 * or binaries built with older tools to have 0xfe (254) dynamic libraries.  In
234 * this case the ordinal value 0xfe (254) must be treated as a library ordinal
235 * for compatibility. 
236 */
237#define GET_LIBRARY_ORDINAL(n_desc) (((n_desc) >> 8) & 0xff)
238#define SET_LIBRARY_ORDINAL(n_desc,ordinal) \
239	(n_desc) = (((n_desc) & 0x00ff) | (((ordinal) & 0xff) << 8))
240#define SELF_LIBRARY_ORDINAL 0x0
241#define MAX_LIBRARY_ORDINAL 0xfd
242#define DYNAMIC_LOOKUP_ORDINAL 0xfe
243#define EXECUTABLE_ORDINAL 0xff
244
245/*
246 * The bit 0x0020 of the n_desc field is used for two non-overlapping purposes
247 * and has two different symbolic names, N_NO_DEAD_STRIP and N_DESC_DISCARDED.
248 */
249
250/*
251 * The N_NO_DEAD_STRIP bit of the n_desc field only ever appears in a 
252 * relocatable .o file (MH_OBJECT filetype). And is used to indicate to the
253 * static link editor it is never to dead strip the symbol.
254 */
255#define N_NO_DEAD_STRIP 0x0020 /* symbol is not to be dead stripped */
256
257/*
258 * The N_DESC_DISCARDED bit of the n_desc field never appears in linked image.
259 * But is used in very rare cases by the dynamic link editor to mark an in
260 * memory symbol as discared and longer used for linking.
261 */
262#define N_DESC_DISCARDED 0x0020	/* symbol is discarded */
263
264/*
265 * The N_WEAK_REF bit of the n_desc field indicates to the dynamic linker that
266 * the undefined symbol is allowed to be missing and is to have the address of
267 * zero when missing.
268 */
269#define N_WEAK_REF	0x0040 /* symbol is weak referenced */
270
271/*
272 * The N_WEAK_DEF bit of the n_desc field indicates to the static and dynamic
273 * linkers that the symbol definition is weak, allowing a non-weak symbol to
274 * also be used which causes the weak definition to be discared.  Currently this
275 * is only supported for symbols in coalesed sections.
276 */
277#define N_WEAK_DEF	0x0080 /* coalesed symbol is a weak definition */
278
279/*
280 * The N_REF_TO_WEAK bit of the n_desc field indicates to the dynamic linker
281 * that the undefined symbol should be resolved using flat namespace searching.
282 */
283#define	N_REF_TO_WEAK	0x0080 /* reference to a weak symbol */
284
285/*
286 * The N_ARM_THUMB_DEF bit of the n_desc field indicates that the symbol is
287 * a defintion of a Thumb function.
288 */
289#define N_ARM_THUMB_DEF	0x0008 /* symbol is a Thumb function (ARM) */
290
291/*
292 * The N_SYMBOL_RESOLVER bit of the n_desc field indicates that the
293 * that the function is actually a resolver function and should
294 * be called to get the address of the real function to use.
295 * This bit is only available in .o files (MH_OBJECT filetype)
296 */
297#define N_SYMBOL_RESOLVER  0x0100 
298
299/*
300 * The N_ALT_ENTRY bit of the n_desc field indicates that the
301 * symbol is pinned to the previous content.
302 */
303#define N_ALT_ENTRY 0x0200
304
305/*
306 * The N_COLD_FUNC bit of the n_desc field indicates that the symbol is used
307 * infrequently and the linker should order it towards the end of the section.
308 */
309#define N_COLD_FUNC 0x0400
310
311#ifndef __STRICT_BSD__
312#ifdef __cplusplus
313extern "C" {
314#endif /* __cplusplus */
315/*
316 * The function nlist(3) from the C library.
317 */
318extern int nlist (const char *filename, struct nlist *list);
319#ifdef __cplusplus
320}
321#endif /* __cplusplus */
322#endif /* __STRICT_BSD__ */
323
324#endif /* _MACHO_LIST_H_ */