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 | /* * Copyright (c) 2017 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 "Defines.h" #include <stdint.h> #include <stdio.h> #include <string.h> #include <assert.h> #include "FunctionVariants.h" namespace mach_o { // // MARK: --- FunctionVariantsRuntimeTable methods --- // uint32_t FunctionVariantsRuntimeTable::forEachVariant(void (^callback)(FunctionVariantsRuntimeTable::Kind kind, uint32_t impl, bool implIsTableIndex, std::span<const uint8_t> flagIndexes, bool& stop)) const { bool stop = false; for (uint32_t i=0; i < this->count; ++i) { int flagCount = 0; const uint8_t* flagBitNums = this->entries[i].flagBitNums; for (int f=0; f < 4; ++f) { if ( flagBitNums[f] != 0 ) flagCount = f+1; } std::span<const uint8_t> flags(flagBitNums, flagCount); callback(this->kind, this->entries[i].impl, this->entries[i].anotherTable, flags, stop); if ( stop ) break; } return (uint32_t)offsetof(FunctionVariantsRuntimeTable, entries[this->count]); } uint32_t FunctionVariantsRuntimeTable::size() const { return (uint32_t)offsetof(FunctionVariantsRuntimeTable, entries[this->count]); } Error FunctionVariantsRuntimeTable::valid(size_t length) const { // verify kind is known switch ( this->kind ) { case Kind::perProcess: case Kind::systemWide: case Kind::arm64: case Kind::x86_64: break; default: return Error("unknown FunctionVariantsRuntimeTable::Kind (%d)", this->kind); } // verify length size_t actualSize = offsetof(FunctionVariantsRuntimeTable, entries[this->count]); if ( (actualSize != length) && (actualSize != length-4) ) // last entry's size may be rounded up to align linkedit blob return Error("invalid FunctionVariantsRuntimeTable length %lu for count=%u", length, this->count); // verify "default" is last if ( this->entries[this->count - 1].flagBitNums[0] != 0 ) return Error("last entry in FunctionVariantsRuntimeTable entries is not 'default'"); return Error::none(); } // // MARK: --- FunctionVariants methods --- // FunctionVariants::FunctionVariants(std::span<const uint8_t> linkeditBytes) : _bytes(linkeditBytes) { } Error FunctionVariants::valid() const { const OnDiskFormat* p = header(); if ( p == nullptr ) return Error("FunctionVariants is too small"); if ( offsetof(OnDiskFormat, tableOffsets[p->tableCount]) >= _bytes.size() ) return Error("FunctionVariants tableCount=%u is too large for size=%lu", p->tableCount, _bytes.size()); // verify layout each entry is in increasing order and fits in byte range for (uint32_t i=0; i < p->tableCount; ++i) { uint32_t offsetInBlob = p->tableOffsets[i]; if ( offsetInBlob > _bytes.size() ) return Error("tableOffsets[%d]=0x%08X which is > total size 0x%08lX", i, p->tableOffsets[i], _bytes.size()); const FunctionVariantsRuntimeTable* fvrt = (FunctionVariantsRuntimeTable*)(&_bytes[offsetInBlob]); if ( offsetInBlob+fvrt->size() > _bytes.size() ) return Error("entry %d extends to 0x%08X which beyond total size 0x%08lX", i, offsetInBlob+fvrt->size(), _bytes.size()); } // verify each entry for (uint32_t i=0; i < p->tableCount; ++i) { const FunctionVariantsRuntimeTable* entry = this->entry(i); uint32_t length; if ( i < (p->tableCount - 1) ) length = p->tableOffsets[i+1] - p->tableOffsets[i]; else length = (uint32_t)_bytes.size() - p->tableOffsets[i]; if ( Error err = entry->valid(length) ) return err; } return Error::none(); } uint32_t FunctionVariants::count() const { if ( const OnDiskFormat* p = header() ) return p->tableCount; return 0; } FunctionVariants::OnDiskFormat* FunctionVariants::header() const { if ( _bytes.size() < sizeof(OnDiskFormat) ) return nullptr; return (OnDiskFormat*)_bytes.data(); } const FunctionVariantsRuntimeTable* FunctionVariants::entry(uint32_t index) const { if ( index < count() ) { if ( const OnDiskFormat* p = header() ) { uint32_t offsetInBlob = p->tableOffsets[index]; if ( offsetInBlob < _bytes.size() ) return (FunctionVariantsRuntimeTable*)(&_bytes[offsetInBlob]); } } return nullptr; } // // MARK: --- FunctionVariantFixups methods --- // FunctionVariantFixups::FunctionVariantFixups(std::span<const uint8_t> linkeditBytes) { _fixups = std::span<const InternalFixup>((InternalFixup*)linkeditBytes.data(), linkeditBytes.size()/sizeof(InternalFixup)); } void FunctionVariantFixups::forEachFixup(void (^callback)(InternalFixup fixupInfo)) const { for ( InternalFixup fixup : _fixups ) callback(fixup); } } // namespace mach_o |