Loading...
  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
/*
 * Copyright (c) 2021 Apple Inc. All rights reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_LICENSE_HEADER_END@
 */

#include <sys/types.h>
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>

#include <mach-o/nlist.h>

#include "Symbol.h"


namespace mach_o {


//
// MARK: --- Symbol methods ---
//
Symbol Symbol::makeRegularExport(CString name, uint64_t imageOffset, uint8_t sectNum, bool dontDeadStrip, bool cold, bool neverStrip, bool isThumb)
{
    Symbol symbol(name);
    symbol._kind        = Kind::regular;
    symbol._sectOrdinal = sectNum;
    symbol._scope       = neverStrip ? Scope::globalNeverStrip : Scope::global;
    symbol._implOffset  = imageOffset;
    if ( dontDeadStrip )
        symbol.setDontDeadStrip();
    if ( cold )
        symbol.setCold();
    if ( isThumb )
        symbol.setIsThumb();
    return symbol;
}

Symbol Symbol::makeRegularHidden(CString name, uint64_t imageOffset, uint8_t sectNum, bool dontDeadStrip, bool cold, bool isThumb)
{
    Symbol symbol(name);
    symbol._kind        = Kind::regular;
    symbol._sectOrdinal = sectNum;
    symbol._scope       = Scope::linkageUnit;
    symbol._implOffset  = imageOffset;
    if ( dontDeadStrip )
        symbol.setDontDeadStrip();
    if ( cold )
        symbol.setCold();
    if ( isThumb )
        symbol.setIsThumb();
    return symbol;
}

Symbol Symbol::makeRegularLocal(CString name, uint64_t imageOffset, uint8_t sectNum, bool dontDeadStrip, bool cold, bool isThumb)
{
    Symbol symbol(name);
    symbol._kind        = Kind::regular;
    symbol._sectOrdinal = sectNum;
    symbol._scope       = Scope::translationUnit;
    symbol._implOffset  = imageOffset;
    if ( dontDeadStrip )
        symbol.setDontDeadStrip();
    if ( cold )
        symbol.setCold();
    if ( isThumb )
        symbol.setIsThumb();
    return symbol;
}

Symbol Symbol::makeRegularWasPrivateExtern(CString name, uint64_t imageOffset, uint8_t sectNum, bool dontDeadStrip, bool cold, bool isThumb)
{
    Symbol symbol(name);
    symbol._kind        = Kind::regular;
    symbol._sectOrdinal = sectNum;
    symbol._scope       = Scope::wasLinkageUnit;
    symbol._implOffset  = imageOffset;
    if ( dontDeadStrip )
        symbol.setDontDeadStrip();
    if ( cold )
        symbol.setCold();
    if ( isThumb )
        symbol.setIsThumb();
    return symbol;
}

Symbol Symbol::makeWeakDefExport(CString name, uint64_t imageOffset, uint8_t sectOrd, bool dontDeadStrip, bool cold, bool isThumb)
{
    Symbol symbol(name);
    symbol._kind        = Kind::regular;
    symbol._sectOrdinal = sectOrd;
    symbol._scope       = Scope::global;
    symbol._weakDef     = true;
    symbol._implOffset  = imageOffset;
    if ( dontDeadStrip )
        symbol.setDontDeadStrip();
    if ( cold )
        symbol.setCold();
    if ( isThumb )
        symbol.setIsThumb();
    return symbol;
}

Symbol Symbol::makeWeakDefAutoHide(CString name, uint64_t imageOffset, uint8_t sectOrd, bool dontDeadStrip, bool cold, bool isThumb)
{
    Symbol symbol(name);
    symbol._kind        = Kind::regular;
    symbol._sectOrdinal = sectOrd;
    symbol._scope       = Scope::autoHide;
    symbol._weakDef     = true;
    symbol._implOffset  = imageOffset;
    if ( dontDeadStrip )
        symbol.setDontDeadStrip();
    if ( cold )
        symbol.setCold();
    if ( isThumb )
        symbol.setIsThumb();
    return symbol;
}

Symbol Symbol::makeWeakDefHidden(CString name, uint64_t imageOffset, uint8_t sectOrd, bool dontDeadStrip, bool cold, bool isThumb)
{
    Symbol symbol(name);
    symbol._kind        = Kind::regular;
    symbol._sectOrdinal = sectOrd;
    symbol._scope       = Scope::linkageUnit;
    symbol._weakDef     = true;
    symbol._implOffset  = imageOffset;
    if ( dontDeadStrip )
        symbol.setDontDeadStrip();
    if ( cold )
        symbol.setCold();
    if ( isThumb )
        symbol.setIsThumb();
    return symbol;
}

Symbol Symbol::makeWeakDefWasPrivateExtern(CString name, uint64_t imageOffset, uint8_t sectOrd, bool dontDeadStrip, bool cold, bool isThumb)
{
    Symbol symbol(name);
    symbol._kind        = Kind::regular;
    symbol._sectOrdinal = sectOrd;
    symbol._scope       = Scope::wasLinkageUnit;
    symbol._weakDef     = true;
    symbol._implOffset  = imageOffset;
    if ( dontDeadStrip )
        symbol.setDontDeadStrip();
    if ( cold )
        symbol.setCold();
    if ( isThumb )
        symbol.setIsThumb();
    return symbol;
}

Symbol Symbol::makeAltEntry(CString name, uint64_t imageOffset, uint8_t sectOrd, Scope scope, bool dontDeadStrip, bool cold, bool weakDef, bool isThumb)
{
    Symbol symbol(name);
    symbol._kind        = Kind::altEntry;
    symbol._sectOrdinal = sectOrd;
    symbol._scope       = scope;
    symbol._weakDef     = weakDef;
    symbol._implOffset  = imageOffset;
    if ( dontDeadStrip )
        symbol.setDontDeadStrip();
    if ( cold )
        symbol.setCold();
    if ( isThumb )
        symbol.setIsThumb();
    return symbol;
}

Symbol Symbol::makeDynamicResolver(CString name, uint8_t sectNum, uint64_t stubImageOffset, uint64_t funcImageOffset, Symbol::Scope scope)
{
    // FIXME: do we need to support non-exported resolver functions?
    Symbol symbol(name);
    symbol._kind        = Kind::resolver;
    symbol._scope       = scope;
    symbol._sectOrdinal = sectNum;
    symbol._implOffset  = funcImageOffset;
    symbol._u.resolverStubOffset = stubImageOffset;
    return symbol;
}

Symbol Symbol::makeFunctionVariantExport(CString name, uint8_t sectNumOfDefault, uint64_t imageOffsetOfDefault, uint32_t functionVariantTableIndex)
{
    Symbol symbol(name);
    symbol._kind        = Kind::functionVariant;
    symbol._scope       = Scope::global;
    symbol._sectOrdinal = sectNumOfDefault;
    symbol._implOffset  = imageOffsetOfDefault;
    symbol._u.functionVariantTableIndex = functionVariantTableIndex;
    return symbol;
}

Symbol Symbol::makeThreadLocalExport(CString name, uint64_t imageOffset, uint8_t sectOrd, bool dontDeadStrip, bool cold, bool weakDef)
{
    Symbol symbol(name);
    symbol._kind        = Kind::threadLocal;
    symbol._scope       = Scope::global;
    symbol._sectOrdinal = sectOrd;
    symbol._implOffset  = imageOffset;
    symbol._weakDef     = weakDef;
    if ( dontDeadStrip )
        symbol.setDontDeadStrip();
    if ( cold )
        symbol.setCold();
    return symbol;
}

Symbol Symbol::makeAbsolute(CString name, uint64_t address, bool dontDeadStrip, Scope scope, uint8_t sectNum)
{
    Symbol symbol(name);
    symbol._kind        = Kind::absolute;
    symbol._scope       = scope;
    symbol._implOffset  = address;
    symbol._sectOrdinal = sectNum;
    if ( dontDeadStrip )
        symbol.setDontDeadStrip();
    return symbol;
}

Symbol Symbol::makeReExport(CString name, int libOrdinal, const char* importName, Symbol::Scope scope)
{
    Symbol symbol(name);
    symbol._kind         = Kind::reExport;
    symbol._scope        = scope;
    symbol._implOffset   = libOrdinal;
    symbol._u.importName = importName;
    return symbol;
}

Symbol Symbol::makeUndefined(CString name, int libOrdinal, bool weakImport)
{
    Symbol symbol(name);
    symbol._kind         = Kind::undefine;
    symbol._scope        = Scope::global;
    symbol._implOffset   = libOrdinal;
    symbol._weakImport   = weakImport;
    return symbol;
}

Symbol Symbol::makeTentativeDef(CString name, uint64_t size, uint8_t alignP2, bool dontDeadStrip, bool cold)
{
    Symbol symbol(name);
    symbol._kind         = Kind::tentative;
    symbol._scope        = Scope::global;
    symbol._sectOrdinal  = alignP2;         // sectOrdinal is not used for tent-def, so stuff alignment there
    symbol._implOffset   = size;
    if ( dontDeadStrip )
        symbol.setDontDeadStrip();
    if ( cold )
        symbol.setCold();
    return symbol;
}

Symbol Symbol::makeHiddenTentativeDef(CString name, uint64_t size, uint8_t alignP2, bool dontDeadStrip, bool cold)
{
    Symbol symbol(name);
    symbol._kind         = Kind::tentative;
    symbol._scope        = Scope::linkageUnit;
    symbol._sectOrdinal  = alignP2;        // sectOrdinal is not used for tent-def, so stuff alignment there
    symbol._implOffset   = size;
    if ( dontDeadStrip )
        symbol.setDontDeadStrip();
    if ( cold )
        symbol.setCold();
    return symbol;
}

bool Symbol::operator==(const Symbol& other) const
{
    if ( _name != other._name )
        return false;
    if ( _implOffset != other._implOffset )
        return false;
    if ( _kind != other._kind )
        return false;
    if ( _scope != other._scope )
        return false;
    if ( _weakDef != other._weakDef )
        return false;
    if ( _kind == Kind::reExport ) {
        if ( (_u.importName != nullptr) && (other._u.importName != nullptr) && (strcmp(_u.importName, other._u.importName) != 0) )
            return false;
        if ( _u.importName != other._u.importName )
            return false;
    }
    else if ( _kind == Kind::resolver ) {
        if ( _u.resolverStubOffset != other._u.resolverStubOffset )
            return false;
    }
    return true;
}


uint64_t Symbol::implOffset() const
{
    assert((_kind != Kind::reExport) && (_kind != Kind::absolute));
    return _implOffset;
}

bool Symbol::isDynamicResolver(uint64_t& resolverStubOffset) const
{
    if ( _kind != Kind::resolver )
        return false;
    resolverStubOffset = _u.resolverStubOffset;
    return true;
}

bool Symbol::isFunctionVariant(uint32_t& functionVariantTableIndex) const
{
    if ( _kind != Kind::functionVariant )
        return false;
    functionVariantTableIndex = _u.functionVariantTableIndex;
    return true;
}

bool Symbol::isReExport(int& libOrdinal, const char*& importName) const
{
    if ( _kind != Kind::reExport )
        return false;
    libOrdinal = (int)_implOffset;
    importName = (_u.importName != nullptr) ? _u.importName : _name.c_str();
    return true;
}

bool Symbol::isAbsolute(uint64_t& absAddress) const
{
    if ( _kind != Kind::absolute )
        return false;
    absAddress = _implOffset;
    return true;
}

bool Symbol::isUndefined() const
{
    return _kind == Kind::undefine;
}

bool Symbol::isUndefined(int& libOrdinal, bool& weakImport) const
{
    if ( _kind != Kind::undefine )
        return false;
    libOrdinal = (int)_implOffset;
    weakImport = _weakImport;
    return true;
}

bool Symbol::isRegular(uint64_t& implOffset) const
{
    if ( _kind != Kind::regular )
        return false;
    implOffset = _implOffset;
    return true;
}

bool Symbol::isThreadLocal(uint64_t& implOffset) const
{
    if ( _kind != Kind::threadLocal )
        return false;
    implOffset = _implOffset;
    return true;
}

bool Symbol::isAltEntry(uint64_t& implOffset) const
{
    if ( _kind != Kind::altEntry )
        return false;
    implOffset = _implOffset;
    return true;
}

bool Symbol::isTentativeDef() const
{
    return _kind == Kind::tentative;
}

bool Symbol::isTentativeDef(uint64_t& size, uint8_t& p2align) const
{
    if ( _kind != Kind::tentative )
        return false;
    size    =_implOffset;
    p2align = _sectOrdinal;    // sectOrdinal not used by tent-def, so alignment was stored there
    return true;
}

void Symbol::setName(const char* newName)
{
    _name = newName;
}

void Symbol::setimplOffset(uint64_t newOffset)
{
    _implOffset = newOffset;
}

void Symbol::changeRegularToAltEntry()
{
    assert(_kind == Kind::regular && "only regular symbols can be changed to an alt entry");
    _kind = Kind::altEntry;
}

void Symbol::changeAltEntryToRegular()
{
    assert(_kind == Kind::altEntry && "only altEntry symbols can be changed to regular");
    _kind = Kind::regular;
}
} // namespace mach_o