blob: 3d3d5f8f715d61d81219f593b0ff632c1175f2a5 [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>
Petr Machatae4e693b2012-03-27 03:10:53 +020026#include "sysdep.h"
Petr Machata2b46cfc2012-02-18 11:17:29 +010027
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
Petr Machataecb082f2012-04-05 02:10:10 +020044/* Dict interface. */
45unsigned int target_address_hash(const void *key);
46int target_address_cmp(const void *key1, const void *key2);
47
Petr Machata2b46cfc2012-02-18 11:17:29 +010048struct library_symbol {
49 struct library_symbol *next;
Petr Machata29add4f2012-02-18 16:38:05 +010050 struct library *lib;
Petr Machata2b46cfc2012-02-18 11:17:29 +010051 const char *name;
52 target_address_t enter_addr;
53 enum toplt plt_type;
Petr Machata2b46cfc2012-02-18 11:17:29 +010054 char own_name;
Petr Machatae4e693b2012-03-27 03:10:53 +020055 struct arch_library_symbol_data arch;
Petr Machata2b46cfc2012-02-18 11:17:29 +010056};
57
58/* Init LIBSYM. NAME will be freed when LIBSYM is destroyed if
Petr Machatae4e693b2012-03-27 03:10:53 +020059 * OWN_NAME. ARCH has to be initialized by a separate call. */
Petr Machatae6523e62012-03-24 04:54:06 +010060void library_symbol_init(struct library_symbol *libsym,
Petr Machata522a6ca2012-03-16 02:41:10 +010061 target_address_t addr, const char *name, int own_name,
Petr Machatabe04d6b2012-03-24 02:01:45 +010062 enum toplt type_of_plt);
Petr Machata2b46cfc2012-02-18 11:17:29 +010063
64/* Copy library symbol SYM into the area pointed-to by RETP. Return 0
65 * on success or a negative value on failure. */
66int library_symbol_clone(struct library_symbol *retp,
67 struct library_symbol *sym);
68
69/* Destroy library symbol. This essentially just frees name if it's
70 * owned. It doesn't free the memory associated with SYM pointer
71 * itself. Returns 0 on success or a negative value in case of an
72 * error (which would be an out of memory condition). */
73void library_symbol_destroy(struct library_symbol *sym);
74
75/* Compare two library symbols. Returns a negative value, 0, or a
76 * positive value, much like strcmp. The function compares symbol
77 * addresses, and if those are equal, it compares symbol names. If
78 * those are equal, too, the symbols are considered equal. */
79int library_symbol_cmp(struct library_symbol *a, struct library_symbol *b);
80
Petr Machata157cc4d2012-04-04 19:00:34 +020081/* Set a name for library symbol. This frees the old name, if
82 * that is owned. */
83void library_symbol_set_name(struct library_symbol *libsym,
84 const char *name, int own_name);
85
Petr Machata2b46cfc2012-02-18 11:17:29 +010086/* A function that can be used as library_each_symbol callback. Looks
87 * for a symbol SYM for which library_symbol_cmp(SYM, STANDARD)
88 * returns 0. */
89enum callback_status library_symbol_equal_cb(struct library_symbol *libsym,
90 void *standard);
91
Petr Machatab5f80ac2012-04-04 01:46:18 +020092enum library_type {
93 LT_LIBTYPE_MAIN,
94 LT_LIBTYPE_DSO,
95 LT_LIBTYPE_DLOPEN,
96};
97
Petr Machata2b46cfc2012-02-18 11:17:29 +010098/* XXX we might consider sharing libraries across processes. Things
99 * like libc will be opened by every single process, no point cloning
100 * these everywhere. But for now, keep the ownership structure
101 * simple. */
102struct library {
103 struct library *next;
104
Petr Machata89ac0392012-04-03 02:30:48 +0200105 /* Unique key. Two library objects are considered equal, if
106 * they have the same key. */
107 target_address_t key;
108
Petr Machata2b46cfc2012-02-18 11:17:29 +0100109 /* Address where the library is mapped. Two library objects
110 * are considered equal, if they have the same base. */
111 target_address_t base;
112
113 /* Absolute address of the entry point. Useful for main
Petr Machata52dbfb12012-03-29 16:38:26 +0200114 * binary, though I suppose the value might be useful for the
115 * dynamic linker, too (in case we ever want to do early
116 * process tracing). */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100117 target_address_t entry;
118
Petr Machata52dbfb12012-03-29 16:38:26 +0200119 /* Address of PT_DYNAMIC segment. */
120 target_address_t dyn_addr;
121
Petr Machata2b46cfc2012-02-18 11:17:29 +0100122 /* Symbols associated with the library. */
123 struct library_symbol *symbols;
Petr Machata0b55b582012-04-02 00:38:46 +0200124
125 const char *soname;
126 const char *pathname;
127
Petr Machatab5f80ac2012-04-04 01:46:18 +0200128 enum library_type type;
129
Petr Machata0b55b582012-04-02 00:38:46 +0200130 char own_soname : 1;
131 char own_pathname : 1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100132};
133
Petr Machata0b55b582012-04-02 00:38:46 +0200134/* Init LIB. */
Petr Machatab5f80ac2012-04-04 01:46:18 +0200135void library_init(struct library *lib, enum library_type type);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100136
137/* Initialize RETP to a library identical to LIB. Symbols are not
138 * shared, but copied over. Returns 0 on success and a negative value
139 * in case of failure. */
140int library_clone(struct library *retp, struct library *lib);
141
142/* Destroy library. Doesn't free LIB itself. */
143void library_destroy(struct library *lib);
144
Petr Machata0b55b582012-04-02 00:38:46 +0200145/* Set library soname. Frees the old name if necessary. */
146void library_set_soname(struct library *lib,
147 const char *new_name, int own_name);
148
149/* Set library pathname. Frees the old name if necessary. */
150void library_set_pathname(struct library *lib,
151 const char *new_name, int own_name);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100152
153/* Iterate through list of symbols of library LIB. Restarts are
Petr Machata74132a42012-03-16 02:46:18 +0100154 * supported via START_AFTER (see each_process for details of
155 * iteration interface). */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100156struct library_symbol *library_each_symbol
Petr Machata74132a42012-03-16 02:46:18 +0100157 (struct library *lib, struct library_symbol *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100158 enum callback_status (*cb)(struct library_symbol *, void *),
159 void *data);
160
161/* Add a new symbol SYM to LIB. SYM is assumed owned, we need to
162 * overwrite SYM->next. */
163void library_add_symbol(struct library *lib, struct library_symbol *sym);
164
165/* A function that can be used as proc_each_library callback. Looks
166 * for a library with the name passed in DATA. PROC is ignored. */
167enum callback_status library_named_cb(struct Process *proc,
168 struct library *lib, void *name);
169
170/* A function that can be used as proc_each_library callback. Looks
171 * for a library with given base.
172 *
Petr Machata89ac0392012-04-03 02:30:48 +0200173 * NOTE: The key is passed as a POINTER to target_address_t (that
174 * because in general, target_address_t doesn't fit in void*). */
175enum callback_status library_with_key_cb(struct Process *proc,
176 struct library *lib, void *keyp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100177
Petr Machatab88fd6e2012-03-21 05:00:56 +0100178/* XXX this should really be in backend.h (as on pmachata/revamp
179 * branch), or, on this branch, in common.h. But we need
180 * target_address_t (which should also be in backend.h, I reckon), so
181 * stuff it here for the time being. */
182/* This function is implemented in the back end. It is called for all
183 * raw addresses as gleaned from symbol tables etc. If necessary on
184 * given architecture, this function should translate the address
185 * according to .opd or other indirection mechanism. Returns 0 on
186 * success and a negative value on failure. */
187int arch_translate_address(struct Process *proc,
188 target_address_t addr, target_address_t *ret);
189
Petr Machata2b46cfc2012-02-18 11:17:29 +0100190#endif /* _LIBRARY_H_ */