1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #pragma once
30
31 #include <link.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34
35 #include <utility>
36 #include <vector>
37
38 #include "linker_common_types.h"
39 #include "linker_globals.h"
40 #include "linker_soinfo.h"
41
42 static constexpr ElfW(Versym) kVersymHiddenBit = 0x8000;
43
44 enum RelocationKind {
45 kRelocAbsolute = 0,
46 kRelocRelative,
47 kRelocSymbol,
48 kRelocSymbolCached,
49 kRelocMax
50 };
51
52 void count_relocation(RelocationKind kind);
53
count_relocation_if(RelocationKind kind)54 template <bool Enabled> void count_relocation_if(RelocationKind kind) {
55 if (Enabled) count_relocation(kind);
56 }
57
58 void print_linker_stats();
59
is_symbol_global_and_defined(const soinfo * si,const ElfW (Sym)* s)60 inline bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
61 if (__predict_true(ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
62 ELF_ST_BIND(s->st_info) == STB_WEAK)) {
63 return s->st_shndx != SHN_UNDEF;
64 } else if (__predict_false(ELF_ST_BIND(s->st_info) != STB_LOCAL)) {
65 DL_WARN("Warning: unexpected ST_BIND value: %d for \"%s\" in \"%s\" (ignoring)",
66 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->get_realpath());
67 }
68 return false;
69 }
70