1/*
2 * Copyright 2011, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include "GOT.h"
19
20void *got_symbol_addresses[NUM_OF_GOT_ENTRY];
21int got_symbol_indexes[NUM_OF_GOT_ENTRY];
22size_t got_symbol_count = 0;
23
24void *got_address()
25{
26  return &got_symbol_addresses[0];
27}
28
29int search_got(int symbol_index, void *addr, uint8_t bind_type)
30{
31  size_t i;
32
33  // For local symbols (R_MIPS_GOT16), we only store the high 16-bit value
34  // after adding 0x8000.
35  if (bind_type == STB_LOCAL)
36    addr = (void *)(((intptr_t)addr + 0x8000) & 0xFFFF0000);
37
38  for (i = 0; i < got_symbol_count; i++) {
39    if (got_symbol_indexes[i] == symbol_index) {
40      if (bind_type == STB_LOCAL) {
41        // Check if the value is the same for local symbols.
42        // If yes, we can reuse this entry.
43        // If not, we continue searching.
44        if (got_symbol_addresses[i] == addr) {
45          return i;
46        }
47      }
48      else {
49        // The value must be the same for global symbols .
50        rsl_assert (got_symbol_addresses[i] == addr
51                    && "MIPS GOT address error.");
52        return i;
53      }
54    }
55  }
56
57  // Cannot find this symbol with correct value, so we need to create one
58  rsl_assert (got_symbol_count < NUM_OF_GOT_ENTRY && "MIPS GOT is full.");
59  got_symbol_indexes[got_symbol_count] = symbol_index;
60  got_symbol_addresses[got_symbol_count] = addr;
61  got_symbol_count++;
62  return (got_symbol_count - 1);
63}
64