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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
Create a debuginfo package for each subpackage.
Index: build/files.c
===================================================================
--- build/files.c.orig
+++ build/files.c
@@ -20,6 +20,10 @@
#include <rpm/rpmfileutil.h> /* rpmDoDigest() */
#include <rpm/rpmlog.h>
+#if HAVE_GELF_H
+#include <gelf.h>
+#endif
+
#include "rpmio/rpmio_internal.h" /* XXX rpmioSlurp */
#include "rpmio/base64.h"
#include "misc/fts.h"
@@ -2105,13 +2109,237 @@ exit:
return rc;
}
+#if HAVE_GELF_H && HAVE_LIBELF
+/* Query the build-id from the ELF file NAME and store it in the newly
+ allocated *build_id array of size *build_id_size. Returns -1 on
+ error. */
+
+int
+getELFBuildId (const char *name,
+ unsigned char **id, size_t *id_size)
+{
+ int fd, i;
+ Elf *elf;
+ GElf_Ehdr ehdr;
+ Elf_Data *build_id = NULL;
+ size_t build_id_offset = 0, build_id_size = 0;
+
+ /* Now query the build-id of the file and add the
+ corresponding links in the .build-id tree.
+ The following code is based on tools/debugedit.c. */
+ fd = open (name, O_RDONLY);
+ if (fd < 0)
+ return -1;
+ elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
+ if (elf == NULL)
+ {
+ fprintf (stderr, "cannot open ELF file: %s",
+ elf_errmsg (-1));
+ close (fd);
+ return -1;
+ }
+ if (elf_kind (elf) != ELF_K_ELF
+ || gelf_getehdr (elf, &ehdr) == NULL
+ || (ehdr.e_type != ET_DYN
+ && ehdr.e_type != ET_EXEC
+ && ehdr.e_type != ET_REL))
+ {
+ elf_end (elf);
+ close (fd);
+ return -1;
+ }
+ for (i = 0; i < ehdr.e_shnum; ++i)
+ {
+ Elf_Scn *s = elf_getscn (elf, i);
+ GElf_Shdr shdr;
+ Elf_Data *data;
+ Elf32_Nhdr nh;
+ Elf_Data dst =
+ {
+ .d_version = EV_CURRENT, .d_type = ELF_T_NHDR,
+ .d_buf = &nh, .d_size = sizeof nh
+ };
+ Elf_Data src = dst;
+
+ gelf_getshdr (s, &shdr);
+ if (shdr.sh_type != SHT_NOTE
+ || !(shdr.sh_flags & SHF_ALLOC))
+ continue;
+
+ /* Look for a build-ID note here. */
+ data = elf_rawdata (s, NULL);
+ src.d_buf = data->d_buf;
+ assert (sizeof (Elf32_Nhdr) == sizeof (Elf64_Nhdr));
+ while (data->d_buf + data->d_size - src.d_buf > (int) sizeof nh
+ && elf32_xlatetom (&dst, &src, ehdr.e_ident[EI_DATA]))
+ {
+ Elf32_Word len = sizeof nh + nh.n_namesz;
+ len = (len + 3) & ~3;
+
+ if (nh.n_namesz == sizeof "GNU" && nh.n_type == 3
+ && !memcmp (src.d_buf + sizeof nh, "GNU", sizeof "GNU"))
+ {
+ build_id = data;
+ build_id_offset = src.d_buf + len - data->d_buf;
+ build_id_size = nh.n_descsz;
+ break;
+ }
+
+ len += nh.n_descsz;
+ len = (len + 3) & ~3;
+ src.d_buf += len;
+ }
+
+ if (build_id != NULL)
+ break;
+ }
+
+ if (build_id == NULL)
+ return -1;
+
+ *id = malloc (build_id_size);
+ *id_size = build_id_size;
+ memcpy (*id, build_id->d_buf + build_id_offset, build_id_size);
+
+ elf_end (elf);
+ close (fd);
+
+ return 0;
+}
+
+
+static rpmTag copyTagsForDebug[] = {
+ RPMTAG_EPOCH,
+ RPMTAG_VERSION,
+ RPMTAG_RELEASE,
+ RPMTAG_LICENSE,
+ RPMTAG_PACKAGER,
+ RPMTAG_DISTRIBUTION,
+ RPMTAG_DISTURL,
+ RPMTAG_VENDOR,
+ RPMTAG_ICON,
+ RPMTAG_URL,
+ RPMTAG_CHANGELOGTIME,
+ RPMTAG_CHANGELOGNAME,
+ RPMTAG_CHANGELOGTEXT,
+ RPMTAG_PREFIXES,
+ RPMTAG_RHNPLATFORM,
+ RPMTAG_OS,
+ RPMTAG_DISTTAG,
+ RPMTAG_CVSID,
+ RPMTAG_ARCH,
+ 0
+};
+
+static void addDebuginfoPackage(rpmSpec spec, Package pkg, char *buildroot)
+{
+ const char *a;
+
+ elf_version(EV_CURRENT);
+ a = headerGetString(pkg->header, RPMTAG_ARCH);
+ if (strcmp(a, "noarch") != 0 && strcmp(a, "src") != 0 && strcmp(a, "nosrc") != 0)
+ {
+ Package dbg;
+ rpmfi fi = pkg->cpioList;
+ char tmp[1024];
+ const char *name;
+ ARGV_t files = NULL;
+ int seen_build_id = 0;
+
+ /* Check if the current package has files with debug info
+ and record them. */
+ fi = rpmfiInit (fi, 0);
+ while (rpmfiNext (fi) >= 0)
+ {
+ const char *base;
+ int i;
+ unsigned char *build_id;
+ size_t build_id_size = 0;
+ struct stat sbuf;
+
+ name = rpmfiFN (fi);
+ /* Skip leading buildroot. */
+ base = name + strlen (buildroot);
+ /* Pre-pend %buildroot/usr/lib/debug and append .debug. */
+ snprintf (tmp, 1024, "%s/usr/lib/debug%s.debug",
+ buildroot, base);
+ /* If that file exists we have debug information for it. */
+ if (access (tmp, F_OK) != 0)
+ continue;
+
+ /* Append the file list preamble. */
+ if (!files)
+ {
+ argvAdd(&files, "%defattr(-,root,root)");
+ argvAdd(&files, "%dir /usr/lib/debug");
+ }
+ /* Add the files main debug-info file. */
+ snprintf (tmp, 1024, "/usr/lib/debug/%s.debug", base);
+ argvAdd(&files, tmp);
+
+ /* Do not bother to check build-ids for symbolic links.
+ We'll handle them for the link target. */
+ if (lstat (name, &sbuf) == -1
+ || S_ISLNK (sbuf.st_mode))
+ continue;
+
+ /* Try to gather the build-id from the binary. */
+ if (getELFBuildId (name, &build_id, &build_id_size) == -1)
+ continue;
+
+ /* If we see build-id links for the first time add the
+ directory. */
+ if (!seen_build_id)
+ argvAdd(&files, "%dir /usr/lib/debug/.build-id");
+
+ /* From the build-id construct the two links pointing back
+ to the debug information file and the binary. */
+ snprintf (tmp, 1024, "/usr/lib/debug/.build-id/%02x/",
+ build_id[0]);
+ for (i = 1; i < build_id_size; ++i)
+ sprintf (tmp + strlen (tmp), "%02x", build_id[i]);
+ argvAdd(&files, tmp);
+ sprintf (tmp + strlen (tmp), ".debug");
+ argvAdd(&files, tmp);
+
+ free (build_id);
+ }
+
+ /* If there are debuginfo files for this package add a
+ new debuginfo package. */
+ if (files)
+ {
+ dbg = newPackage (spec);
+ headerNVR (pkg->header, &name, NULL, NULL);
+ /* Set name, summary and group. */
+ snprintf (tmp, 1024, "%s-debuginfo", name);
+ headerPutString(dbg->header, RPMTAG_NAME, tmp);
+ snprintf (tmp, 1024, "Debug information for package %s", name);
+ headerPutString(dbg->header, RPMTAG_SUMMARY, tmp);
+ snprintf (tmp, 1024, "This package provides debug information for package %s.\n"
+ "Debug information is useful when developing applications that use this\n"
+ "package or when debugging this package.", name);
+ headerPutString(dbg->header, RPMTAG_DESCRIPTION, tmp);
+ headerPutString(dbg->header, RPMTAG_GROUP, "Development/Debug");
+ /* Inherit other tags from parent. */
+ headerCopyTags (pkg->header, dbg->header, copyTagsForDebug);
+
+ /* Build up the files list. */
+ dbg->fileList = files;
+ }
+ }
+}
+#endif
+
rpmRC processBinaryFiles(rpmSpec spec, rpmBuildPkgFlags pkgFlags,
int installSpecialDoc, int test)
{
Package pkg;
rpmRC rc = RPMRC_OK;
+ char *buildroot;
check_fileList = newStringBuf();
+ buildroot = rpmGenPath(spec->rootDir, spec->buildRoot, NULL);
genSourceRpmName(spec);
for (pkg = spec->packages; pkg != NULL; pkg = pkg->next) {
@@ -2127,8 +2355,12 @@ rpmRC processBinaryFiles(rpmSpec spec, r
rpmlog(RPMLOG_NOTICE, _("Processing files: %s\n"), nvr);
free(nvr);
- if ((rc = processPackageFiles(spec, pkgFlags, pkg, installSpecialDoc, test)) != RPMRC_OK ||
- (rc = rpmfcGenerateDepends(spec, pkg)) != RPMRC_OK)
+ if ((rc = processPackageFiles(spec, pkgFlags, pkg, installSpecialDoc, test)) != RPMRC_OK)
+ goto exit;
+#if HAVE_GELF_H && HAVE_LIBELF
+ addDebuginfoPackage(spec, pkg, buildroot);
+#endif
+ if ((rc = rpmfcGenerateDepends(spec, pkg)) != RPMRC_OK)
goto exit;
a = headerGetString(pkg->header, RPMTAG_ARCH);
Index: scripts/find-debuginfo.sh
===================================================================
--- scripts/find-debuginfo.sh.orig
+++ scripts/find-debuginfo.sh
@@ -267,19 +267,11 @@ while read nlinks inum f; do
fi
done || exit
-# For each symlink whose target has a .debug file,
-# make a .debug symlink to that file.
-find $RPM_BUILD_ROOT ! -path "${debugdir}/*" -type l -print |
-while read f
-do
- t=$(readlink -m "$f").debug
- f=${f#$RPM_BUILD_ROOT}
- t=${t#$RPM_BUILD_ROOT}
- if [ -f "$debugdir$t" ]; then
- echo "symlinked /usr/lib/debug$t to /usr/lib/debug${f}.debug"
- debug_link "/usr/lib/debug$t" "${f}.debug"
- fi
-done
+# We used to make a .debug symlink for each symlink whose target
+# has a .debug file to that file. This is not necessary because
+# the debuglink section contains only the destination of those links.
+# Creating those links anyway results in debuginfo packages for
+# devel packages just because of the .so symlinks in them.
if [ -s "$SOURCEFILE" ]; then
mkdir -p "${RPM_BUILD_ROOT}/usr/src/debug"
Index: macros.in
===================================================================
--- macros.in.orig
+++ macros.in
@@ -180,17 +180,6 @@
%debug_package \
%ifnarch noarch\
%global __debug_package 1\
-%package debuginfo\
-Summary: Debug information for package %{name}\
-Group: Development/Debug\
-AutoReqProv: 0\
-%description debuginfo\
-This package provides debug information for package %{name}.\
-Debug information is useful when developing applications that use this\
-package or when debugging this package.\
-%files debuginfo -f debugfiles.list\
-%defattr(-,root,root)\
-\
%package debugsource\
Summary: Debug sources for package %{name}\
Group: Development/Debug\
|