summaryrefslogtreecommitdiff
path: root/rts/RtsSymbolInfo.c
blob: 0553308f3670185cabb22fff0f4cf788cd9b3fdc (plain)
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
/* -----------------------------------------------------------------------------
 *
 * (c) The GHC Team, 2000-2015
 *
 * RTS Symbols
 *
 * ---------------------------------------------------------------------------*/

#include "ghcplatform.h"
#include "RtsSymbolInfo.h"

#include "Rts.h"
#include "HsFFI.h"

#include "Hash.h"
#include "RtsUtils.h"

#include <stdbool.h>

/* Generic function to update any extra info fields.  */
void setSymbolInfo(ObjectCode *owner, const void *label, symbolUpdater updater)
{
    SymbolInfo *info;
    if (owner && label)
    {
        info = NULL;
        if (!owner->extraInfos)
            owner->extraInfos = allocStrHashTable();
        else
            info = lookupStrHashTable(owner->extraInfos, label);

        if (!info)
        {
            info = stgMallocBytes(sizeof(SymbolInfo), "setSymbolInfo");
            info->kind = 0;
        }

        updater(info);
        insertStrHashTable(owner->extraInfos, label, info);
    }
}

/* -----------------------------------------------------------------------------
* Performs a check to see if the symbol at the given address
* is a weak symbol or not.
*
* Returns: true on symbol being weak, else false
*/
bool isSymbolWeak(ObjectCode *owner, const void *label)
{
    SymbolInfo *info;
    return owner
        && label
        && owner->extraInfos
        && (info = lookupStrHashTable(owner->extraInfos, label)) != NULL
        && (info->kind & KIND_WEAK) == KIND_WEAK;
}

/* -----------------------------------------------------------------------------
* Performs a check to see if the symbol at the given address
* is an import symbol or not.
*
* Returns: true on symbol being weak, else false
*/
bool isSymbolImport(ObjectCode *owner, const void *label)
{
    SymbolInfo *info;
    return owner
        && label
        && owner->extraInfos
        && (info = lookupStrHashTable(owner->extraInfos, label)) != NULL
        && (info->kind & KIND_IMPORT) == KIND_IMPORT;
}

static void markWeak(SymbolInfo* info)
{
    if(info)
      info->kind |= KIND_WEAK;
}

static void markImport(SymbolInfo* info)
{
    if(info)
      info->kind |= KIND_IMPORT;
}

static void unmarkImport(SymbolInfo* info)
{
    if(info)
      info->kind &= ~KIND_IMPORT;
}

/* -----------------------------------------------------------------------------
* Marks the symbol at the given address as weak or not.
* If the extra symbol infos table has not been initialized
* yet this will create and allocate a new Hashtable
*/
void setWeakSymbol(ObjectCode *owner, const void *label)
{
    setSymbolInfo (owner, label, &markWeak);
}

/* -----------------------------------------------------------------------------
* Marks the symbol at the given address as import or not.
* If the extra symbol infos table has not been initialized
* yet this will create and allocate a new Hashtable
*/
void setImportSymbol(ObjectCode *owner, const void *label)
{
    setSymbolInfo (owner, label, &markImport);
}

/* -----------------------------------------------------------------------------
* Clear the import symbol flag.
* If the extra symbol infos table has not been initialized
* yet this will create and allocate a new Hashtable
*/
void clearImportSymbol(ObjectCode *owner, const void *label)
{
    setSymbolInfo (owner, label, &unmarkImport);
}