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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*
 * Copyright (c) 2024 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 "SharedCacheLinker.h"
#include "AtomFileFormat.h"
#include "SharedCacheLinker_private.h"

// mach-o
#include "Architecture.h"
#include "Atom.h"
#include "Containers.h"
#include "Diagnostics.h"
#include "StringUtils.h"

// ld
#include "Layout.h"
#include "AtomFileConsolidator.h"
#include "DynamicAtom.h"
#include "JSONReader.h"
#include "Linker.h"

// Darwin
#include <mach-o/loader.h>
#include <sys/mman.h>

using namespace ld;
using namespace mach_o;
using namespace json;

struct JSONHeader
{
    uint64_t            version;
    PlatformAndVersions pvs;
    Architecture        arch;
    std::string         installName;
    std::string         options;
    const Node*         atoms=nullptr;
    const Node*         dylibs=nullptr;
    const Node*         customSections=nullptr;
};

// map to fill references to dylibIndex fixups
using DylibIndexMap = StringViewMap<uint16_t>;

static const Fixup::Kind fixupKindAndSizeFromString(CString str, uint32_t& size)
{
    if ( str == "ptr64" ) {
        size = 8;
        return Fixup::Kind::ptr64;
    }

    if ( str == "ptr32" ) {
        size = 4;
        return Fixup::Kind::ptr32;
    }

    if ( str == "arm64_auth_ptr" ) {
        size = 8;
        return Fixup::Kind::arm64_auth_ptr;
    }

    if ( str == "dylibIndex" ) {
        size = 2;
        return Fixup::Kind::dylibIndex;
    }

    return Fixup::Kind::none;
}

static Atom::ContentType contentTypeFromString(CString str)
{
    if ( str == "constText" )
        return Atom::ContentType::constText;
    if ( str == "cstring" )
        return Atom::ContentType::cstringLiteral;
    if ( str == "data" )
        return Atom::ContentType::data;
    if ( str == "constData" )
        return Atom::ContentType::constData;
    if ( str == "objcData" )
        return Atom::ContentType::objcData;
    if ( str == "objcConst" )
        return Atom::ContentType::objcConst;
    if ( str == "custom" )
        return Atom::ContentType::custom;
    // ld-prime doesn't need to understand these content types (yet)
    // so this uses a custom type with a custom section
    if ( str == "pointerHashTable" )
        return Atom::ContentType::custom;
    if ( str == "pointerHashTableKey" )
        return Atom::ContentType::custom;

    return Atom::ContentType::invalid;
}


static void parseHeader(Diagnostics& diag, JSONHeader& header, const Node& rootNode)
{
    header.version = parseRequiredInt(diag, getRequiredValue(diag, rootNode, "version"));
    if ( diag.hasError() )
        return;

    if ( header.version != 1 ) {
        diag.error("JSON version not supported: %llu", header.version);
        return;
    }

    uint32_t           rawPlatform = (uint32_t)parseRequiredInt(diag, getRequiredValue(diag, rootNode, "platform"));
    const std::string& rawPlatformVersion = parseRequiredString(diag, getRequiredValue(diag, rootNode, "platformVersion"));
    const std::string& rawArch = parseRequiredString(diag, getRequiredValue(diag, rootNode, "arch"));
    header.installName  = parseRequiredString(diag, getRequiredValue(diag, rootNode, "installName"));
    if ( diag.hasError() )
        return;

    header.arch = Architecture::byName(rawArch);
    if ( header.arch == Architecture::invalid  ) {
        diag.error("%s is not a valid architecture name", rawArch.c_str());
        return;
    }

    Platform platform(rawPlatform);
    if ( Error err = platform.valid() ) {
        diag.error(err);
        return;
    }

    Version32 ver;
    if ( Error err = Version32::fromString(rawPlatformVersion, ver) ) {
        diag.error(err);
        return;
    }

    header.pvs.platform = platform;
    header.pvs.minOS    = ver;
    header.pvs.sdk      = ver;

    header.atoms  = &getRequiredValue(diag, rootNode, "atoms");
    header.dylibs = &getRequiredValue(diag, rootNode, "dylibs");
    header.customSections = getOptionalValue(diag, rootNode, "customSections");

    if ( const Node* node = getOptionalValue(diag, rootNode, "options") ) {
        header.options = parseRequiredString(diag, *node);
    }
}

static DynamicAtomFile* addDylib(Diagnostics& diag, File::Ordinal ordinal, AtomGroup atomGroup, const Node& node)
{
    const std::string& installName  = parseRequiredString(diag, getRequiredValue(diag, node, "installName"));
    Node        exports      = getRequiredValue(diag, node, "exports");
    if ( diag.hasError() )
        return nullptr;

    // TODO: explicit versions?
    Version32 curVer;
    Version32 compatVer;
    DynamicAtomFile* af = new DynamicAtomFile(ordinal, CString::dup(installName), atomGroup);

    __block std::vector<uint32_t> rawPlatforms;
    atomGroup.pvs.unzip(^(PlatformAndVersions pvs) {
        rawPlatforms.push_back(pvs.platform.value());
    });
    af->setDylibFileInfo(DylibFileInfo::makeDylibFileInfo(installName, curVer, compatVer, rawPlatforms, {}, {}, {}));

    DylibExportsBuilder exportsBuilder(af, curVer, compatVer);
    for ( const Node& exportNode : exports.array ) {
        const std::string& name     = parseRequiredString(diag, getRequiredValue(diag, exportNode, "name"));
        bool               weakDef  = false;

        if ( const Node* weakDefNode = getOptionalValue(diag, exportNode, "weakDef") )
            weakDef = parseRequiredBool(diag, *weakDefNode);

        if ( diag.hasError() )
            return nullptr;

        exportsBuilder.addDylibExport(name, weakDef);
    }
    exportsBuilder.finalize();
    af->setActive(false, true);
    af->reclaimAllocatorResources();
    return af;
}

static void addFixup(Diagnostics& diag, const Architecture& arch, bool usesAuthPtrs,
                     DynamicAtom* atom, uint32_t offset, Fixup::Kind kind, const Atom* targetAtom,
                     const Node* addendNode, const Node* authPtrNode)
{
    int64_t addend = 0;
    if ( addendNode )
        addend = parseRequiredInt(diag, *addendNode);

    if ( authPtrNode && kind != Fixup::Kind::arm64_auth_ptr ) {
       diag.error("only arm64_auth_ptr fixups can have 'authPtr' data");
       return;
    }

    if ( kind == Fixup::Kind::arm64_auth_ptr ) {
        uint8_t  key = 0;
        bool     addr = false;
        uint16_t diversity = 0;

        if ( !usesAuthPtrs ) {
            diag.error("arm64_auth_ptr fixup can't be used with %s architecture", arch.name());
            return;
        }

        if ( authPtrNode ) {
            if ( const Node* keyNode = getOptionalValue(diag, *authPtrNode, "key") )
                key = (uint8_t)parseRequiredInt(diag, *keyNode);
            if ( const Node* addrNode = getOptionalValue(diag, *authPtrNode, "addr") )
                addr = parseRequiredBool(diag, *addrNode);
            if ( const Node* divNode = getOptionalValue(diag, *authPtrNode, "diversity") )
                diversity = (uint16_t)parseRequiredInt(diag, *divNode);
        }

        atom->addFixupAuthPointer(offset, targetAtom, key, addr, diversity, (int32_t)addend);
        return;
    }


    // regular fixup
    atom->addFixup(kind, offset, targetAtom, addend);
}

static void addAtoms(Diagnostics& diag, DynamicAtomFile* af, const Node& atomsNode, const DylibIndexMap& dylibIndexMap,
        std::span<const DynamicCustomSection> customSections, size_t pointerHashTableSectIndex, size_t pointerHashTableKeySectIndex)
{
    assert(af->atoms().empty());

    // keep track of defined/undefined atom indexes in the atom file
    // indexes are needed to setup fixup targets
    StringViewMap<uint32_t> atomNameToTargetIndex;
    StringViewMap<uint32_t> dylibNameToTargetIndex;

    // atom defaults
    const Atom::Alignment ptrSizeAlign(af->is64() ? 3 : 2);
    Architecture          arch = af->arch();
    bool                  usesAuthPtrs = arch.usesArm64AuthPointers();

    // in first pass create all defined atoms to fill the name map
    for ( const Node& atomNode : atomsNode.array ) {
        const std::string& name = parseRequiredString(diag, getRequiredValue(diag, atomNode, "name"));
        const std::string& ctName = parseRequiredString(diag, getRequiredValue(diag, atomNode, "contentType"));

        if ( diag.hasError() )
            return;

        Atom::Scope         scope   = Atom::Scope::global;
        bool                weakDef = false;
        Atom::ContentType   ct      = contentTypeFromString(ctName);
        Atom::Alignment     align   = ptrSizeAlign;

        if ( ct == Atom::ContentType::invalid ) {
            diag.error("unknown content type: %s", ctName.c_str());
            return;
        }

        if ( const Node* alignNode = getOptionalValue(diag, atomNode, "p2align") ) {
            align = Atom::Alignment((int)parseRequiredInt(diag, *alignNode));

            if ( diag.hasError() )
                return;
        }

        if ( const Node* weakDefNode = getOptionalValue(diag, atomNode, "weakDef") )
            weakDef = parseRequiredBool(diag, *weakDefNode);

        // default to 1-byte alignment for string literals
        if ( ct == Atom::ContentType::cstringLiteral )
            align = Atom::Alignment(0);

        DynamicAtom* atom   = af->makeSymbolAtom(name, ct, scope, weakDef);
        atom->setAlignment(align);
        if ( atomNameToTargetIndex.find(atom->name().str()) != atomNameToTargetIndex.end() ) {
            diag.error("duplicate atom name: %s", name.c_str());
        } else {
            atomNameToTargetIndex[atom->name().str()] = atom->atomOrdinal();
        }

        std::optional<uint64_t> customSectIndex;
        if ( const Node* customSection = getOptionalValue(diag, atomNode, "section") ) {
            uint64_t sectIndex = parseRequiredInt(diag, *customSection);
            if ( diag.hasError() )
                return;
            customSectIndex = sectIndex;
        } else {
            if ( ctName == "pointerHashTable" ) {
                customSectIndex = pointerHashTableSectIndex;
            } else if ( ctName == "pointerHashTableKey" ) {
                customSectIndex = pointerHashTableKeySectIndex;;
            }
        }

        if ( customSectIndex ) {
            if ( *customSectIndex > customSections.size() ) {
                diag.error("Invalid section index (%llu) in atom %s, max allowed: %lu\n", *customSectIndex, name.c_str(), customSections.size());
                return;
            }

            atom->setCustomSection(customSections[*customSectIndex]);
        }
    }

    // now parse all atom contents and their fixups
    for ( size_t atomIndex = 0; atomIndex < atomsNode.array.size(); ++atomIndex ) {
        const Node&        atomNode = atomsNode.array[atomIndex];
        const Node&        contents = getRequiredValue(diag, atomNode, "contents");
        if ( diag.hasError() )
            return;

        // note: atoms vector of the atom file can be modified in this loop, so call atoms() each time
        DynamicAtom* atom = (DynamicAtom*)af->atoms()[atomIndex];
        std::vector<uint8_t> bytes;

        for ( const Node& contentEntry : contents.array ) {
            if ( contentEntry.map.empty() ) {
                // content bytes string
                std::string bytesFrag = parseRequiredString(diag, contentEntry);
                if ( bytesFrag.size() & 0x1 ) {
                    diag.error("odd length (%lu) of content hex string for atom: %s",
                               bytesFrag.size(),
                               atom->name().c_str());
                    return;
                }

                for ( size_t i = 0; i < bytesFrag.size() / 2; ++i ) {
                    uint8_t high;
                    uint8_t low;
                    if ( !hexCharToUInt(bytesFrag[i * 2], high) || !hexCharToUInt(bytesFrag[(i * 2) + 1], low) ) {
                        diag.error("invalid hex content");
                        return;
                    }

                    bytes.push_back((high << 4) | low);
                }
                continue;
            }

            // handle fixup
            uint32_t targetIndex;
            const std::string& targetName = parseRequiredString(diag, getRequiredValue(diag, contentEntry, "target"));
            const std::string& kindStr = parseRequiredString(diag, getRequiredValue(diag, contentEntry, "kind"));
            if ( diag.hasError() )
                return;

            uint32_t fixupSize = 0;
            Fixup::Kind kind = fixupKindAndSizeFromString(kindStr, fixupSize);
            if ( kind == Fixup::Kind::none ) {
                diag.error("unsupported fixup kind: %s", kindStr.c_str());
                return;
            }

            // special case dylibIndex fixups
            // use anon placeholder atoms to turn dylib index fixups into constant values
            // note: alternative could be to use real fixups, but we'd need to teach shared cache builder to patch them too then
            if ( kind == Fixup::Kind::dylibIndex ) {
                if ( auto targetIt = dylibNameToTargetIndex.find(targetName); targetIt != dylibNameToTargetIndex.end() ) {
                    targetIndex = targetIt->second;
                } else {
                    uint16_t dylibIndex = 0;
                    if ( auto dylibIt = dylibIndexMap.find(targetName); dylibIt != dylibIndexMap.end() )
                        dylibIndex = dylibIt->second;
                    else {
                        diag.error("dylib index not found: %s", targetName.c_str());
                        return;
                    }
                    Atom* target = af->makeAnonPlaceholder(dylibIndex);
                    targetIndex = target->atomOrdinal();
                }
            } else if ( auto targetIt = atomNameToTargetIndex.find(targetName); targetIt != atomNameToTargetIndex.end() ) {
                targetIndex = targetIt->second;
            } else {
                Atom* target = af->makeUndefine(targetName);
                targetIndex = target->atomOrdinal();
                atomNameToTargetIndex[target->name().str()] = targetIndex;
            }

            const Node* addendNode = getOptionalValue(diag, contentEntry, "addend");
            const Node* authPtrNode = getOptionalValue(diag, contentEntry, "authPtr");
            size_t newAtomSize = bytes.size() + fixupSize;
            atom->setContentAsZeros(newAtomSize);
            addFixup(diag, arch, usesAuthPtrs, atom, (uint32_t)bytes.size(), kind,
                     af->atoms()[targetIndex], addendNode, authPtrNode);
            if ( diag.hasError() )
                return;

            // resize atom's byte to make place for the fixup content
            bytes.resize(newAtomSize);

            if ( diag.hasError() )
                return;
        }
        atom->setRawContentBytes(bytes);
    }
}

Error linkerMakeFromJSON(Linker& linker, std::span<const char> jsonData, std::span<const char*> dylibList, const char* outputPath)
{
    // read input JSON
    Diagnostics jsonDiag;
    Node rootNode = readJSON(jsonDiag, jsonData.data(), jsonData.size(), false /* useJSON5 */);
    if ( jsonDiag.hasError() )
        return jsonDiag.toError();

    // parse JSON header to construct linker options and get root atom nodes
    JSONHeader header;
    parseHeader(jsonDiag, header, rootNode);
    if ( jsonDiag.hasError() )
        return jsonDiag.toError();

    // configure linker
    char verStr[32];
    header.pvs.minOS.toString(verStr);
    std::vector<CString> rawArgv{"-arch", header.arch.name(),
        "-platform_version", header.pvs.platform.name(), verStr, verStr,
        "-dylib", "-o", outputPath,
        "-install_name", strdup(header.installName.data()),
        "-add_lldb_no_nlist_section" // rdar://146167046 (Please add `__TEXT,__lldb_no_nlist` section to libswiftPrespecialized.dylib)
    };
    // convert raw options string into options vector
    // this only splits options by a whitespace, no special logic to escape quotes or so
    CString extraArgv = header.options;
    while ( !extraArgv.empty() ) {
        if ( auto spacePos = extraArgv.find(' '); spacePos != CString::npos ) {
            rawArgv.push_back(extraArgv.dupSubstr(0, spacePos));
            extraArgv = extraArgv.substr(spacePos + 1);
        } else {
            rawArgv.push_back(extraArgv.dup());
            extraArgv = CString();
        }
    }

    ArgVector argv(std::move(rawArgv));
    File::Ordinal baseOrdinal = argv.nextFileOrdinal();
    if ( Error err = linker.setOptions(std::move(argv)) )
        return jsonDiag.toError();

    // create atom file to hold all content atoms
    baseOrdinal = baseOrdinal.nextFileListOrdinal();
    DynamicAtomFile* af = new DynamicAtomFile(baseOrdinal, "json.o",
                                              {linker.options().output.arch, linker.options().output.pvs});

    // create dylib atom files and their exports
    bool hasLibSystem = false;
    baseOrdinal = baseOrdinal.nextFileListOrdinal();
    File::Ordinal reservedLibSystemOrdinal = baseOrdinal;

    for ( const Node& dylib : header.dylibs->array ) {
        baseOrdinal = baseOrdinal.nextFileListOrdinal();

        DynamicAtomFile* dylibAf = addDylib(jsonDiag, baseOrdinal, af->atomsGroup(), dylib);
        if ( jsonDiag.hasError() )
            return jsonDiag.toError();

        assert(dylibAf);
        linker.addAtomFile(dylibAf);

        if ( const DylibFileInfo* dylibInfo = dylibAf->dylibFileInfo() )
            hasLibSystem |= dylibInfo->installName().contains("libSystem");
    }

    // always link with libSystem
    if ( !hasLibSystem ) {
        DynamicAtomFile* libSystem = new DynamicAtomFile(reservedLibSystemOrdinal, "/usr/lib/libSystem.B.dylib", af->atomsGroup());

        __block std::vector<uint32_t> rawPlatforms;
        af->atomsGroup().pvs.unzip(^(PlatformAndVersions pvs) {
            rawPlatforms.push_back(pvs.platform.value());
        });
        libSystem->setDylibFileInfo(DylibFileInfo::makeDylibFileInfo("/usr/lib/libSystem.B.dylib",
                                    Version32(), Version32(), rawPlatforms, {}, {}, {}));
        linker.addAtomFile(libSystem);
    }

    DylibIndexMap dylibIndexMap;
    for ( uint16_t i = 0; i < dylibList.size(); ++i )
        dylibIndexMap[dylibList[i]] = i;

    std::vector<DynamicCustomSection> customSections;
    if ( header.customSections ) {
        if ( header.customSections->array.empty() ) {
            return Error("customSections can't be an empty, either add sections or remove the field entirely");
        }

        for ( const Node& sectNode : header.customSections->array ) {
            const std::string& perms = parseRequiredString(jsonDiag, getRequiredValue(jsonDiag, sectNode, "segPerms"));
            const std::string& segName = parseRequiredString(jsonDiag, getRequiredValue(jsonDiag, sectNode, "segName"));
            const std::string& sectName = parseRequiredString(jsonDiag, getRequiredValue(jsonDiag, sectNode, "sectName"));
            if ( jsonDiag.hasError() )
                return jsonDiag.toError();

            uint32_t segPerms = 0;
            if ( perms.find('r') != std::string::npos )
                segPerms |= PROT_READ;
            if ( perms.find('w') != std::string::npos )
                segPerms |= PROT_WRITE;
            if ( perms.find('x') != std::string::npos )
                segPerms |= PROT_EXEC;

            uint32_t sectionFlags = S_REGULAR;
            if ( const Node* flagsNode = getOptionalValue(jsonDiag, sectNode, "sectFlags") )
                sectionFlags = (uint32_t)parseRequiredInt(jsonDiag, *flagsNode);
            if ( jsonDiag.hasError() )
                return jsonDiag.toError();

            customSections.push_back(af->makeCustomSection(segPerms, sectionFlags, strdup(segName.c_str()), strdup(sectName.c_str())));
        }
    }

    // reserve slots for pointer hash table sections
    size_t pointerHashTableSectIndex = customSections.size();
    customSections.push_back(af->makeCustomSection(PROT_READ | PROT_WRITE, S_REGULAR, "__DATA_CONST", "__ptrhashtab"));
    size_t pointerHashTableKeySectIndex = customSections.size();
    customSections.push_back(af->makeCustomSection(PROT_READ | PROT_WRITE, S_REGULAR, "__DATA_CONST", "__ptrhashtabkey"));

    // add all content atoms
    addAtoms(jsonDiag, af, *header.atoms, dylibIndexMap, customSections, pointerHashTableSectIndex, pointerHashTableKeySectIndex);
    if ( jsonDiag.hasError() )
        return jsonDiag.toError();

    linker.addAtomFile(af);
    return Error::none();
}

const char* ldMakeDylibFromJSON(std::span<const char> jsonData, std::span<const char*> dylibList, const char* outputPath)
{
    Linker linker;

    if ( Error err = linkerMakeFromJSON(linker, jsonData, dylibList, outputPath) )
        return strdup(err.message());

    if ( Error err = linker.run() )
        return strdup(err.message());

    return nullptr;
}