linker_debug.h revision 2e85579c34047c305caf15fb0ebe02bf3d001d0e
1/*
2 * Copyright (C) 2008 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#ifndef _LINKER_DEBUG_H_
30#define _LINKER_DEBUG_H_
31
32#include <stdio.h>
33
34/* WARNING: For linker debugging only.. Be careful not to leave  any of
35 * this on when submitting back to repository */
36#define LINKER_DEBUG         0
37#define TRACE_DEBUG          0
38#define DO_TRACE_LOOKUP      0
39#define DO_TRACE_RELO        0
40#define TIMING               0
41#define STATS                0
42#define COUNT_PAGES          0
43
44/*********************************************************************
45 * You shouldn't need to modify anything below unless you are adding
46 * more debugging information.
47 *
48 * To enable/disable specific debug options, change the defines above
49 *********************************************************************/
50
51
52/*********************************************************************/
53#undef TRUE
54#undef FALSE
55#define TRUE                 1
56#define FALSE                0
57
58/* Only use printf() during debugging.  We have seen occasional memory
59 * corruption when the linker uses printf().
60 */
61#if LINKER_DEBUG
62extern int debug_verbosity;
63#warning "*** LINKER IS USING printf(); DO NOT CHECK THIS IN ***"
64#define _PRINTVF(v,f,x...)                                                \
65    do {                                                                  \
66        (debug_verbosity > (v)) && (printf(x), ((f) && fflush(stdout)));  \
67    } while (0)
68#else /* !LINKER_DEBUG */
69#define _PRINTVF(v,f,x...)   do {} while(0)
70#endif /* LINKER_DEBUG */
71
72#define PRINT(x...)          _PRINTVF(-1, FALSE, x)
73#define INFO(x...)           _PRINTVF(0, TRUE, x)
74#define TRACE(x...)          _PRINTVF(1, TRUE, x)
75#define WARN(fmt,args...)    \
76        _PRINTVF(-1, TRUE, "%s:%d| WARNING: " fmt, __FILE__, __LINE__, ## args)
77#define ERROR(fmt,args...)    \
78        _PRINTVF(-1, TRUE, "%s:%d| ERROR: " fmt, __FILE__, __LINE__, ## args)
79
80
81#if TRACE_DEBUG
82#define DEBUG(x...)          _PRINTVF(2, TRUE, "DEBUG: " x)
83#else /* !TRACE_DEBUG */
84#define DEBUG(x...)          do {} while (0)
85#endif /* TRACE_DEBUG */
86
87#if LINKER_DEBUG
88#define TRACE_TYPE(t,x...)   do { if (DO_TRACE_##t) { TRACE(x); } } while (0)
89#else  /* !LINKER_DEBUG */
90#define TRACE_TYPE(t,x...)   do {} while (0)
91#endif /* LINKER_DEBUG */
92
93#if STATS
94#define RELOC_ABSOLUTE        0
95#define RELOC_RELATIVE        1
96#define RELOC_COPY            2
97#define RELOC_SYMBOL          3
98#define NUM_RELOC_STATS       4
99
100struct _link_stats {
101    int reloc[NUM_RELOC_STATS];
102};
103extern struct _link_stats linker_stats;
104
105#define COUNT_RELOC(type)                                 \
106        do { if (type >= 0 && type < NUM_RELOC_STATS) {   \
107                linker_stats.reloc[type] += 1;            \
108             } else  {                                    \
109                PRINT("Unknown reloc stat requested\n");  \
110             }                                            \
111           } while(0)
112#else /* !STATS */
113#define COUNT_RELOC(type)     do {} while(0)
114#endif /* STATS */
115
116#if TIMING
117#undef WARN
118#define WARN(x...)           do {} while (0)
119#endif /* TIMING */
120
121#if COUNT_PAGES
122extern unsigned bitmask[];
123#define MARK(offset)         do {                                        \
124        bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
125    } while(0)
126#else
127#define MARK(x)              do {} while (0)
128#endif
129
130#define DEBUG_DUMP_PHDR(phdr, name, pid) do { \
131        DEBUG("%5d %s (phdr = 0x%08x)\n", (pid), (name), (unsigned)(phdr));   \
132        DEBUG("\t\tphdr->offset   = 0x%08x\n", (unsigned)((phdr)->p_offset)); \
133        DEBUG("\t\tphdr->p_vaddr  = 0x%08x\n", (unsigned)((phdr)->p_vaddr));  \
134        DEBUG("\t\tphdr->p_paddr  = 0x%08x\n", (unsigned)((phdr)->p_paddr));  \
135        DEBUG("\t\tphdr->p_filesz = 0x%08x\n", (unsigned)((phdr)->p_filesz)); \
136        DEBUG("\t\tphdr->p_memsz  = 0x%08x\n", (unsigned)((phdr)->p_memsz));  \
137        DEBUG("\t\tphdr->p_flags  = 0x%08x\n", (unsigned)((phdr)->p_flags));  \
138        DEBUG("\t\tphdr->p_align  = 0x%08x\n", (unsigned)((phdr)->p_align));  \
139    } while (0)
140
141#endif /* _LINKER_DEBUG_H_ */
142