blob: 9d7a0baf563e46631c3277d738cd3242028ccd84 [file] [log] [blame]
Petr Machata2b46cfc2012-02-18 11:17:29 +01001/*
2 * This file is part of ltrace.
3 * Copyright (C) 2012 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2006 Paul Gilliam
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19 * 02110-1301 USA
20 */
21
22#ifndef _LIBRARY_H_
23#define _LIBRARY_H_
24
25#include <stdint.h>
26#include <gelf.h> // XXX
27
28struct Process;
29struct library;
30
31enum toplt {
32 LS_TOPLT_NONE = 0, /* PLT not used for this symbol. */
33 LS_TOPLT_EXEC, /* PLT for this symbol is executable. */
34 LS_TOPLT_POINT /* PLT for this symbol is a non-executable. */
35};
36
37/* We should in general be able to trace 64-bit processes with 32-bit
38 * ltrace. (At least PPC has several PTRACE requests related to
39 * tracing 64-on-32, so presumably it should be possible.) But ltrace
40 * is currently hopelessly infested with using void* for host address.
41 * So keep with it, for now. */
42typedef void *target_address_t;
43
44struct library_symbol {
45 struct library_symbol *next;
Petr Machata29add4f2012-02-18 16:38:05 +010046 struct library *lib;
Petr Machata2b46cfc2012-02-18 11:17:29 +010047 const char *name;
48 target_address_t enter_addr;
49 enum toplt plt_type;
Petr Machata2b46cfc2012-02-18 11:17:29 +010050 char own_name;
51};
52
53/* Init LIBSYM. NAME will be freed when LIBSYM is destroyed if
54 * OWN_NAME. */
Petr Machatae6523e62012-03-24 04:54:06 +010055void library_symbol_init(struct library_symbol *libsym,
Petr Machata522a6ca2012-03-16 02:41:10 +010056 target_address_t addr, const char *name, int own_name,
Petr Machatabe04d6b2012-03-24 02:01:45 +010057 enum toplt type_of_plt);
Petr Machata2b46cfc2012-02-18 11:17:29 +010058
59/* Copy library symbol SYM into the area pointed-to by RETP. Return 0
60 * on success or a negative value on failure. */
61int library_symbol_clone(struct library_symbol *retp,
62 struct library_symbol *sym);
63
64/* Destroy library symbol. This essentially just frees name if it's
65 * owned. It doesn't free the memory associated with SYM pointer
66 * itself. Returns 0 on success or a negative value in case of an
67 * error (which would be an out of memory condition). */
68void library_symbol_destroy(struct library_symbol *sym);
69
70/* Compare two library symbols. Returns a negative value, 0, or a
71 * positive value, much like strcmp. The function compares symbol
72 * addresses, and if those are equal, it compares symbol names. If
73 * those are equal, too, the symbols are considered equal. */
74int library_symbol_cmp(struct library_symbol *a, struct library_symbol *b);
75
76/* A function that can be used as library_each_symbol callback. Looks
77 * for a symbol SYM for which library_symbol_cmp(SYM, STANDARD)
78 * returns 0. */
79enum callback_status library_symbol_equal_cb(struct library_symbol *libsym,
80 void *standard);
81
82/* XXX we might consider sharing libraries across processes. Things
83 * like libc will be opened by every single process, no point cloning
84 * these everywhere. But for now, keep the ownership structure
85 * simple. */
86struct library {
87 struct library *next;
88
89 /* Address where the library is mapped. Two library objects
90 * are considered equal, if they have the same base. */
91 target_address_t base;
92
93 /* Absolute address of the entry point. Useful for main
94 * binary, though I the value might be useful for the dynamic
95 * linker, too (in case we ever want to do early process
96 * tracing). */
97 target_address_t entry;
98
99 /* Symbols associated with the library. */
100 struct library_symbol *symbols;
101 const char *name;
102 char own_name;
103};
104
105/* Init LIB. NAME will be freed when LIB is destroyed if
106 * OWN_NAME. */
107void library_init(struct library *lib, const char *name, int own_name);
108
109/* Initialize RETP to a library identical to LIB. Symbols are not
110 * shared, but copied over. Returns 0 on success and a negative value
111 * in case of failure. */
112int library_clone(struct library *retp, struct library *lib);
113
114/* Destroy library. Doesn't free LIB itself. */
115void library_destroy(struct library *lib);
116
117/* Set library name. Frees the old name if necessary. */
118void library_set_name(struct library *lib, const char *new_name, int own_name);
119
120/* Iterate through list of symbols of library LIB. Restarts are
Petr Machata74132a42012-03-16 02:46:18 +0100121 * supported via START_AFTER (see each_process for details of
122 * iteration interface). */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100123struct library_symbol *library_each_symbol
Petr Machata74132a42012-03-16 02:46:18 +0100124 (struct library *lib, struct library_symbol *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100125 enum callback_status (*cb)(struct library_symbol *, void *),
126 void *data);
127
128/* Add a new symbol SYM to LIB. SYM is assumed owned, we need to
129 * overwrite SYM->next. */
130void library_add_symbol(struct library *lib, struct library_symbol *sym);
131
132/* A function that can be used as proc_each_library callback. Looks
133 * for a library with the name passed in DATA. PROC is ignored. */
134enum callback_status library_named_cb(struct Process *proc,
135 struct library *lib, void *name);
136
137/* A function that can be used as proc_each_library callback. Looks
138 * for a library with given base.
139 *
140 * NOTE: The base is passed as a POINTER to target_address_t (that
141 * because in general, target_address_t doesn't fit in void*). */
142enum callback_status library_with_base_cb(struct Process *proc,
143 struct library *lib, void *basep);
144
Petr Machatab88fd6e2012-03-21 05:00:56 +0100145/* XXX this should really be in backend.h (as on pmachata/revamp
146 * branch), or, on this branch, in common.h. But we need
147 * target_address_t (which should also be in backend.h, I reckon), so
148 * stuff it here for the time being. */
149/* This function is implemented in the back end. It is called for all
150 * raw addresses as gleaned from symbol tables etc. If necessary on
151 * given architecture, this function should translate the address
152 * according to .opd or other indirection mechanism. Returns 0 on
153 * success and a negative value on failure. */
154int arch_translate_address(struct Process *proc,
155 target_address_t addr, target_address_t *ret);
156
Petr Machata2b46cfc2012-02-18 11:17:29 +0100157#endif /* _LIBRARY_H_ */