| 1 | /* |
| 2 | * Copyright (c) 2019, Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included |
| 12 | * in all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | * OTHER DEALINGS IN THE SOFTWARE. |
| 21 | */ |
| 22 | // CM Fast Composite Linking library. |
| 23 | // |
| 24 | |
| 25 | #include <cstring> |
| 26 | |
| 27 | #include "cm_fc_ld.h" |
| 28 | |
| 29 | #include "PatchInfoLinker.h" |
| 30 | #include "PatchInfoReader.h" |
| 31 | #include "PatchInfoRecord.h" |
| 32 | |
| 33 | |
| 34 | int cm_fc_get_callee_info(const char *buf, size_t size, |
| 35 | void *c, |
| 36 | cm_fc_link_type *out_link_type) { |
| 37 | // Read patch info. |
| 38 | if (readPatchInfo(buf, size, *((cm::patch::Collection *)c))) |
| 39 | return CM_FC_FAILURE; |
| 40 | |
| 41 | cm::patch::Collection *C = reinterpret_cast<cm::patch::Collection *>(c); |
| 42 | |
| 43 | // Bail out if there's no symbol. |
| 44 | if (C->sym_begin() == C->sym_end()) |
| 45 | return CM_FC_FAILURE; |
| 46 | // Bail out if there's no binary. |
| 47 | if (C->bin_begin() == C->bin_end()) |
| 48 | return CM_FC_FAILURE; |
| 49 | |
| 50 | cm::patch::Binary &Bin = *(C->bin_begin()); |
| 51 | |
| 52 | cm::patch::Symbol *S = nullptr; |
| 53 | for (auto I = C->sym_begin(), E = C->sym_end(); I != E; ++I) |
| 54 | if (I->getBinary() == &Bin) { |
| 55 | S = &*I; |
| 56 | } |
| 57 | if (!S) |
| 58 | return CM_FC_FAILURE; |
| 59 | |
| 60 | unsigned LT = S->getExtra() & 0x3; |
| 61 | if (LT >= 3) |
| 62 | return CM_FC_FAILURE; |
| 63 | *out_link_type = cm_fc_link_type(LT); |
| 64 | |
| 65 | if (Bin.rel_begin() == Bin.rel_end()) { |
| 66 | // If there is no relocation, there's no callee info to return. |
| 67 | return CM_FC_FAILURE; |
| 68 | } |
| 69 | |
| 70 | return CM_FC_OK; |
| 71 | } |
| 72 | |
| 73 | int cm_fc_combine_kernels(size_t num_kernels, cm_fc_kernel_t kernels[], |
| 74 | char *out_buf, size_t *out_size, |
| 75 | const char *options) { |
| 76 | if (!out_buf || !out_size) |
| 77 | return CM_FC_FAILURE; |
| 78 | |
| 79 | cm::patch::Collection C; |
| 80 | if (linkPatchInfo(C, num_kernels, kernels, options)) |
| 81 | return CM_FC_FAILURE; |
| 82 | |
| 83 | const std::string &B = C.getLinkedBinary(); |
| 84 | if (B.size() > *out_size) { |
| 85 | *out_size = B.size(); |
| 86 | return CM_FC_NOBUFS; |
| 87 | } |
| 88 | |
| 89 | std::memcpy(out_buf, B.data(), B.size()); |
| 90 | *out_size = B.size(); |
| 91 | |
| 92 | return CM_FC_OK; |
| 93 | } |