├── .gitignore
├── COPYING
├── Makefile
├── README.md
├── compare.c
├── compare.h
├── generate.c
├── generate.h
├── hash.c
├── hash.h
├── ksymtab.c
├── ksymtab.h
├── list.c
├── list.h
├── main.c
├── main.h
├── objects.c
├── objects.h
├── parser.h
├── parser.l
├── parser.y
├── record.h
├── show.c
├── show.h
├── stack.c
├── stack.h
├── utils.c
└── utils.h
/.gitignore:
--------------------------------------------------------------------------------
1 | cscope.files
2 | cscope.out
3 | *.o
4 | output/*
5 | .depend
6 | kabi-dw
7 | *.tab.c
8 | *.tab.h
9 | *.yy.c
10 |
--------------------------------------------------------------------------------
/COPYING:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | # Copyright(C) 2016, Red Hat, Inc., Stanislav Kozina
2 | #
3 | # This program is free software: you can redistribute it and/or modify
4 | # it under the terms of the GNU General Public License as published by
5 | # the Free Software Foundation, either version 3 of the License, or
6 | # (at your option) any later version.
7 | #
8 | # This program is distributed in the hope that it will be useful,
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 | # GNU General Public License for more details.
12 | #
13 | # You should have received a copy of the GNU General Public License
14 | # along with this program. If not, see .
15 |
16 | PROG=kabi-dw
17 | SRCS=generate.c ksymtab.c utils.c main.c stack.c objects.c hash.c list.c
18 | SRCS += compare.c show.c
19 |
20 | CC?=gcc
21 | CFLAGS+=-Wall --std=gnu99 -D_GNU_SOURCE -c
22 | LDFLAGS+=-ldw -lelf
23 |
24 | CFLAGS_RELEASE+=-O2 -Wl,-pie -D_FORTIFY_SOURCE=2
25 | CFLAGS_DEBUG+=-O0 -g3 -DDEBUG -Wextra -pedantic
26 |
27 | LDFLAGS_RELEASE+=-Wl,-z,now
28 |
29 | YACC=bison
30 | YACCFLAGS=-d -t
31 |
32 | FLEX=flex
33 | FLEXFLAGS=
34 |
35 | OBJS=$(SRCS:.c=.o)
36 | OBJS+=parser.yy.o parser.tab.o
37 |
38 | .PHONY: clean all depend debug asan
39 |
40 | ifeq (,$(findstring -c,$(CFLAGS)))
41 | override CFLAGS+=-c
42 | endif
43 |
44 | ifeq (,$(findstring -D_GNU_SOURCE,$(CFLAGS)))
45 | override CFLAGS+=-D_GNU_SOURCE
46 | endif
47 |
48 | ifeq (,$(findstring -ldw,$(LDFLAGS)))
49 | override LDFLAGS+=-ldw
50 | endif
51 |
52 | ifeq (,$(findstring -lelf,$(LDFLAGS)))
53 | override LDFLAGS+=-lelf
54 | endif
55 |
56 | all: CFLAGS+=$(CFLAGS_RELEASE)
57 | all: LDFLAGS+=$(LDFLAGS_RELEASE)
58 | all: $(PROG)
59 |
60 | debug: CFLAGS+=$(CFLAGS_DEBUG)
61 | debug: LDFLAGS:=$(LDFLAGS)
62 | debug: FLEXFLAGS+=-d
63 | debug: $(PROG)
64 |
65 | asan-debug: CFLAGS+=$(CFLAGS_DEBUG) -fsanitize=address
66 | asan-debug: LDFLAGS:=-lasan $(LDFLAGS)
67 | asan-debug: FLEXFLAGS+=-d
68 | asan-debug: $(PROG)
69 |
70 | asan: CFLAGS+=-fsanitize=address
71 | asan: LDFLAGS:=-lasan $(LDFLAGS)
72 | asan: $(PROG)
73 |
74 | $(PROG): $(OBJS)
75 | $(CC) -o $(PROG) $(OBJS) $(LDFLAGS)
76 |
77 | %.o: %.c
78 | $(CC) $(CFLAGS) $< -o $@
79 |
80 | parser.tab.c: parser.y
81 | $(YACC) $(YACCFLAGS) parser.y
82 |
83 | parser.yy.c: parser.tab.c parser.l parser.h
84 | $(FLEX) $(FLEXFLAGS) -o parser.yy.c parser.l
85 |
86 | depend: .depend
87 |
88 | .depend: $(SRCS)
89 | $(CC) $(CFLAGS) -MM $^ > ./.depend
90 |
91 | -include .depend
92 |
93 | clean:
94 | rm -f $(PROG) $(OBJS) .depend parser *.tab.c *.tab.h *.yy.c
95 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Synopsis
2 |
3 | The aim of **kabi-dw** is to detect any changes in the ABI between the successive builds of the Linux kernel.
4 | This is done by dumping the DWARF type information (the .debug\_info section) for the specific symbols into the text files and later comparing the text files.
5 |
6 | ## Example
7 |
8 | Build your kernel with CONFIG\_DEBUG\_INFO set:
9 |
10 | ```
11 | git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
12 | cd linux && git checkout v4.5
13 | make allyesconfig
14 | make
15 | ```
16 |
17 | Get all the modules *and* vmlinux into single directory
18 |
19 | ```
20 | make modules_install && cp vmlinux /usr/lib/modules/4.5.0
21 | ```
22 |
23 | ... build another kernel you want to compare to, lets say v4.6.
24 |
25 | Build a list of symbols you want to monitor:
26 |
27 | ~~~
28 | cat > symbols << EOF
29 | init_task
30 | schedule
31 | dev_queue_xmit
32 | __kmalloc
33 | printk
34 | EOF
35 | ~~~
36 |
37 | Generate the type information for both kernels based on the current stablelist
38 |
39 | ~~~
40 | ./kabi-dw generate -s symbols -o kabi-4.5 /usr/lib/modules/4.5.0
41 | ./kabi-dw generate -s symbols -o kabi-4.6 /usr/lib/modules/4.6.0
42 | ~~~
43 |
44 | Compare the two type dumps:
45 |
46 | ~~~
47 | ./kabi-dw compare kabi-4.5 kabi-4.6
48 | ~~~
49 |
50 | ## Motivation
51 |
52 | Traditionally Unix System V had a stable ABI to allow external modules to work with the OS kernel without a recompilation called Device Driver Interface.
53 | Linux however never developed such stable kernel ABI. Therefore it's vital to monitor all kernel interfaces used by the external module for change, and if such change happens, the module needs to be recompiled.
54 |
55 | Linux has an option (CONFIG\_MODVERSIONS) to generate a checksum identifying all exported symbols through the EXPORT\_SYMBOL() macro. But these checksum are not sufficient to actually identify the scope of the change. For example changing a couple of unused padding bits in a structure to a new field won't break any external modules, but such change changes the checksum of any function which receives such structure through its arguments.
56 |
57 | ## Installation
58 |
59 | This program needs elfutils installed. Check out your distribution to figure out how to install elfutils.
60 |
61 | For *Fedora* and *CentOS 7* systems:
62 | ~~~
63 | dnf install kabi-dw
64 | ~~~
65 |
66 | For *Ubuntu* systems, you need to compile it. Install the dependencies:
67 | ~~~
68 | sudo apt-get install elfutils
69 | ~~~
70 |
71 | Then just make and run:
72 | ~~~
73 | make
74 | ./kabi-dw
75 | ~~~
76 |
77 | ## Contributors
78 |
79 | Developed by Stanislav Kozina, Red Hat, Inc. with the help of others.
80 |
81 | ## License
82 |
83 | This program is free software: you can redistribute it and/or modify
84 | it under the terms of the GNU General Public License as published by
85 | the Free Software Foundation, either version 3 of the License, or
86 | (at your option) any later version.
87 |
--------------------------------------------------------------------------------
/compare.c:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright(C) 2017, Red Hat, Inc.
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | #ifndef _GNU_SOURCE /* We use GNU basename() that doesn't modify the arg */
19 | #error "We need GNU version of basename()!"
20 | #endif
21 |
22 | #include
23 | #include
24 | #include
25 | #include
26 | #include
27 | #include
28 | #include
29 | #include
30 |
31 | #include "main.h"
32 | #include "objects.h"
33 | #include "utils.h"
34 | #include "compare.h"
35 |
36 | /* diff -u style prefix for tree comparison */
37 | #define ADD_PREFIX "+"
38 | #define DEL_PREFIX "-"
39 |
40 | /* Return values for the (_)compare_tree functions */
41 | enum {
42 | COMP_SAME = 0, /* Subtree are equal */
43 | COMP_DIFF, /* Subtree differs, stop the scanning */
44 | COMP_CONT, /* Only offset or alignment change, continue */
45 | };
46 |
47 | int comp_return_value(int old, int new) {
48 | switch (new) {
49 | case COMP_DIFF:
50 | return COMP_DIFF;
51 | case COMP_CONT:
52 | if (old != COMP_DIFF)
53 | return COMP_CONT;
54 | case COMP_SAME:
55 | ;
56 | }
57 | return old;
58 | }
59 |
60 | /*
61 | * Is this symbol a duplicate, i.e. is not the first version of this symbol.
62 | */
63 | static bool is_duplicate(char *filename)
64 | {
65 | char *base = basename(filename);
66 | char *prefix = NULL, *name = NULL;
67 | int version = 0;
68 | bool ret = (sscanf(base, "%m[a-z]--%m[^.-]-%i.txt",
69 | &prefix, &name, &version) == 3);
70 |
71 | free(prefix);
72 | free(name);
73 |
74 | return ret;
75 | }
76 |
77 | static void _print_node_list(const char *s, const char *prefix,
78 | obj_list_t *list, obj_list_t *last, FILE *stream) {
79 | obj_list_t *l = list;
80 |
81 | fprintf(stream, "%s:\n", s);
82 | while (l && l != last) {
83 | obj_print_tree__prefix(l->member, prefix, stream);
84 | l = l->next;
85 | }
86 | }
87 |
88 | static void print_node_list(const char *s, const char *prefix,
89 | obj_list_t *list, FILE *stream) {
90 | _print_node_list(s, prefix, list, NULL, stream);
91 | }
92 |
93 |
94 | /*
95 | * There is some ambiguity here that need to be cleared and a
96 | * hierarchy that need to be explicitly established. The current
97 | * situation is: if there is a real change to the object
98 | * (different name, type...) we return CMP_DIFF; If that's not
99 | * the case, but a referred symbol has changed, we return
100 | * CMP_REFFILE; If that's not the case, but the offset has
101 | * changed, we return CMP_OFFSET. So the current order is
102 | * CMP_DIFF > CMP_REFFILE > CMP_OFFSET > CMP_ALIGNMENT > CMP_BYTE_SIZE
103 | * In case of alignment, if the structure alignment has changed,
104 | * only that is reported. If not, then the fields are checked and
105 | * the all the different fields are reported.
106 | * The same is true of byte size changes.
107 | */
108 |
109 | typedef enum {
110 | CMP_SAME = 0,
111 | CMP_OFFSET, /* Only the offset has changed */
112 | CMP_DIFF, /* Nodes are differents */
113 | CMP_REFFILE, /* A refered symbol has changed */
114 | CMP_ALIGNMENT, /* An alignment has changed */
115 | CMP_BYTE_SIZE, /* Byte size has changed */
116 | CMP_NAMESPACE, /* Symbol namespace has changed */
117 | } cmp_ret_t;
118 |
119 | static int compare_two_files(const char *filename, const char *newfile,
120 | bool follow);
121 |
122 | static int cmp_node_reffile(obj_t *o1, obj_t *o2)
123 | {
124 | char *s1 = filenametotype(o1->base_type);
125 | char *s2 = filenametotype(o2->base_type);
126 | int len;
127 | bool ret;
128 |
129 | ret = safe_streq(s1, s2);
130 | free(s1);
131 | free(s2);
132 |
133 | if (!ret)
134 | return CMP_DIFF;
135 |
136 | /*
137 | * Compare the symbol referenced by file, but be careful not
138 | * to follow imaginary declaration path.
139 | *
140 | * TODO: This is quite wasteful. We reopen files and parse
141 | * them again many times.
142 | */
143 | len = strlen(DECLARATION_PATH);
144 | if (strncmp(o1->base_type, DECLARATION_PATH, len) &&
145 | strncmp(o2->base_type, DECLARATION_PATH, len) &&
146 | compare_two_files(o1->base_type, o2->base_type, true))
147 | return CMP_REFFILE;
148 |
149 | return CMP_SAME;
150 | }
151 |
152 | static int _cmp_nodes(obj_t *o1, obj_t *o2, bool search)
153 | {
154 | if ((o1->type != o2->type) ||
155 | !safe_streq(o1->name, o2->name) ||
156 | (is_weak(o1) != is_weak(o2)) ||
157 | (is_weak(o1) && is_weak(o2) && !safe_streq(o1->link, o2->link)) ||
158 | ((o1->ptr == NULL) != (o2->ptr == NULL)) ||
159 | (has_constant(o1) && (o1->constant != o2->constant)) ||
160 | (has_index(o1) && (o1->index != o2->index)) ||
161 | (is_bitfield(o1) != is_bitfield(o2)) ||
162 | (is_bitfield(o1) && ((o1->last_bit - o1->first_bit) !=
163 | (o2->last_bit - o2->first_bit))))
164 | return CMP_DIFF;
165 |
166 | if (o1->type == __type_reffile) {
167 | int ret;
168 |
169 | ret = cmp_node_reffile(o1, o2);
170 | if (ret)
171 | return ret;
172 | } else if (!safe_streq(o1->base_type, o2->base_type))
173 | return CMP_DIFF;
174 |
175 | if (has_offset(o1) &&
176 | ((o1->offset != o2->offset) ||
177 | (is_bitfield(o1) && (o1->first_bit != o2->first_bit)))) {
178 | if (search && o1->name == NULL)
179 | /*
180 | * This field is an unnamed struct or union. When
181 | * searching for a node, avoid to consider the next
182 | * unnamed struct or union to be the same one.
183 | */
184 | return CMP_DIFF;
185 | return CMP_OFFSET;
186 | }
187 |
188 | if (o1->alignment != o2->alignment)
189 | return CMP_ALIGNMENT;
190 |
191 | if (o1->byte_size != o2->byte_size)
192 | return CMP_BYTE_SIZE;
193 |
194 | if (!safe_streq(o1->ns, o2->ns))
195 | return CMP_NAMESPACE;
196 |
197 | return CMP_SAME;
198 | }
199 |
200 | static int cmp_nodes(obj_t *o1, obj_t *o2)
201 | {
202 | return _cmp_nodes(o1, o2, false);
203 | }
204 |
205 | typedef enum {
206 | DIFF_INSERT,
207 | DIFF_DELETE,
208 | DIFF_REPLACE,
209 | DIFF_CONT,
210 | } diff_ret_t;
211 |
212 | /*
213 | * When field are changed or moved around, there can be several diff
214 | * representations for the change. We are trying to keep the diff as
215 | * small as possible, while keeping most significant changes in term
216 | * of kABI (mainly shifted fields, which most likely indicate that
217 | * some change to the ABI have been overlooked).
218 | *
219 | * This function compare two lists whose first member diverge. We're
220 | * looking at four different scenarios:
221 | * - N fields appears only in list2, then the lists rejoined (insertion)
222 | * - P fields appears only in list1, then the lists rejoined (deletion)
223 | * - Q fields diverges, then the lists rejoined (replacement)
224 | * - the lists never rejoined
225 | *
226 | * Since for the same change, several of the scenarios above might
227 | * represent the change, we choose the one that minimize the diff
228 | * (min(N,P,Q)). So we're looking for the first element of list1 in
229 | * list2, the first element of list2 in list1 or the first line where
230 | * list1 and list2 do not differ, whichever comes first.
231 | */
232 | static diff_ret_t list_diff(obj_list_t *list1, obj_list_t **next1,
233 | obj_list_t *list2, obj_list_t **next2)
234 | {
235 | obj_t *o1 = list2->member, *o2 = list1->member, *o = o1;
236 | int d1 = 0, d2 = 0, ret;
237 | obj_list_t *next;
238 |
239 | next = *next1 = list1;
240 | *next2 = list2;
241 |
242 | while (next) {
243 | ret = _cmp_nodes(o, next->member, true);
244 | if (ret == CMP_SAME || ret == CMP_OFFSET
245 | || ret == CMP_ALIGNMENT) {
246 | if (o == o1)
247 | /* We find the first element of list2
248 | on list1, that is d1 elements have
249 | been removed from list1 */
250 | return DIFF_DELETE;
251 | else
252 | return DIFF_INSERT;
253 | }
254 |
255 | if (d1 == d2) {
256 | ret = _cmp_nodes((*next1)->member, (*next2)->member,
257 | true);
258 | if (ret == CMP_SAME || ret == CMP_OFFSET
259 | || ret == CMP_ALIGNMENT) {
260 | /* d1 fields have been replaced */
261 | return DIFF_REPLACE;
262 | }
263 |
264 | }
265 |
266 | if (!(*next1) || !((*next1)->next) || (d2 < d1)) {
267 | next = *next2 = (*next2)->next;
268 | o = o2;
269 | d2++;
270 | } else {
271 | next = *next1 = (*next1)->next;
272 | o = o1;
273 | d1++;
274 | }
275 | }
276 | return DIFF_CONT;
277 | }
278 |
279 | /*
280 | * We want to show practical output to the user. For instance if a
281 | * struct member type change, we want to show which struct member
282 | * changed type, not that somewhere a "signed int" has been changed
283 | * into a "unsigned bin".
284 | *
285 | * For now, we consider that a useful output should start at a named
286 | * object or at a struct field or var (the field/var itself may be
287 | * unamed, typically when it's an union or struct of alternative
288 | * elements but it most likely contains named element).
289 | */
290 | static bool worthy_of_print(obj_t *o)
291 | {
292 | return (o->name != NULL) ||
293 | (o->type == __type_struct_member) ||
294 | (o->type == __type_var);
295 | }
296 |
297 | static void print_two_nodes(const char *s, obj_t *o1, obj_t *o2, FILE *stream)
298 | {
299 |
300 | while (!worthy_of_print(o1)) {
301 | o1 = o1->parent;
302 | o2 = o2->parent;
303 | if ((o1 == NULL) || (o2 == NULL))
304 | fail("No ancestor worthy of print\n");
305 | }
306 | fprintf(stream, "%s:\n", s);
307 | obj_print_tree__prefix(o1, DEL_PREFIX, stream);
308 | obj_print_tree__prefix(o2, ADD_PREFIX, stream);
309 | }
310 |
311 | typedef struct compare_config_s {
312 | bool debug;
313 | bool hide_kabi;
314 | bool hide_kabi_new;
315 | bool skip_duplicate; /* Don't show multiple version of a symbol */
316 | int follow;
317 | char *old_dir;
318 | char *new_dir;
319 | char *filename;
320 | char **flist;
321 | int flistsz;
322 | int flistcnt;
323 | int ret;
324 | /*
325 | * The following options allow to hide some symbol changes in
326 | * kABI comparison. Hides...
327 | */
328 | int no_replaced; /* replaced symbols */
329 | int no_shifted; /* symbols whose offset shifted */
330 | int no_inserted; /* symbols inserted in the middle of a struct/union */
331 | int no_deleted; /* symbols removed in the middle (poke a hole) */
332 | int no_added; /* symbols added at the end of a struct/union... */
333 | int no_removed; /* symbols removed at the end of a struct/union... */
334 | int no_moved_files; /* file that has been moved (or removed) */
335 | } compare_config_t;
336 |
337 | compare_config_t compare_config = {false, false, false, false, 0,
338 | NULL, NULL, NULL, NULL,
339 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
340 |
341 | static void message_alignment_value(unsigned v, FILE *stream)
342 | {
343 | if (v == 0)
344 | fprintf(stream, "");
345 | else
346 | fprintf(stream, "%u", v);
347 | }
348 |
349 | static void message_byte_size_value(unsigned int v, FILE *stream)
350 | {
351 | if (v == 0)
352 | fprintf(stream, "");
353 | else
354 | fprintf(stream, "%u", v);
355 | }
356 |
357 | static void message_namespace_value(const char *ns, FILE *stream)
358 | {
359 | if (ns == NULL)
360 | fprintf(stream, "");
361 | else
362 | fprintf(stream, "%s", ns);
363 | }
364 |
365 | static void message_alignment(obj_t *o1, obj_t *o2, FILE *stream)
366 | {
367 | char *part_str;
368 |
369 | if (o1->type == __type_struct_member) {
370 | part_str = "field";
371 | } else {
372 | part_str = "symbol";
373 | }
374 |
375 | fprintf(stream, "The alignment of %s '%s' has changed from ",
376 | part_str, o1->name);
377 |
378 | message_alignment_value(o1->alignment, stream);
379 | fprintf(stream, " to ");
380 | message_alignment_value(o2->alignment, stream);
381 | fprintf(stream, "\n");
382 | }
383 |
384 | static void message_byte_size(obj_t *o1, obj_t *o2, FILE *stream)
385 | {
386 | fprintf(stream, "The byte size of symbol '%s' has changed from ",
387 | o1->name);
388 |
389 | message_byte_size_value(o1->byte_size, stream);
390 | fprintf(stream, " to ");
391 | message_byte_size_value(o2->byte_size, stream);
392 | fprintf(stream, "\n");
393 | }
394 |
395 | static void message_namespace(obj_t *o1, obj_t *o2, FILE *stream)
396 | {
397 | fprintf(stream, "The namespace of symbol '%s' has changed from ",
398 | o1->name);
399 |
400 | message_namespace_value(o1->ns, stream);
401 | fprintf(stream, " to ");
402 | message_namespace_value(o2->ns, stream);
403 | fprintf(stream, "\n");
404 | }
405 |
406 | static int _compare_tree(obj_t *o1, obj_t *o2, FILE *stream)
407 | {
408 | obj_list_t *list1 = NULL, *list2 = NULL;
409 | int ret = COMP_SAME, tmp;
410 |
411 | tmp = cmp_nodes(o1, o2);
412 | if (tmp) {
413 | if (tmp == CMP_REFFILE) {
414 | fprintf(stream, "symbol %s has changed\n",
415 | o1->base_type);
416 | ret = COMP_DIFF;
417 | } else if ((tmp == CMP_OFFSET && !compare_config.no_shifted) ||
418 | (tmp == CMP_DIFF && !compare_config.no_replaced)) {
419 | const char *s = (tmp == CMP_OFFSET) ?
420 | "Shifted" : "Replaced";
421 | print_two_nodes(s, o1, o2, stream);
422 | ret = COMP_CONT;
423 | } else if (tmp == CMP_ALIGNMENT) {
424 | message_alignment(o1, o2, stream);
425 | ret = COMP_CONT;
426 | } else if (tmp == CMP_BYTE_SIZE) {
427 | message_byte_size(o1, o2, stream);
428 | ret = COMP_CONT;
429 | } else if (tmp == CMP_NAMESPACE) {
430 | message_namespace(o1, o2, stream);
431 | ret = COMP_CONT;
432 | }
433 |
434 | if (ret == COMP_DIFF)
435 | return ret;
436 | }
437 |
438 | if (o1->member_list)
439 | list1 = o1->member_list->first;
440 | if (o2->member_list)
441 | list2 = o2->member_list->first;
442 |
443 | while (list1 && list2) {
444 | if (cmp_nodes(list1->member, list2->member) == CMP_DIFF) {
445 | int index;
446 | obj_list_t *next1, *next2;
447 |
448 | index = list_diff(list1, &next1, list2, &next2);
449 |
450 | switch (index) {
451 | case DIFF_INSERT:
452 | /* Insertion */
453 | if (!compare_config.no_inserted) {
454 | _print_node_list("Inserted", ADD_PREFIX,
455 | list2, next2, stream);
456 | ret = COMP_DIFF;
457 | }
458 | list2 = next2;
459 | break;
460 | case DIFF_DELETE:
461 | /* Removal */
462 | if (!compare_config.no_deleted) {
463 | _print_node_list("Deleted", DEL_PREFIX,
464 | list1, next1, stream);
465 | ret = COMP_DIFF;
466 | }
467 | list1 = next1;
468 | break;
469 | case DIFF_REPLACE:
470 | /*
471 | * We could print the diff here, but relying on
472 | * the next calls to _compare_tree() to display
473 | * the replaced fields individually works too.
474 | */
475 | case DIFF_CONT:
476 | /* Nothing to do */
477 | ;
478 | }
479 | }
480 |
481 | tmp =_compare_tree(list1->member, list2->member, stream);
482 | ret = comp_return_value(ret, tmp);
483 |
484 | list1 = list1->next;
485 | list2 = list2->next;
486 | if (!list1 && list2) {
487 | if (!compare_config.no_added) {
488 | print_node_list("Added", ADD_PREFIX,
489 | list2, stream);
490 | ret = COMP_DIFF;
491 | }
492 | return ret;
493 | }
494 | if (list1 && !list2) {
495 | if (!compare_config.no_removed) {
496 | print_node_list("Removed", DEL_PREFIX,
497 | list1, stream);
498 | ret = COMP_DIFF;
499 | }
500 | return ret;
501 | }
502 | }
503 |
504 | if (o1->ptr && o2->ptr) {
505 | tmp = _compare_tree(o1->ptr, o2->ptr, stream);
506 | ret = comp_return_value(ret, tmp);
507 | }
508 |
509 | return ret;
510 | }
511 |
512 | /*
513 | * Compare two symbols and show the difference in a c-like format
514 | */
515 | static int compare_tree(obj_t *o1, obj_t *o2, FILE *stream)
516 | {
517 | return _compare_tree(o1, o2, stream);
518 | }
519 |
520 | static bool push_file(const char *filename)
521 | {
522 | int i, sz = compare_config.flistsz;
523 | int cnt = compare_config.flistcnt;
524 | char **flist = compare_config.flist;
525 |
526 | for (i = 0; i < cnt; i++)
527 | if (!strcmp(flist[i], filename))
528 | return false;
529 |
530 | if (!sz) {
531 | compare_config.flistsz = sz = 16;
532 | compare_config.flist = flist =
533 | safe_zmalloc(16 * sizeof(char *));
534 | }
535 | if (cnt >= sz) {
536 | sz *= 2;
537 | compare_config.flistsz = sz;
538 | compare_config.flist = flist =
539 | safe_realloc(flist, sz * sizeof(char *));
540 | }
541 |
542 | flist[cnt] = strdup(filename);
543 | compare_config.flistcnt++;
544 |
545 | return true;
546 | }
547 |
548 | static void free_files()
549 | {
550 | int i;
551 |
552 | for (i = 0; i < compare_config.flistcnt; i++)
553 | free(compare_config.flist[i]);
554 | free(compare_config.flist);
555 | compare_config.flistcnt = compare_config.flistsz = 0;
556 | }
557 |
558 | static void compare_usage()
559 | {
560 | printf("Usage:\n"
561 | "\tcompare [options] kabi_dir kabi_dir [kabi_file...]\n"
562 | "\tcompare [options] kabi_file kabi_file\n"
563 | "\nOptions:\n"
564 | " -h, --help:\t\tshow this message\n"
565 | " -k, --hide-kabi:\thide changes made by RH_KABI_REPLACE()\n"
566 | " -n, --hide-kabi-new:\n\t\t\thide the kabi trickery made by"
567 | " RH_KABI_REPLACE, but show the new field\n"
568 | " -d, --debug:\tprint the raw tree\n"
569 | " --follow:\t\tfollow referenced symbols\n"
570 | " --no-offset:\tdon't display the offset of struct fields\n"
571 | " --no-replaced:\thide replaced symbols"
572 | " (symbols that changed, but hasn't moved)\n"
573 | " --no-shifted:\thide shifted symbols"
574 | " (symbol that hasn't changed, but whose offset changed)\n"
575 | " --no-inserted:\t"
576 | "hide symbols inserted in the middle of a struct, union...\n"
577 | " --no-deleted:\t"
578 | "hide symbols removed from the middle of a struct, union...\n"
579 | " --no-added:\t\t"
580 | "hide symbols added at the end of a struct, union...\n"
581 | " --no-removed:\t"
582 | "hide symbols removed from the end of a struct, union...\n"
583 | " --no-moved-files:\thide changes caused by symbols "
584 | "definition moving to another file\n\t\t\t"
585 | "Warning: it also hides symbols that are removed entirely\n"
586 | " -s, --skip-duplicate:\tshow only the first version of a "
587 | "symbol when several exist\n");
588 |
589 | exit(1);
590 | }
591 |
592 | /*
593 | * Parse two files and compare the resulting tree.
594 | *
595 | * filename: file to compare (relative to compare_config.*_dir)
596 | * newfile: if not NULL, the file to use in compare_config.new_dir,
597 | * otherwise, filename is used for both.
598 | * follow: Are we here because we followed a reference file? If so,
599 | * don't print anything and exit immediately if follow
600 | * option isn't set.
601 | */
602 | static int compare_two_files(const char *filename, const char *newfile,
603 | bool follow)
604 | {
605 | obj_t *root1, *root2;
606 | char *old_dir = compare_config.old_dir;
607 | char *new_dir = compare_config.new_dir;
608 | char *path1, *path2, *s = NULL;
609 | const char *filename2;
610 | FILE *file1, *file2, *stream;
611 | struct stat fstat;
612 | size_t sz;
613 | int ret = 0, tmp;
614 |
615 | if (follow && !compare_config.follow)
616 | return 0;
617 |
618 | /* Avoid infinite loop */
619 | if (!push_file(filename))
620 | return 0;
621 |
622 | safe_asprintf(&path1, "%s/%s", old_dir, filename);
623 | filename2 = newfile ? newfile : filename;
624 | safe_asprintf(&path2, "%s/%s", new_dir, filename2);
625 |
626 | if (stat(path2, &fstat) != 0) {
627 | if (errno == ENOENT) {
628 | /* Don't consider an incomplete definition a change */
629 | if (strncmp(filename2, DECLARATION_PATH,
630 | strlen(DECLARATION_PATH)) &&
631 | !compare_config.no_moved_files) {
632 | ret = EXIT_KABI_CHANGE;
633 | printf("Symbol removed or moved: %s\n",
634 | filename);
635 | }
636 |
637 | free(path1);
638 | free(path2);
639 |
640 | return ret;
641 | } else {
642 | fail("Failed to stat() file%s: %s\n",
643 | path2, strerror(errno));
644 | }
645 | }
646 |
647 | file1 = safe_fopen(path1);
648 | file2 = safe_fopen(path2);
649 |
650 | root1 = obj_parse(file1, path1);
651 | root2 = obj_parse(file2, path2);
652 |
653 | free(path1);
654 | free(path2);
655 |
656 | if (compare_config.hide_kabi) {
657 | obj_hide_kabi(root1, compare_config.hide_kabi_new);
658 | obj_hide_kabi(root2, compare_config.hide_kabi_new);
659 | }
660 |
661 | if (compare_config.debug && !follow) {
662 | obj_debug_tree(root1);
663 | obj_debug_tree(root2);
664 | }
665 |
666 | if (follow) {
667 | stream = fopen("/dev/null", "w");
668 | if (stream == NULL)
669 | fail("Unable to open /dev/null: %s\n", strerror(errno));
670 | } else {
671 | stream = open_memstream(&s, &sz);
672 | }
673 | tmp = compare_tree(root1, root2, stream);
674 |
675 | if (tmp != COMP_SAME) {
676 | if (!follow) {
677 | printf("Changes detected in: %s\n", filename);
678 | fflush(stream);
679 | fputs(s, stdout);
680 | putchar('\n');
681 | }
682 | ret = EXIT_KABI_CHANGE;
683 | }
684 |
685 | obj_free(root1);
686 | obj_free(root2);
687 | fclose(file1);
688 | fclose(file2);
689 | fclose(stream);
690 | free(s);
691 |
692 | return ret;
693 |
694 | }
695 |
696 | static walk_rv_t compare_files_cb(char *kabi_path, void *arg)
697 | {
698 | compare_config_t *conf = (compare_config_t *)arg;
699 | char *filename;
700 |
701 | if (compare_config.skip_duplicate && is_duplicate(kabi_path))
702 | return WALK_CONT;
703 |
704 | /* If conf->*_dir contains slashes, skip them */
705 | filename = kabi_path + strlen(conf->old_dir);
706 | while (*filename == '/')
707 | filename++;
708 |
709 | free_files();
710 | if (compare_two_files(filename, NULL, false))
711 | conf->ret = EXIT_KABI_CHANGE;
712 |
713 | return WALK_CONT;
714 | }
715 |
716 | #define COMPARE_NO_OPT(name) \
717 | {"no-"#name, no_argument, &compare_config.no_##name, 1}
718 |
719 | /*
720 | * Performs the compare command
721 | */
722 | int compare(int argc, char **argv)
723 | {
724 | int opt, opt_index;
725 | char *old_dir, *new_dir;
726 | struct stat sb1, sb2;
727 | struct option loptions[] = {
728 | {"debug", no_argument, 0, 'd'},
729 | {"hide-kabi", no_argument, 0, 'k'},
730 | {"hide-kabi-new", no_argument, 0, 'n'},
731 | {"help", no_argument, 0, 'h'},
732 | {"skip-duplicate", no_argument, 0, 's'},
733 | {"follow", no_argument, &compare_config.follow, 1},
734 | {"no-offset", no_argument, &display_options.no_offset, 1},
735 | COMPARE_NO_OPT(replaced),
736 | COMPARE_NO_OPT(shifted),
737 | COMPARE_NO_OPT(inserted),
738 | COMPARE_NO_OPT(deleted),
739 | COMPARE_NO_OPT(added),
740 | COMPARE_NO_OPT(removed),
741 | {"no-moved-files", no_argument,
742 | &compare_config.no_moved_files, 1},
743 | {0, 0, 0, 0}
744 | };
745 |
746 | memset(&display_options, 0, sizeof(display_options));
747 |
748 | while ((opt = getopt_long(argc, argv, "dknhs",
749 | loptions, &opt_index)) != -1) {
750 | switch (opt) {
751 | case 0:
752 | break;
753 | case 'd':
754 | compare_config.debug = true;
755 | break;
756 | case 'n':
757 | compare_config.hide_kabi_new = true;
758 | /* fall through */
759 | case 'k':
760 | compare_config.hide_kabi = true;
761 | break;
762 | case 's':
763 | compare_config.skip_duplicate = true;
764 | break;
765 | case 'h':
766 | default:
767 | compare_usage();
768 | }
769 | }
770 |
771 | if (argc < optind + 2) {
772 | printf("Wrong number of argument\n");
773 | compare_usage();
774 | }
775 |
776 | old_dir = compare_config.old_dir = argv[optind++];
777 | new_dir = compare_config.new_dir = argv[optind++];
778 |
779 | if ((stat(old_dir, &sb1) == -1) || (stat(new_dir, &sb2) == -1))
780 | fail("stat failed: %s\n", strerror(errno));
781 |
782 | if (S_ISREG(sb1.st_mode) && S_ISREG(sb2.st_mode)) {
783 | char *oldname = basename(old_dir);
784 | char *newname = basename(new_dir);
785 |
786 | if (optind != argc) {
787 | printf("Too many arguments\n");
788 | compare_usage();
789 | }
790 | compare_config.old_dir = dirname(old_dir);
791 | compare_config.new_dir = dirname(new_dir);
792 |
793 | return compare_two_files(oldname, newname, false);
794 | }
795 |
796 | if (!S_ISDIR(sb1.st_mode) || !S_ISDIR(sb2.st_mode)) {
797 | printf("Compare takes two directories or two regular"
798 | " files as arguments\n");
799 | compare_usage();
800 | }
801 |
802 | if (optind == argc) {
803 | walk_dir(old_dir, false, compare_files_cb, &compare_config);
804 |
805 | return compare_config.ret;
806 | }
807 |
808 | while (optind < argc) {
809 | char *path, *filename;
810 |
811 | filename = compare_config.filename = argv[optind++];
812 | safe_asprintf(&path, "%s/%s", old_dir, filename);
813 |
814 | if (stat(path, &sb1) == -1) {
815 | if (errno == ENOENT)
816 | fail("file does not exist: %s\n", path);
817 | fail("stat failed: %s\n", strerror(errno));
818 | }
819 |
820 | if (!S_ISREG(sb1.st_mode)) {
821 | printf("Compare third argument must be a regular file");
822 | compare_usage();
823 | }
824 | free(path);
825 |
826 | if (compare_two_files(filename, NULL, false))
827 | compare_config.ret = EXIT_KABI_CHANGE;
828 | }
829 |
830 | return compare_config.ret;
831 | }
832 |
--------------------------------------------------------------------------------
/compare.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright(C) 2017, Red Hat, Inc.
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | #ifndef KABI_DW_COMAPRE_H_
19 | #define KABI_DW_COMAPRE_H_
20 |
21 | /* Return value when we detect a kABI change */
22 | #define EXIT_KABI_CHANGE 2
23 |
24 | int compare(int argc, char **argv);
25 |
26 | #endif
27 |
--------------------------------------------------------------------------------
/generate.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright(C) 2016, Red Hat, Inc., Stanislav Kozina
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | #ifndef GENERATE_H_
19 | #define GENERATE_H_
20 |
21 | #include "main.h"
22 |
23 | void generate(int argc, char **argv);
24 |
25 | #endif /* GENERATE_H_ */
26 |
--------------------------------------------------------------------------------
/hash.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Borrowed from libkmod - interface to kernel module operations
3 | *
4 | * Copyright (C) 2011-2013 ProFUSION embedded systems
5 | *
6 | * This library is free software; you can redistribute it and/or
7 | * modify it under the terms of the GNU Lesser General Public
8 | * License as published by the Free Software Foundation; either
9 | * version 2.1 of the License, or (at your option) any later version.
10 | *
11 | * This library is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 | * Lesser General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU Lesser General Public
17 | * License along with this library; if not, see .
18 | */
19 |
20 | #include
21 | #include
22 | #include
23 | #include
24 |
25 | #include "hash.h"
26 |
27 | /* endianess and alignments */
28 | /* taken from kmod shared/util.h */
29 | /* ************************************************************************ */
30 | #define get_unaligned(ptr) \
31 | ({ \
32 | struct __attribute__((packed)) { \
33 | typeof(*(ptr)) __v; \
34 | } *__p = (typeof(__p)) (ptr); \
35 | __p->__v; \
36 | })
37 |
38 | #define put_unaligned(val, ptr) \
39 | do { \
40 | struct __attribute__((packed)) { \
41 | typeof(*(ptr)) __v; \
42 | } *__p = (typeof(__p)) (ptr); \
43 | __p->__v = (val); \
44 | } while (0)
45 |
46 | static inline unsigned int ALIGN_POWER2(unsigned int u)
47 | {
48 | return 1 << ((sizeof(u) * 8) - __builtin_clz(u - 1));
49 | }
50 |
51 | static inline size_t min(size_t a, size_t b)
52 | {
53 | return a < b ? a : b;
54 | }
55 |
56 | struct hash_entry {
57 | const char *key;
58 | size_t keylen;
59 | const void *value;
60 | };
61 |
62 | struct hash_bucket {
63 | struct hash_entry *entries;
64 | unsigned int used;
65 | unsigned int total;
66 | };
67 |
68 | struct hash {
69 | unsigned int count;
70 | unsigned int step;
71 | unsigned int n_buckets;
72 | void (*free_value)(void *value);
73 | struct hash_bucket buckets[];
74 | };
75 |
76 | static inline int hash_key_cmp(const char *key1, size_t size1,
77 | const char *key2, size_t size2)
78 | {
79 | int rc;
80 | size_t size;
81 |
82 | size = min(size1, size2);
83 |
84 | rc = memcmp(key1, key2, size);
85 | if (rc != 0)
86 | return rc;
87 |
88 | return (int)(size1 - size2);
89 | }
90 |
91 | struct hash *hash_new(unsigned int n_buckets,
92 | void (*free_value)(void *value))
93 | {
94 | struct hash *hash;
95 |
96 | n_buckets = ALIGN_POWER2(n_buckets);
97 | hash = calloc(1, sizeof(struct hash) +
98 | n_buckets * sizeof(struct hash_bucket));
99 | if (hash == NULL)
100 | return NULL;
101 | hash->n_buckets = n_buckets;
102 | hash->free_value = free_value;
103 | hash->step = n_buckets / 32;
104 | if (hash->step == 0)
105 | hash->step = 4;
106 | else if (hash->step > 64)
107 | hash->step = 64;
108 | return hash;
109 | }
110 |
111 | void hash_free(struct hash *hash)
112 | {
113 | struct hash_bucket *bucket, *bucket_end;
114 |
115 | if (hash == NULL)
116 | return;
117 |
118 | bucket = hash->buckets;
119 | bucket_end = bucket + hash->n_buckets;
120 | for (; bucket < bucket_end; bucket++) {
121 | if (hash->free_value) {
122 | struct hash_entry *entry, *entry_end;
123 | entry = bucket->entries;
124 | entry_end = entry + bucket->used;
125 | for (; entry < entry_end; entry++)
126 | hash->free_value((void *)entry->value);
127 | }
128 | free(bucket->entries);
129 | }
130 | free(hash);
131 | }
132 |
133 | static inline unsigned int hash_superfast(const char *key, unsigned int len)
134 | {
135 | /* Paul Hsieh (http://www.azillionmonkeys.com/qed/hash.html)
136 | * used by WebCore (http://webkit.org/blog/8/hashtables-part-2/)
137 | * EFL's eina and possible others.
138 | */
139 | unsigned int tmp, hash = len, rem = len & 3;
140 |
141 | len /= 4;
142 |
143 | /* Main loop */
144 | for (; len > 0; len--) {
145 | hash += get_unaligned((uint16_t *) key);
146 | tmp = (get_unaligned((uint16_t *)(key + 2)) << 11) ^ hash;
147 | hash = (hash << 16) ^ tmp;
148 | key += 4;
149 | hash += hash >> 11;
150 | }
151 |
152 | /* Handle end cases */
153 | switch (rem) {
154 | case 3:
155 | hash += get_unaligned((uint16_t *) key);
156 | hash ^= hash << 16;
157 | hash ^= key[2] << 18;
158 | hash += hash >> 11;
159 | break;
160 |
161 | case 2:
162 | hash += get_unaligned((uint16_t *) key);
163 | hash ^= hash << 11;
164 | hash += hash >> 17;
165 | break;
166 |
167 | case 1:
168 | hash += *key;
169 | hash ^= hash << 10;
170 | hash += hash >> 1;
171 | }
172 |
173 | /* Force "avalanching" of final 127 bits */
174 | hash ^= hash << 3;
175 | hash += hash >> 5;
176 | hash ^= hash << 4;
177 | hash += hash >> 17;
178 | hash ^= hash << 25;
179 | hash += hash >> 6;
180 |
181 | return hash;
182 | }
183 |
184 | /*
185 | * add or replace key in hash map.
186 | *
187 | * none of key or value are copied, just references are remembered as is,
188 | * make sure they are live while pair exists in hash!
189 | */
190 | int hash_add_bin(struct hash *hash,
191 | const char *key, size_t keylen, const void *value)
192 | {
193 | unsigned int hashval = hash_superfast(key, keylen);
194 | unsigned int pos = hashval & (hash->n_buckets - 1);
195 | struct hash_bucket *bucket = hash->buckets + pos;
196 | struct hash_entry *entry, *entry_end;
197 |
198 | if (bucket->used + 1 >= bucket->total) {
199 | unsigned new_total = bucket->total + hash->step;
200 | size_t size = new_total * sizeof(struct hash_entry);
201 | struct hash_entry *tmp = realloc(bucket->entries, size);
202 | if (tmp == NULL)
203 | return -errno;
204 | bucket->entries = tmp;
205 | bucket->total = new_total;
206 | }
207 |
208 | entry = bucket->entries;
209 | entry_end = entry + bucket->used;
210 | for (; entry < entry_end; entry++) {
211 | int c = hash_key_cmp(key, keylen, entry->key, entry->keylen);
212 | if (c == 0) {
213 | if (hash->free_value)
214 | hash->free_value((void *)entry->value);
215 | entry->key = key;
216 | entry->keylen = keylen;
217 | entry->value = value;
218 | return 0;
219 | } else if (c < 0) {
220 | memmove(entry + 1, entry,
221 | (entry_end - entry) * sizeof(struct hash_entry));
222 | break;
223 | }
224 | }
225 |
226 | entry->key = key;
227 | entry->keylen = keylen;
228 | entry->value = value;
229 | bucket->used++;
230 | hash->count++;
231 | return 0;
232 | }
233 |
234 | /* similar to hash_add(), but fails if key already exists */
235 | int hash_add_unique_bin(struct hash *hash,
236 | const char *key, size_t keylen, const void *value)
237 | {
238 | unsigned int hashval = hash_superfast(key, keylen);
239 | unsigned int pos = hashval & (hash->n_buckets - 1);
240 | struct hash_bucket *bucket = hash->buckets + pos;
241 | struct hash_entry *entry, *entry_end;
242 |
243 | if (bucket->used + 1 >= bucket->total) {
244 | unsigned new_total = bucket->total + hash->step;
245 | size_t size = new_total * sizeof(struct hash_entry);
246 | struct hash_entry *tmp = realloc(bucket->entries, size);
247 | if (tmp == NULL)
248 | return -errno;
249 | bucket->entries = tmp;
250 | bucket->total = new_total;
251 | }
252 |
253 | entry = bucket->entries;
254 | entry_end = entry + bucket->used;
255 | for (; entry < entry_end; entry++) {
256 | int c = hash_key_cmp(key, keylen, entry->key, entry->keylen);
257 | if (c == 0)
258 | return -EEXIST;
259 | else if (c < 0) {
260 | memmove(entry + 1, entry,
261 | (entry_end - entry) * sizeof(struct hash_entry));
262 | break;
263 | }
264 | }
265 |
266 | entry->key = key;
267 | entry->keylen = keylen;
268 | entry->value = value;
269 | bucket->used++;
270 | hash->count++;
271 | return 0;
272 | }
273 |
274 | static int hash_entry_cmp(const void *pa, const void *pb)
275 | {
276 | const struct hash_entry *a = pa;
277 | const struct hash_entry *b = pb;
278 | return hash_key_cmp(a->key, a->keylen, b->key, b->keylen);
279 | }
280 |
281 | void *hash_find_bin(const struct hash *hash, const char *key, size_t keylen)
282 | {
283 | unsigned int hashval = hash_superfast(key, keylen);
284 | unsigned int pos = hashval & (hash->n_buckets - 1);
285 | const struct hash_bucket *bucket = hash->buckets + pos;
286 | const struct hash_entry se = {
287 | .key = key,
288 | .keylen = keylen,
289 | .value = NULL
290 | };
291 | const struct hash_entry *entry = bsearch(
292 | &se, bucket->entries, bucket->used,
293 | sizeof(struct hash_entry), hash_entry_cmp);
294 | if (entry == NULL)
295 | return NULL;
296 | return (void *)entry->value;
297 | }
298 |
299 | int hash_del_bin(struct hash *hash, const char *key, size_t keylen)
300 | {
301 | unsigned int hashval = hash_superfast(key, keylen);
302 | unsigned int pos = hashval & (hash->n_buckets - 1);
303 | unsigned int steps_used, steps_total;
304 | struct hash_bucket *bucket = hash->buckets + pos;
305 | struct hash_entry *entry, *entry_end;
306 | const struct hash_entry se = {
307 | .key = key,
308 | .keylen = keylen,
309 | .value = NULL
310 | };
311 |
312 | entry = bsearch(&se, bucket->entries, bucket->used,
313 | sizeof(struct hash_entry), hash_entry_cmp);
314 | if (entry == NULL)
315 | return -ENOENT;
316 |
317 | if (hash->free_value)
318 | hash->free_value((void *)entry->value);
319 |
320 | entry_end = bucket->entries + bucket->used;
321 | memmove(entry, entry + 1,
322 | (entry_end - entry) * sizeof(struct hash_entry));
323 |
324 | bucket->used--;
325 | hash->count--;
326 |
327 | steps_used = bucket->used / hash->step;
328 | steps_total = bucket->total / hash->step;
329 | if (steps_used + 1 < steps_total) {
330 | size_t size = (steps_used + 1) *
331 | hash->step * sizeof(struct hash_entry);
332 | struct hash_entry *tmp = realloc(bucket->entries, size);
333 | if (tmp) {
334 | bucket->entries = tmp;
335 | bucket->total = (steps_used + 1) * hash->step;
336 | }
337 | }
338 |
339 | return 0;
340 | }
341 |
342 | unsigned int hash_get_count(const struct hash *hash)
343 | {
344 | return hash->count;
345 | }
346 |
347 | void hash_iter_init(const struct hash *hash, struct hash_iter *iter)
348 | {
349 | iter->hash = hash;
350 | iter->bucket = 0;
351 | iter->entry = -1;
352 | }
353 |
354 | bool hash_iter_next_bin(struct hash_iter *iter, const char **key,
355 | size_t *keylen,
356 | const void **value)
357 | {
358 | const struct hash_bucket *b = iter->hash->buckets + iter->bucket;
359 | const struct hash_entry *e;
360 |
361 | iter->entry++;
362 |
363 | if (iter->entry >= b->used) {
364 | iter->entry = 0;
365 |
366 | for (iter->bucket++; iter->bucket < iter->hash->n_buckets;
367 | iter->bucket++) {
368 | b = iter->hash->buckets + iter->bucket;
369 |
370 | if (b->used > 0)
371 | break;
372 | }
373 |
374 | if (iter->bucket >= iter->hash->n_buckets)
375 | return false;
376 | }
377 |
378 | e = b->entries + iter->entry;
379 |
380 | if (value != NULL)
381 | *value = e->value;
382 | if (key != NULL)
383 | *key = e->key;
384 | if (keylen != NULL)
385 | *keylen = e->keylen;
386 |
387 | return true;
388 | }
389 |
--------------------------------------------------------------------------------
/hash.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include
5 |
6 | struct hash;
7 |
8 | struct hash_iter {
9 | const struct hash *hash;
10 | unsigned int bucket;
11 | unsigned int entry;
12 | };
13 |
14 | struct hash *hash_new(unsigned int n_buckets, void (*free_value)(void *value));
15 | void hash_free(struct hash *hash);
16 | int hash_add_bin(struct hash *hash,
17 | const char *key, size_t keylen, const void *value);
18 | int hash_add_unique_bin(struct hash *hash,
19 | const char *key, size_t keylen, const void *value);
20 | int hash_del_bin(struct hash *hash, const char *key, size_t keylen);
21 | void *hash_find_bin(const struct hash *hash, const char *key, size_t keylen);
22 | unsigned int hash_get_count(const struct hash *hash);
23 | void hash_iter_init(const struct hash *hash, struct hash_iter *iter);
24 | bool hash_iter_next_bin(struct hash_iter *iter, const char **key, size_t *keylen,
25 | const void **value);
26 |
27 |
28 | static inline int hash_add(struct hash *hash, const char *key, const void *value)
29 | {
30 | size_t keylen = strlen(key);
31 | return hash_add_bin(hash, key, keylen, value);
32 | }
33 | static inline int hash_add_unique(struct hash *hash,
34 | const char *key, const void *value)
35 | {
36 | size_t keylen = strlen(key);
37 | return hash_add_unique_bin(hash, key, keylen, value);
38 | }
39 | static inline int hash_del(struct hash *hash, const char *key)
40 | {
41 | size_t keylen = strlen(key);
42 | return hash_del_bin(hash, key, keylen);
43 | }
44 | static inline void *hash_find(const struct hash *hash, const char *key)
45 | {
46 | size_t keylen = strlen(key);
47 | return hash_find_bin(hash, key, keylen);
48 | }
49 | static inline bool hash_iter_next(struct hash_iter *iter, const char **key,
50 | const void **value)
51 | {
52 | return hash_iter_next_bin(iter, key, NULL, value);
53 | }
54 |
--------------------------------------------------------------------------------
/ksymtab.c:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright(C) 2016, Red Hat, Inc., Stanislav Kozina
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | /*
19 | * This file contains the code which reads the __ksymtab section of the kernel
20 | * binaries to ensure that the symbol we parse is actually exported using the
21 | * EXPORT_SYMBOL() macro.
22 | */
23 |
24 | #include
25 | #include
26 | #include
27 | #include
28 | #include
29 | #include
30 | #include
31 | #include
32 | #include
33 | #include
34 |
35 | #include
36 | #include
37 | #include "main.h"
38 | #include "utils.h"
39 | #include "hash.h"
40 | #include "ksymtab.h"
41 |
42 | #define KSYMTAB "__ksymtab"
43 | #define KSYMTAB_GPL "__ksymtab_gpl"
44 | #define KSYMTAB_STRINGS "__ksymtab_strings"
45 | #define SYMTAB ".symtab"
46 | #define STRTAB ".strtab"
47 | #define STRTAB_NS_PREFIX "__kstrtabns_"
48 | #define KSYMTAB_PREFIX "__ksymtab_"
49 |
50 | #define KSYMTAB_SIZE 8192
51 |
52 | struct ksymtab {
53 | struct hash *hash;
54 | size_t mark_count;
55 | Elf64_Addr addr;
56 | };
57 |
58 | struct ksym;
59 |
60 | static Elf64_Addr elf_get_section(Elf *elf,
61 | size_t shstrndx,
62 | const char *section,
63 | const char **d_data,
64 | size_t *size)
65 | {
66 | Elf_Scn *scn;
67 | GElf_Shdr shdr;
68 | char *name;
69 | Elf_Data *data;
70 |
71 | scn = elf_nextscn(elf, NULL);
72 | for (; scn != NULL; scn = elf_nextscn(elf, scn)) {
73 | if (gelf_getshdr(scn, &shdr) != &shdr)
74 | fail("getshdr() failed: %s\n", elf_errmsg(-1));
75 | name = elf_strptr(elf, shstrndx, shdr.sh_name);
76 | if (name == NULL)
77 | fail("elf_strptr() failed: %s\n", elf_errmsg(-1));
78 |
79 | if (strcmp(name, section) == 0)
80 | break;
81 | }
82 |
83 | if (scn == NULL) /* no suitable section */
84 | return -1;
85 |
86 | /*
87 | * This is unlucky. Fedora/EL builds -debuginfo packages by running
88 | * eu-strip --reloc-debug-sections which places only standard .debug*
89 | * sections into the -debuginfo modules. The sections which cannot
90 | * be stripped completely (because they are allocated) are changed
91 | * to SHT_NOBITS type to indicate you need to look in the original
92 | * (non-debug) module for them. But those are xzipped.
93 | * So we reject such stuff. We only support fresh output from the
94 | * kernel build.
95 | */
96 | if (shdr.sh_type == SHT_NOBITS) {
97 | printf("The %s section has type SHT_NOBITS. Most likely you're "
98 | "running this tool on modules coming from kernel-debuginfo "
99 | "packages. They don't contain the %s section, you need to "
100 | "use the raw modules before they are stripped\n", section,
101 | section);
102 | exit(1);
103 | }
104 |
105 | if (gelf_getshdr(scn, &shdr) != &shdr)
106 | fail("getshdr() failed: %s\n", elf_errmsg(-1));
107 |
108 | data = elf_getdata(scn, NULL);
109 | if (data == NULL || data->d_size == 0)
110 | fail("%s section empty!\n", section);
111 |
112 | *d_data = data->d_buf;
113 | *size = data->d_size;
114 |
115 | return shdr.sh_addr;
116 | }
117 |
118 | struct elf_data *elf_open(const char *filename)
119 | {
120 | Elf *elf;
121 | int fd;
122 | int class;
123 | GElf_Ehdr *ehdr;
124 | size_t shstrndx;
125 | struct elf_data *data = NULL;
126 |
127 | if (elf_version(EV_CURRENT) == EV_NONE)
128 | fail("elf_version() failed: %s\n", elf_errmsg(-1));
129 |
130 | fd = open(filename, O_RDONLY, 0);
131 | if (fd < 0)
132 | fail("Failed to open file %s: %s\n", filename,
133 | strerror(errno));
134 |
135 | elf = elf_begin(fd, ELF_C_READ, NULL);
136 | if (elf == NULL)
137 | fail("elf_begin() failed: %s\n", elf_errmsg(-1));
138 |
139 | if (elf_kind(elf) != ELF_K_ELF) {
140 | printf("Doesn't look like an ELF file, ignoring: %s\n",
141 | filename);
142 | (void) elf_end(elf);
143 | (void) close(fd);
144 | goto out;
145 | }
146 |
147 | ehdr = safe_zmalloc(sizeof(*ehdr));
148 |
149 | if (gelf_getehdr(elf, ehdr) == NULL)
150 | fail("getehdr() failed: %s\n", elf_errmsg(-1));
151 |
152 | class = gelf_getclass(elf);
153 | if (class != ELFCLASS64) {
154 | printf("Unsupported elf class of %s: %d\n", filename, class);
155 | free(ehdr);
156 | (void) elf_end(elf);
157 | (void) close(fd);
158 | goto out;
159 | }
160 |
161 | /*
162 | * Get section index of the string table associated with the section
163 | * headers in the ELF file.
164 | * Required by elf_get_section calls.
165 | */
166 | if (elf_getshdrstrndx(elf, &shstrndx) != 0)
167 | fail("elf_getshdrstrndx() failed: %s\n", elf_errmsg(-1))
168 |
169 | data = safe_zmalloc(sizeof(*data));
170 |
171 | data->fd = fd;
172 | data->elf = elf;
173 | data->ehdr = ehdr;
174 | data->shstrndx = shstrndx;
175 | out:
176 | return data;
177 | }
178 |
179 | void elf_close(struct elf_data *ed)
180 | {
181 | if (ed == NULL)
182 | return;
183 | (void) elf_end(ed->elf);
184 | (void) close(ed->fd);
185 | }
186 |
187 | static inline bool elf_iter_global_weak(int st_bind)
188 | {
189 | return st_bind == STB_GLOBAL || st_bind == STB_WEAK;
190 | }
191 |
192 | static inline bool elf_iter_local(int st_bind)
193 | {
194 | return st_bind == STB_LOCAL;
195 | }
196 |
197 | static void elf_for_each_sym(struct elf_data *ed,
198 | void (*fn)(const char *name,
199 | uint64_t value,
200 | int binding,
201 | void *ctx),
202 | void *ctx,
203 | bool (*binding_match)(int))
204 | {
205 | const Elf64_Sym *end;
206 | Elf64_Sym *sym;
207 | int binding;
208 | const char *name;
209 | const char *data;
210 | size_t size;
211 |
212 | if (elf_get_section(ed->elf, ed->shstrndx, SYMTAB, &data, &size) == -1)
213 | return;
214 |
215 | sym = (Elf64_Sym *)data;
216 | end = (Elf64_Sym *)(data + size);
217 |
218 | sym++; /* skip first zero record */
219 | for (; sym < end; sym++) {
220 |
221 | binding = ELF64_ST_BIND(sym->st_info);
222 |
223 | if (!binding_match(binding))
224 | continue;
225 |
226 | if (sym->st_name == 0)
227 | continue;
228 |
229 | if (sym->st_name > ed->strtab_size)
230 | fail("Symbol name index %d out of range %ld\n",
231 | sym->st_name, ed->strtab_size);
232 |
233 | name = ed->strtab + sym->st_name;
234 | if (name == NULL)
235 | fail("Could not find symbol name\n");
236 |
237 | fn(name, sym->st_value, binding, ctx);
238 | }
239 | }
240 |
241 | static inline void elf_for_each_global_sym(struct elf_data *ed,
242 | void (*fn)(const char *name,
243 | uint64_t value,
244 | int binding,
245 | void *ctx),
246 | void *ctx)
247 | {
248 | return elf_for_each_sym(ed, fn, ctx, elf_iter_global_weak);
249 | }
250 |
251 | void ksymtab_ksym_mark(struct ksym *ksym)
252 | {
253 | if (!ksym->mark)
254 | ksym->ksymtab->mark_count++;
255 | ksym->mark = true;
256 | }
257 |
258 | static void ksymtab_ksym_free(void *arg)
259 | {
260 | struct ksym *ksym = arg;
261 |
262 | free(ksym->link);
263 | free(ksym->ns);
264 | free(ksym);
265 | }
266 |
267 | void ksymtab_free(struct ksymtab *ksymtab)
268 | {
269 | struct hash *h;
270 |
271 | if (ksymtab == NULL)
272 | return;
273 |
274 | h = ksymtab->hash;
275 |
276 | hash_free(h);
277 | free(ksymtab);
278 | }
279 |
280 | struct ksymtab *ksymtab_new(size_t size)
281 | {
282 | struct hash *h;
283 | struct ksymtab *ksymtab;
284 |
285 | h = hash_new(size, ksymtab_ksym_free);
286 | assert(h != NULL);
287 |
288 | ksymtab = safe_zmalloc(sizeof(*ksymtab));
289 | ksymtab->hash = h;
290 | /* ksymtab->mark_count is zeroed by the allocator */
291 |
292 | return ksymtab;
293 | }
294 |
295 | struct ksym *ksymtab_add_sym(struct ksymtab *ksymtab,
296 | const char *str,
297 | size_t len,
298 | uint64_t value)
299 | {
300 | struct hash *h = ksymtab->hash;
301 | struct ksym *ksym;
302 |
303 | ksym = safe_zmalloc(sizeof(*ksym) + len + 1);
304 | memcpy(ksym->key, str, len);
305 | ksym->key[len] = '\0';
306 | ksym->value = value;
307 | ksym->ksymtab = ksymtab;
308 | ksym->ns = NULL;
309 | /* ksym->link is zeroed by the allocator */
310 | hash_add(h, ksym->key, ksym);
311 |
312 | return ksym;
313 | }
314 |
315 | struct ksym *ksymtab_copy_sym(struct ksymtab *ksymtab, struct ksym *ksym)
316 | {
317 | const char *name = ksymtab_ksym_get_name(ksym);
318 | uint64_t value = ksymtab_ksym_get_value(ksym);
319 | char *link = ksymtab_ksym_get_link(ksym);
320 | struct ksym *new;
321 |
322 | new = ksymtab_add_sym(ksymtab, name, strlen(name), value);
323 | ksymtab_ksym_set_link(new, link);
324 |
325 | return new;
326 | }
327 |
328 | struct ksym *ksymtab_find(struct ksymtab *ksymtab, const char *name)
329 | {
330 | struct ksym *v;
331 | struct hash *h = ksymtab->hash;
332 |
333 | if (name == NULL)
334 | return NULL;
335 |
336 | v = hash_find(h, name);
337 | if (v == NULL)
338 | return NULL;
339 |
340 | return v;
341 | }
342 |
343 | size_t ksymtab_len(struct ksymtab *ksymtab)
344 | {
345 | struct hash *h;
346 |
347 | if (ksymtab == NULL)
348 | return 0;
349 |
350 | h = ksymtab->hash;
351 | return hash_get_count(h);
352 | }
353 |
354 | size_t ksymtab_mark_count(struct ksymtab *ksymtab)
355 | {
356 | return ksymtab->mark_count;
357 | }
358 |
359 | void ksymtab_for_each(struct ksymtab *ksymtab,
360 | void (*f)(struct ksym *, void *),
361 | void *ctx)
362 | {
363 | struct hash *h;
364 | struct hash_iter iter;
365 | const void *v;
366 | struct ksym *vv;
367 |
368 | if (ksymtab == NULL)
369 | return;
370 |
371 | h = ksymtab->hash;
372 |
373 | hash_iter_init(h, &iter);
374 | while (hash_iter_next(&iter, NULL, &v)) {
375 | vv = (struct ksym *)v;
376 | f(vv, ctx);
377 | }
378 | }
379 |
380 | struct ksymtab_section
381 | {
382 | size_t size;
383 | Elf64_Addr addr;
384 | };
385 |
386 | struct ksymtab_symbol_filter_ctx
387 | {
388 | struct ksymtab *ksymtab;
389 | struct ksymtab_section sec;
390 | struct ksymtab_section sec_gpl;
391 | };
392 |
393 | static int ksymtab_in_section(Elf64_Addr addr, struct ksymtab_section *sec)
394 | {
395 | return sec->addr != -1 &&
396 | addr >= sec->addr &&
397 | addr < sec->addr + sec->size;
398 | }
399 |
400 | static void ksymtab_symbol_filter(const char *name, uint64_t value, int bind, void *_ctx)
401 | {
402 | struct ksymtab_symbol_filter_ctx *ctx = (struct ksymtab_symbol_filter_ctx *)_ctx;
403 | static size_t i = 0;
404 |
405 | if (strncmp(name, KSYMTAB_PREFIX, strlen(KSYMTAB_PREFIX)))
406 | return;
407 |
408 | if (!ksymtab_in_section(value, &ctx->sec) &&
409 | !ksymtab_in_section(value, &ctx->sec_gpl))
410 | return;
411 |
412 | name += strlen(KSYMTAB_PREFIX);
413 | ksymtab_add_sym(ctx->ksymtab, name, strlen(name), i++);
414 | }
415 |
416 | static struct ksymtab *parse_ksymtab_symbols(struct elf_data *data)
417 | {
418 | struct ksymtab_symbol_filter_ctx ctx;
419 | const char *unused;
420 |
421 | ctx.ksymtab = ksymtab_new(KSYMTAB_SIZE);
422 |
423 | ctx.sec.addr = elf_get_section(data->elf,
424 | data->shstrndx,
425 | KSYMTAB,
426 | &unused,
427 | &ctx.sec.size);
428 |
429 | ctx.sec_gpl.addr = elf_get_section(data->elf,
430 | data->shstrndx,
431 | KSYMTAB_GPL,
432 | &unused,
433 | &ctx.sec_gpl.size);
434 |
435 | if (ctx.sec.addr != -1 || ctx.sec_gpl.addr != -1)
436 | elf_for_each_sym(data,
437 | ksymtab_symbol_filter,
438 | (void *) &ctx,
439 | elf_iter_local);
440 |
441 | return ctx.ksymtab;
442 | }
443 |
444 | struct ns_filter_ctx {
445 | const char *ksymtab_strings;
446 | struct ksymtab *ksymtab;
447 | Elf64_Half e_type;
448 | Elf64_Addr sh_addr;
449 | };
450 |
451 | static void ns_filter(const char *name, uint64_t value, int bind, void *_ctx)
452 | {
453 | char *ns;
454 | struct ksym *ksym;
455 | struct ns_filter_ctx *ctx = (struct ns_filter_ctx*) _ctx;
456 |
457 | if (strncmp(name, STRTAB_NS_PREFIX, strlen(STRTAB_NS_PREFIX)))
458 | return;
459 |
460 | name += strlen(STRTAB_NS_PREFIX);
461 | ns = (char *) ctx->ksymtab_strings;
462 | ns += (ctx->e_type == ET_EXEC) ? value - ctx->sh_addr : value;
463 |
464 | if (!strlen(ns))
465 | return;
466 |
467 | if (!(ksym = hash_find(ctx->ksymtab->hash, name)))
468 | return;
469 |
470 | safe_asprintf(&ksym->ns, "%s", ns);
471 |
472 | if (!(ksym = hash_find(ctx->ksymtab->hash, ns)))
473 | return;
474 | ksymtab_ksym_mark(ksym);
475 | }
476 |
477 | static void ksymtab_fill_ns(const char *ksymtab_strings, struct ksymtab *ksymtab,
478 | struct elf_data *elf)
479 | {
480 | struct ns_filter_ctx ctx = {
481 | .ksymtab = ksymtab,
482 | .ksymtab_strings = ksymtab_strings,
483 | .e_type = elf->ehdr->e_type,
484 | .sh_addr = ksymtab->addr,
485 | };
486 |
487 | elf_for_each_sym(elf, ns_filter, (void*) &ctx, elf_iter_local);
488 | }
489 |
490 | /*
491 | * An entry for address -> symbol mapping.
492 | * The key will be "value".
493 | * We can use name pointer directly from the elf,
494 | * it will be freed later.
495 | */
496 | struct map_entry {
497 | uint64_t value;
498 | const char *name;
499 | };
500 |
501 | static struct map_entry *map_entry_new(uint64_t value, const char *name)
502 | {
503 | struct map_entry *res;
504 |
505 | res = safe_zmalloc(sizeof(*res));
506 | res->value = value;
507 | res->name = name;
508 |
509 | return res;
510 | }
511 |
512 | struct weak_filter_ctx {
513 | struct ksymtab *ksymtab;
514 | struct ksymtab *weaks;
515 | struct hash *map;
516 | };
517 |
518 | /*
519 | * Does two things in one pass on the symbol table:
520 | * 1) makes address -> symbol map for GLOBAL symbols;
521 | * 2) collecs subset of EXPORTed symbol, which have WEAK binding.
522 | */
523 | static void weak_filter(const char *name, uint64_t value, int bind, void *_ctx)
524 | {
525 | struct weak_filter_ctx *ctx = _ctx;
526 | struct map_entry *m;
527 | struct ksym *ksym;
528 |
529 | if (bind == STB_GLOBAL) {
530 | m = map_entry_new(value, name);
531 | hash_add_bin(ctx->map,
532 | (const char *)&m->value, sizeof(m->value), m);
533 | return;
534 | }
535 |
536 | /* WEAK handling */
537 |
538 | ksym = ksymtab_find(ctx->ksymtab, name);
539 | if (ksym == NULL)
540 | /* skip non-exported aliases */
541 | return;
542 |
543 | ksymtab_add_sym(ctx->weaks, name, strlen(name), value);
544 | }
545 |
546 | struct weak_to_alias_ctx {
547 | struct ksymtab *aliases;
548 | struct hash *map;
549 | };
550 |
551 | static void weak_to_alias(struct ksym *ksym, void *_ctx)
552 | {
553 | struct weak_to_alias_ctx *ctx = _ctx;
554 | struct map_entry *m;
555 | uint64_t value = ksymtab_ksym_get_value(ksym);
556 | const char *name = ksymtab_ksym_get_name(ksym);
557 | struct ksym *alias;
558 |
559 | m = hash_find_bin(ctx->map, (const char *)&value, sizeof(value));
560 | if (m == NULL)
561 | /* there is no GLOBAL alias for the WEAK exported symbol */
562 | return;
563 |
564 | alias = ksymtab_add_sym(ctx->aliases, m->name, strlen(m->name), 0);
565 | ksymtab_ksym_set_link(alias, name);
566 | }
567 |
568 | static struct ksymtab *ksymtab_weaks_to_aliases(struct ksymtab *weaks,
569 | struct hash *map)
570 | {
571 | struct ksymtab *aliases;
572 | struct weak_to_alias_ctx ctx;
573 |
574 | aliases = ksymtab_new(KSYMTAB_SIZE);
575 | if (aliases == NULL)
576 | fail("Cannot create ksymtab\n");
577 |
578 | ctx.aliases = aliases;
579 | ctx.map = map;
580 |
581 | ksymtab_for_each(weaks, weak_to_alias, &ctx);
582 |
583 | return aliases;
584 | }
585 |
586 | /*
587 | * Generate weak aliases for the symbols, found in the list of exported.
588 | * It will work correctly for one alias only.
589 | */
590 | static struct ksymtab *ksymtab_find_aliases(struct ksymtab *ksymtab,
591 | struct elf_data *elf)
592 | {
593 | struct ksymtab *aliases;
594 | struct ksymtab *weaks;
595 | struct hash *map; /* address to name mapping */
596 | struct weak_filter_ctx ctx;
597 |
598 | weaks = ksymtab_new(KSYMTAB_SIZE);
599 | if (weaks == NULL)
600 | fail("Cannot create weaks symtab\n");
601 |
602 | map = hash_new(KSYMTAB_SIZE, free);
603 | if (map == NULL)
604 | fail("Cannot create address->symbol mapping hash\n");
605 |
606 | ctx.ksymtab = ksymtab;
607 | ctx.weaks = weaks;
608 | ctx.map = map;
609 | /*
610 | * If there's a weak symbol on the stablelist,
611 | * we need to find the proper global
612 | * symbol to generate the type for it.
613 | *
614 | * It is done in two steps below:
615 | * 1) create address -> global symbol mapping and
616 | * suitable weak symbol list;
617 | * 2) for all weak symbols find its alias with the mapping.
618 | */
619 | elf_for_each_global_sym(elf, weak_filter, &ctx);
620 | aliases = ksymtab_weaks_to_aliases(weaks, map);
621 |
622 | hash_free(map);
623 | ksymtab_free(weaks);
624 |
625 | return aliases;
626 | }
627 |
628 | int elf_get_endianness(struct elf_data *data, unsigned int *endianness)
629 | {
630 | if (data->ehdr->e_ident[EI_DATA] != ELFDATA2LSB &&
631 | data->ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
632 | printf("Unsupported ELF endianness (EI_DATA) found: %d.\n",
633 | data->ehdr->e_ident[EI_DATA]);
634 | return 1;
635 | }
636 |
637 | *endianness = data->ehdr->e_ident[EI_DATA];
638 | return 0;
639 | }
640 |
641 | static inline int elf_get_strtab(struct elf_data *data)
642 | {
643 | const char *strtab;
644 | size_t strtab_size;
645 |
646 | if (elf_get_section(data->elf, data->shstrndx, STRTAB, &strtab,
647 | &strtab_size) == -1) {
648 | return 1;
649 | }
650 |
651 | data->strtab = strtab;
652 | data->strtab_size = strtab_size;
653 |
654 | return 0;
655 | }
656 |
657 | /*
658 | * Build list of exported symbols, i.e. searching the symbol table for
659 | * the symbols whose prefix is string "__ksymtab_", and create table
660 | * of aliases -- list of global symbols, which have the same
661 | * addresses, as weak symbols.
662 | */
663 | int elf_get_exported(struct elf_data *data, struct ksymtab **ksymtab,
664 | struct ksymtab **aliases)
665 | {
666 | Elf64_Addr addr;
667 | const char *ksymtab_strings;
668 | size_t ksymtab_strings_sz;
669 |
670 | if (elf_get_strtab(data) > 0)
671 | return 1;
672 |
673 | addr = elf_get_section(data->elf, data->shstrndx, KSYMTAB_STRINGS,
674 | &ksymtab_strings, &ksymtab_strings_sz);
675 | if (addr == -1)
676 | return 1;
677 |
678 | *ksymtab = parse_ksymtab_symbols(data);
679 | (*ksymtab)->addr = addr;
680 | *aliases = ksymtab_find_aliases(*ksymtab, data);
681 | ksymtab_fill_ns(ksymtab_strings, *ksymtab, data);
682 |
683 | return 0;
684 | }
685 |
686 |
687 |
688 |
--------------------------------------------------------------------------------
/ksymtab.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright(C) 2016, Red Hat, Inc., Stanislav Kozina
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | #ifndef KSYMTAB_H_
19 | #define KSYMTAB_H_
20 |
21 | #include
22 |
23 | struct elf_data {
24 | Elf *elf;
25 | GElf_Ehdr *ehdr;
26 | size_t shstrndx;
27 | const char *strtab;
28 | size_t strtab_size;
29 | int fd;
30 | };
31 |
32 | struct ksymtab;
33 | struct ksym {
34 | uint64_t value;
35 | bool mark;
36 | char *link;
37 | struct ksymtab *ksymtab;
38 | char *ns;
39 | char key[];
40 | };
41 |
42 | static inline bool ksymtab_ksym_is_marked(struct ksym *ksym)
43 | {
44 | return ksym->mark;
45 | }
46 |
47 | static inline const char *ksymtab_ksym_get_name(struct ksym *ksym)
48 | {
49 | return ksym->key;
50 | }
51 |
52 | static inline uint64_t ksymtab_ksym_get_value(struct ksym *ksym)
53 | {
54 | return ksym->value;
55 | }
56 |
57 | static inline char *ksymtab_ksym_get_link(struct ksym *ksym)
58 | {
59 | return ksym->link;
60 | }
61 |
62 | static inline void ksymtab_ksym_set_link(struct ksym *ksym, const char *link)
63 | {
64 | if (ksym->link)
65 | free(ksym->link);
66 | ksym->link = safe_strdup_or_null(link);
67 | }
68 |
69 | extern void ksymtab_free(struct ksymtab *);
70 | extern struct elf_data *elf_open(const char *);
71 | extern int elf_get_exported(struct elf_data *, struct ksymtab **,
72 | struct ksymtab **);
73 | extern void elf_close(struct elf_data *);
74 | extern int elf_get_endianness(struct elf_data *, unsigned int *);
75 | extern struct ksym *ksymtab_find(struct ksymtab *, const char *);
76 | extern size_t ksymtab_len(struct ksymtab *);
77 | extern struct ksymtab *ksymtab_new(size_t);
78 | extern struct ksym *ksymtab_add_sym(struct ksymtab *,
79 | const char *, size_t, uint64_t);
80 | extern struct ksym *ksymtab_copy_sym(struct ksymtab *, struct ksym *);
81 | extern void ksymtab_for_each(struct ksymtab *,
82 | void (*f)(struct ksym *, void *),
83 | void *);
84 | extern size_t ksymtab_mark_count(struct ksymtab *);
85 | extern void ksymtab_ksym_mark(struct ksym *);
86 |
87 | #endif /* KSYMTAB_H_ */
88 |
--------------------------------------------------------------------------------
/list.c:
--------------------------------------------------------------------------------
1 | #include "list.h"
2 |
3 | #include
4 |
5 | #include "utils.h"
6 |
7 | struct list *list_new(void (*free)(void *))
8 | {
9 | struct list *list = safe_zmalloc(sizeof(*list));
10 |
11 | list_init(list, free);
12 |
13 | return list;
14 | }
15 |
16 | void list_init(struct list *list, void (*free)(void *))
17 | {
18 | list->free = free;
19 | list->first = NULL;
20 | list->last = NULL;
21 | list->len = 0;
22 | }
23 |
24 | void list_clear(struct list *list)
25 | {
26 | struct list_node *next;
27 |
28 | next = list->first;
29 | while (next != NULL) {
30 | struct list_node *curr;
31 |
32 | curr = next;
33 | next = next->next;
34 |
35 | if (list->free && curr->data)
36 | list->free(curr->data);
37 |
38 | free(curr);
39 | }
40 |
41 | list->first = NULL;
42 | list->last = NULL;
43 | list->len = 0;
44 | }
45 |
46 | void list_free(struct list *list)
47 | {
48 | list_clear(list);
49 | free(list);
50 | }
51 |
52 | struct list_node *list_add(struct list *list, void *data)
53 | {
54 | struct list_node *node = safe_zmalloc(sizeof(*node));
55 |
56 | node->data = data;
57 | node->next = NULL;
58 | node->prev = list->last;
59 | node->list = list;
60 |
61 | if (list_len(list) == 0)
62 | list->first = node;
63 | else
64 | list->last->next = node;
65 | list->last = node;
66 | list->len++;
67 |
68 | return node;
69 | }
70 |
71 | void list_del(struct list_node *node)
72 | {
73 | struct list *list;
74 | struct list_node *next_temp;
75 |
76 | list = node->list;
77 | if (list->first == node)
78 | list->first = node->next;
79 | if (list->last == node)
80 | list->last = node->prev;
81 |
82 | next_temp = node->next;
83 | if (node->next)
84 | node->next->prev = node->prev;
85 | if (node->prev)
86 | node->prev->next = next_temp;
87 |
88 | list->len--;
89 |
90 | free(node);
91 | }
92 |
93 | void list_concat(struct list *dst, struct list *src)
94 | {
95 | struct list_node *iter;
96 |
97 | if (list_len(src) == 0)
98 | return;
99 |
100 | LIST_FOR_EACH(src, iter)
101 | iter->list = dst;
102 |
103 | if (dst->len == 0) {
104 | dst->first = src->first;
105 | dst->last = src->last;
106 | dst->len = src->len;
107 | } else {
108 | dst->last->next = src->first;
109 | src->first->prev = dst->last;
110 | dst->last = src->last;
111 | dst->len += src->len;
112 | }
113 |
114 | src->first = NULL;
115 | src->last = NULL;
116 | src->len = 0;
117 | }
118 |
--------------------------------------------------------------------------------
/list.h:
--------------------------------------------------------------------------------
1 | #ifndef LIST_H_
2 | #define LIST_H_
3 |
4 | struct list_node {
5 | void *data;
6 | struct list_node *next;
7 | struct list_node *prev;
8 | struct list *list;
9 | };
10 |
11 | struct list {
12 | struct list_node *first;
13 | struct list_node *last;
14 | void (*free)(void *data);
15 | int len;
16 | };
17 |
18 | struct list *list_new(void (*free)(void *));
19 | void list_init(struct list *list, void (*free)(void *));
20 |
21 | /*
22 | * Frees every node contained by list.
23 | * Does not accept NULL value for list.
24 | */
25 | void list_clear(struct list *list);
26 |
27 | /*
28 | * Frees list and every node contained by list.
29 | * Does not accept NULL value for list.
30 | */
31 | void list_free(struct list *list);
32 |
33 | struct list_node *list_add(struct list *list, void *data);
34 |
35 | /*
36 | * Removes node from the parent list, without freeing it.
37 | */
38 | void list_del(struct list_node *node);
39 |
40 | /*
41 | * Appends nodes from src to dst, emptying src.
42 | */
43 | void list_concat(struct list *dst, struct list *src);
44 |
45 | #define LIST_FOR_EACH(list, iter) \
46 | for ((iter) = (list)->first; (iter) != NULL; (iter) = (iter)->next)
47 |
48 |
49 | static inline int list_len(struct list *list)
50 | {
51 | return list->len;
52 | }
53 |
54 | static inline void *list_node_data(struct list_node *node)
55 | {
56 | return node->data;
57 | }
58 |
59 | static inline struct list_node *list_node_add(struct list_node *node,
60 | void *data)
61 | {
62 | return list_add(node->list, data);
63 | }
64 |
65 | #endif /* LIST_H_ */
66 |
--------------------------------------------------------------------------------
/main.c:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright(C) 2016, Red Hat, Inc., Stanislav Kozina
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | /*
19 | * The aim of this program is to compare two builds of the Linux kernel and
20 | * list all the ABI differences in the given list of symbols between these two
21 | * builds.
22 | *
23 | * For both builds the kabi information first needs to be stored in a text
24 | * files, see generate(). The the two dumps can be compared, see compare().
25 | * The format of the kabi information loosely follow the syntax of the Go
26 | * programming language for its ease of parsing.
27 | */
28 |
29 | #include
30 | #include
31 | #include
32 |
33 | #include "generate.h"
34 | #include "compare.h"
35 | #include "show.h"
36 | #include "utils.h"
37 |
38 | static char *progname;
39 |
40 | void usage(void)
41 | {
42 | printf("Usage:\n"
43 | "\t %s generate [options] kernel_dir\n"
44 | "\t %s show [options] kabi_file...\n"
45 | "\t %s compare [options] kabi_dir kabi_dir...\n",
46 | progname, progname, progname);
47 | exit(1);
48 | }
49 |
50 | int main(int argc, char **argv)
51 | {
52 | int ret = 0;
53 |
54 | progname = argv[0];
55 |
56 | if (argc < 2)
57 | usage();
58 |
59 | argv++; argc--;
60 |
61 | global_string_keeper_init();
62 |
63 | if (strcmp(argv[0], "generate") == 0)
64 | generate(argc, argv);
65 | else if (strcmp(argv[0], "compare") == 0)
66 | ret = compare(argc, argv);
67 | else if (strcmp(argv[0], "show") == 0)
68 | ret = show(argc, argv);
69 | else
70 | usage();
71 |
72 | global_string_keeper_free();
73 |
74 | return ret;
75 | }
76 |
--------------------------------------------------------------------------------
/main.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright(C) 2016, Red Hat, Inc., Stanislav Kozina
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | #ifndef MAIN_H_
19 | #define MAIN_H_
20 |
21 | #define DEFAULT_OUTPUT_DIR "./output"
22 | #define MODULE_DIR "/usr/lib/modules"
23 | #define DEBUG_MODULE_DIR "/usr/lib/debug/lib/modules"
24 |
25 | /* Default size of buffer for symbols loading */
26 | #define DEFAULT_BUFSIZE 64
27 |
28 | #define TYPEDEF_FILE "typedef--"
29 | #define FUNC_FILE "func--"
30 | #define STRUCT_FILE "struct--"
31 | #define UNION_FILE "union--"
32 | #define ENUM_FILE "enum--"
33 | #define VAR_FILE "var--"
34 |
35 | /* Path used for built-in types */
36 | #define BUILTIN_PATH ""
37 | /* Path used for DW_AT_declaration, ie. those we don't have */
38 | #define DECLARATION_PATH ""
39 |
40 | #define RH_KABI_HIDE "__UNIQUE_ID_rh_kabi_hide"
41 | #define RH_KABI_HIDE_LEN 24
42 |
43 | #endif /* MAIN_H_ */
44 |
--------------------------------------------------------------------------------
/objects.c:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright(C) 2016, Red Hat, Inc., Jerome Marchand
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | /*
19 | * Internal representation and manipulation of symbols
20 | */
21 |
22 | #ifndef _GNU_SOURCE /* We use GNU basename() that doesn't modify the arg */
23 | #error "We need GNU version of basename()!"
24 | #endif
25 |
26 | #include
27 | #include
28 | #include
29 | #include
30 | #include
31 | #include
32 | #include
33 | #include
34 | #include
35 | #include
36 | #include
37 |
38 | #include "objects.h"
39 | #include "utils.h"
40 | #include "main.h"
41 | #include "record.h"
42 |
43 | /* Indentation offset for c-style and tree debug outputs */
44 | #define C_INDENT_OFFSET 8
45 | #define DBG_INDENT_OFFSET 4
46 |
47 | obj_list_t *obj_list_new(obj_t *obj)
48 | {
49 | obj_list_t *list = safe_zmalloc(sizeof(obj_list_t));
50 | list->member = obj;
51 | list->next = NULL;
52 | return list;
53 | }
54 |
55 | static void obj_list_init(obj_list_head_t *head, obj_t *obj)
56 | {
57 | obj_list_t *list = obj_list_new(obj);
58 | head->first = head->last = list;
59 | }
60 |
61 | obj_list_head_t *obj_list_head_new(obj_t *obj)
62 | {
63 | obj_list_head_t *h = safe_zmalloc(sizeof(obj_list_head_t));
64 |
65 | obj_list_init(h, obj);
66 |
67 | return h;
68 | }
69 |
70 | static bool obj_list_empty(obj_list_head_t *head)
71 | {
72 | return head->first == NULL;
73 | }
74 |
75 | void obj_list_add(obj_list_head_t *head, obj_t *obj)
76 | {
77 | obj_list_t *list;
78 |
79 | if (obj_list_empty(head)) {
80 | obj_list_init(head, obj);
81 | return;
82 | }
83 | list = obj_list_new(obj);
84 |
85 | if (head->last->next)
86 | fprintf(stderr, "head->last is not the last\n");
87 |
88 | head->last->next = list;
89 | head->last = list;
90 | }
91 |
92 | obj_t *obj_new(obj_types type, char *name)
93 | {
94 | obj_t *new = safe_zmalloc(sizeof(obj_t));
95 |
96 | new->type = type;
97 | new->name = global_string_get_move(name);
98 | new->ns = NULL;
99 |
100 | return new;
101 | }
102 |
103 | static void _obj_free(obj_t *o, obj_t *skip);
104 |
105 | static void _obj_list_free(obj_list_head_t *l, obj_t *skip)
106 | {
107 | obj_list_t *list;
108 | obj_list_t *next;
109 |
110 | if (l == NULL)
111 | return;
112 |
113 | list = l->first;
114 | free(l);
115 |
116 | while (list) {
117 | _obj_free(list->member, skip);
118 | next = list->next;
119 | free(list);
120 | list = next;
121 | }
122 | }
123 |
124 | static void obj_list_free(obj_list_head_t *l)
125 | {
126 | _obj_list_free(l, NULL);
127 | }
128 |
129 | /*
130 | * Free the tree o, but keep the subtree skip.
131 | */
132 | static void _obj_free(obj_t *o, obj_t *skip)
133 | {
134 | if (!o || (o == skip))
135 | return;
136 |
137 | if (o->type == __type_reffile && o->depend_rec_node) {
138 | list_del(o->depend_rec_node);
139 | o->depend_rec_node = NULL;
140 | }
141 |
142 | _obj_list_free(o->member_list, skip);
143 |
144 | if (o->ptr)
145 | _obj_free(o->ptr, skip);
146 |
147 | if (is_weak(o))
148 | free(o->link);
149 |
150 | free(o);
151 | }
152 |
153 | /*
154 | * Free the all object
155 | */
156 | void obj_free(obj_t *o)
157 | {
158 | _obj_free(o, NULL);
159 | }
160 |
161 | #define _CREATE_NEW_FUNC(type, suffix) \
162 | obj_t *obj_##type##_##suffix(char *name) \
163 | { \
164 | obj_t *new = obj_new(__type_##type, name); \
165 | return new; \
166 | }
167 | #define CREATE_NEW_FUNC(type) _CREATE_NEW_FUNC(type, new)
168 | #define CREATE_NEW_FUNC_NONAME(type) \
169 | _CREATE_NEW_FUNC(type, new_) \
170 | obj_t *obj_##type##_new() \
171 | { \
172 | return obj_##type##_new_(NULL); \
173 | }
174 |
175 | #define _CREATE_NEW_ADD_FUNC(type, infix) \
176 | obj_t *obj_##type##_##infix##_##add(char *name, obj_t *obj) \
177 | { \
178 | obj_t *new = obj_new(__type_##type, name); \
179 | new->ptr = obj; \
180 | return new; \
181 | }
182 | #define CREATE_NEW_ADD_FUNC(type) _CREATE_NEW_ADD_FUNC(type, new)
183 | #define CREATE_NEW_ADD_FUNC_NONAME(type) \
184 | _CREATE_NEW_ADD_FUNC(type, new_) \
185 | obj_t *obj_##type##_new_add(obj_t *obj) \
186 | { \
187 | return obj_##type##_new__add(NULL, obj); \
188 | }
189 |
190 | CREATE_NEW_FUNC(struct)
191 | CREATE_NEW_FUNC(union)
192 | CREATE_NEW_FUNC(enum)
193 | CREATE_NEW_FUNC(constant)
194 | CREATE_NEW_FUNC_NONAME(reffile)
195 | CREATE_NEW_ADD_FUNC(func)
196 | CREATE_NEW_ADD_FUNC(typedef)
197 | CREATE_NEW_ADD_FUNC(var)
198 | CREATE_NEW_ADD_FUNC(struct_member)
199 | CREATE_NEW_ADD_FUNC_NONAME(ptr)
200 | CREATE_NEW_ADD_FUNC_NONAME(array)
201 | CREATE_NEW_ADD_FUNC_NONAME(qualifier)
202 | CREATE_NEW_FUNC(assembly)
203 | CREATE_NEW_FUNC(weak)
204 |
205 | obj_t *obj_basetype_new(char *base_type)
206 | {
207 | obj_t *new = obj_new(__type_base, NULL);
208 |
209 | new->base_type = global_string_get_move(base_type);
210 |
211 | return new;
212 | }
213 |
214 | const char *obj_type_name[NR_OBJ_TYPES+1] = {
215 | "reference file",
216 | "struct",
217 | "union",
218 | "enum",
219 | "func",
220 | "ptr",
221 | "typedef",
222 | "array",
223 | "var",
224 | "struct member",
225 | "type qualifier",
226 | "base",
227 | "constant",
228 | "assembly",
229 | "weak",
230 | "unknown type"
231 | };
232 |
233 | static const char *typetostr(obj_t *o)
234 | {
235 | int t = o->type;
236 | if (t >= NR_OBJ_TYPES)
237 | t = NR_OBJ_TYPES;
238 | return obj_type_name[t];
239 | }
240 |
241 | static int c_precedence(obj_t *o)
242 | {
243 | switch (o->type) {
244 | case __type_func:
245 | case __type_array:
246 | return 1;
247 | case __type_ptr:
248 | return 2;
249 | default:
250 | return INT_MAX;
251 | }
252 | }
253 |
254 | /*
255 | * Returns whether parentheses are needed
256 | *
257 | * Pointer have a higher precedence than function and array, so we need to put
258 | * parentheses around a pointer to a function of array.
259 | */
260 | static bool is_paren_needed(obj_t *node)
261 | {
262 | obj_t *child = node->ptr;
263 |
264 | while (child) {
265 | if (c_precedence(child) < c_precedence(node))
266 | return true;
267 |
268 | child = child->ptr;
269 | }
270 | return false;
271 | }
272 |
273 | static char *print_margin_offset(const char *prefix, const char *s, int depth)
274 | {
275 | size_t len = snprintf(NULL, 0, "%-*s", depth * C_INDENT_OFFSET, s) + 1;
276 | char *ret;
277 |
278 | if (prefix)
279 | len += strlen(prefix);
280 |
281 | if (!len)
282 | return NULL;
283 | ret = safe_zmalloc(len);
284 |
285 | snprintf(ret, len, "%s%-*s",
286 | prefix ? prefix : "", depth * C_INDENT_OFFSET, s);
287 |
288 | return ret;
289 | }
290 |
291 | static char *print_margin(const char *prefix, int depth)
292 | {
293 | return print_margin_offset(prefix, "", depth);
294 | }
295 |
296 | /*
297 | * Return type for print_* functions
298 | *
299 | * Because C mixes prefix and postfix operator, the code generation from a node
300 | * may need to add code before, after or in the middle of the code generated by
301 | * subtrees. Thus we sometimes need two return two strings.
302 | *
303 | * Attention to the precedence and associativity sould be taken when
304 | * deciding where a specific string should be inserted
305 | */
306 | typedef struct {
307 | char *prefix;
308 | char *postfix;
309 | } pp_t;
310 |
311 | static void free_pp(pp_t pp)
312 | {
313 | free(pp.prefix);
314 | free(pp.postfix);
315 | }
316 |
317 | static pp_t _print_tree(obj_t *o, int depth, bool newline, const char *prefix);
318 |
319 | /*
320 | * Add prefix p at the begining of string s (reallocated)
321 | *
322 | * space: add a space between p and s
323 | * freep: free the string p
324 | */
325 | static char *_prefix_str(char **s, char *p, bool space, bool freep)
326 | {
327 | size_t lenp = strlen(p), lens = 0, newlen;
328 |
329 | if (*s)
330 | lens = strlen(*s);
331 | newlen = lens + lenp + 1;
332 |
333 | if (space)
334 | newlen++;
335 |
336 | *s = safe_realloc(*s, newlen);
337 |
338 | if (lens)
339 | memmove(space ? *s+lenp+1 : *s+lenp, *s, lens + 1);
340 | else
341 | (*s)[lenp] = '\0';
342 | memcpy(*s, p, lenp);
343 | if (space)
344 | (*s)[lenp] = ' ';
345 |
346 | if (freep)
347 | free(p);
348 |
349 | return *s;
350 | }
351 |
352 | static char *prefix_str(char **s, char *p)
353 | {
354 | if (!p)
355 | return *s;
356 | return _prefix_str(s, p, false, false);
357 | }
358 |
359 | static char *prefix_str_free(char **s, char *p)
360 | {
361 | if (!p)
362 | return *s;
363 | return _prefix_str(s, p, false, true);
364 | }
365 |
366 | static char *prefix_str_space(char **s, const char *p)
367 | {
368 | if (!p)
369 | return *s;
370 | /* freep is false so we can pass const char * */
371 | return _prefix_str(s, (char *)p, true, false);
372 | }
373 |
374 | /*
375 | * Add suffix p at the end of string s (realocated)
376 | *
377 | * space: add a space between p and s
378 | * freep: free the string p
379 | */
380 | static char *_postfix_str(char **s, char *p, bool space, bool freep)
381 | {
382 | int lenp = strlen(p), lens = 0, newlen;
383 | if (*s)
384 | lens = strlen(*s);
385 | newlen = lens + lenp + 1;
386 |
387 | if (space)
388 | newlen++;
389 |
390 | *s = safe_realloc(*s, newlen);
391 |
392 | if (lens == 0)
393 | (*s)[0] = '\0';
394 | if (space)
395 | strcat(*s, " ");
396 | strcat(*s, p);
397 |
398 | if (freep)
399 | free(p);
400 |
401 | return *s;
402 | }
403 |
404 | static char *postfix_str(char **s, const char *p)
405 | {
406 | if (!p)
407 | return *s;
408 |
409 | /* freep is false so we can pass const char * */
410 | return _postfix_str(s, (char *)p, false, false);
411 | }
412 |
413 | static char *postfix_str_free(char **s, char *p)
414 | {
415 | if (!p)
416 | return *s;
417 | return _postfix_str(s, p, false, true);
418 | }
419 |
420 | static pp_t print_base(obj_t *o, int depth, const char *prefix)
421 | {
422 | pp_t ret = {NULL, NULL};
423 |
424 | safe_asprintf(&ret.prefix, "%s ", o->base_type);
425 |
426 | return ret;
427 | }
428 |
429 | static pp_t print_constant(obj_t *o, int depth, const char *prefix)
430 | {
431 | pp_t ret = {NULL, NULL};
432 |
433 | safe_asprintf(&ret.prefix, "%s = %li", o->name, (long)o->constant);
434 |
435 | return ret;
436 | }
437 |
438 | static pp_t print_reffile(obj_t *o, int depth, const char *prefix)
439 | {
440 | pp_t ret = {NULL, NULL};
441 | char *s = filenametotype(o->base_type);
442 |
443 | s = safe_realloc(s, strlen(s) + 2);
444 | strcat(s, " ");
445 | ret.prefix = s;
446 |
447 | return ret;
448 | }
449 |
450 | /* Print a struct, enum or an union */
451 | static pp_t print_structlike(obj_t *o, int depth, const char *prefix)
452 | {
453 | pp_t ret = {NULL, NULL}, tmp;
454 | obj_list_t *list = NULL;
455 | char *s, *margin;
456 |
457 | if (o->name)
458 | safe_asprintf(&s, "%s %s {\n", typetostr(o), o->name);
459 | else
460 | safe_asprintf(&s, "%s {\n", typetostr(o));
461 |
462 | if (o->member_list)
463 | list = o->member_list->first;
464 | while (list) {
465 | tmp = _print_tree(list->member, depth+1, true, prefix);
466 | postfix_str_free(&s, tmp.prefix);
467 | postfix_str_free(&s, tmp.postfix);
468 | postfix_str(&s, o->type == __type_enum ? ",\n" : ";\n");
469 | list = list->next;
470 | }
471 |
472 | margin = print_margin(prefix, depth);
473 | postfix_str_free(&s, margin);
474 | postfix_str(&s, "}");
475 |
476 | ret.prefix = s;
477 | return ret;
478 | }
479 |
480 | static pp_t print_func(obj_t *o, int depth, const char *prefix)
481 | {
482 | pp_t ret = {NULL, NULL}, return_type;
483 | obj_list_t *list = NULL;
484 | obj_t *next = o->ptr;
485 | char *s, *margin;
486 | const char *name;
487 |
488 | return_type = _print_tree(next, depth, false, prefix);
489 | ret.prefix = return_type.prefix;
490 |
491 | if (o->name)
492 | name = o->name;
493 | else
494 | name = "";
495 |
496 | safe_asprintf(&s, "%s(\n", name);
497 |
498 | if (o->member_list)
499 | list = o->member_list->first;
500 | while (list) {
501 | pp_t arg = _print_tree(list->member, depth+1, true, prefix);
502 | postfix_str_free(&s, arg.prefix);
503 | postfix_str_free(&s, arg.postfix);
504 | list = list->next;
505 | postfix_str(&s, list ? ",\n" : "\n");
506 | }
507 |
508 | margin = print_margin(prefix, depth);
509 | postfix_str_free(&s, margin);
510 | postfix_str(&s, ")");
511 |
512 | ret.postfix = s;
513 | return ret;
514 | }
515 |
516 | static pp_t print_array(obj_t *o, int depth, const char *prefix)
517 | {
518 | pp_t ret;
519 | char *s;
520 | obj_t *next = o->ptr;
521 |
522 | ret = _print_tree(next, depth, false, prefix);
523 |
524 | safe_asprintf(&s, "[%lu]", o->constant);
525 | prefix_str_free(&ret.postfix, s);
526 |
527 | return ret;
528 | }
529 |
530 | static pp_t print_ptr(obj_t *o, int depth, const char *prefix)
531 | {
532 | pp_t ret;
533 | bool need_paren = is_paren_needed(o);
534 | obj_t *next = o->ptr;
535 |
536 | ret = _print_tree(next, depth, false, prefix);
537 | if (need_paren) {
538 | postfix_str(&ret.prefix, "(*");
539 | prefix_str(&ret.postfix, ")");
540 | } else
541 | postfix_str(&ret.prefix, "*");
542 |
543 | return ret;
544 | }
545 |
546 | /* Print a var or a struct_member */
547 | static pp_t print_varlike(obj_t *o, int depth, const char *prefix)
548 | {
549 | pp_t ret;
550 | char *s = NULL;
551 |
552 | if (is_bitfield(o))
553 | safe_asprintf(&s, "%s:%i",
554 | o->name, o->last_bit - o->first_bit + 1);
555 | else
556 | s = (char *)o->name;
557 |
558 | ret = _print_tree(o->ptr, depth, false, prefix);
559 |
560 | if (s)
561 | postfix_str(&ret.prefix, s);
562 |
563 | if (is_bitfield(o))
564 | free(s);
565 |
566 | return ret;
567 | }
568 |
569 | static pp_t print_typedef(obj_t *o, int depth, const char *prefix)
570 | {
571 | pp_t ret;
572 |
573 | ret = _print_tree(o->ptr, depth, false, prefix);
574 |
575 | prefix_str(&ret.prefix, "typedef ");
576 | postfix_str(&ret.prefix, o->name);
577 |
578 | return ret;
579 | }
580 |
581 | static pp_t print_qualifier(obj_t *o, int depth, const char *prefix)
582 | {
583 | pp_t ret;
584 |
585 | ret = _print_tree(o->ptr, depth, false, prefix);
586 | prefix_str_space(&ret.prefix, o->base_type);
587 |
588 | return ret;
589 | }
590 |
591 | static pp_t print_assembly(obj_t *o, int depth, const char *prefix)
592 | {
593 | pp_t ret = {NULL, NULL};
594 |
595 | prefix_str(&ret.prefix, "assembly ");
596 | postfix_str(&ret.prefix, o->name);
597 |
598 | return ret;
599 | }
600 |
601 | static pp_t print_weak(obj_t *o, int depth, const char *prefix)
602 | {
603 | pp_t ret = {NULL, NULL};
604 |
605 | prefix_str(&ret.prefix, "weak ");
606 | postfix_str(&ret.prefix, o->name);
607 | postfix_str(&ret.prefix, " -> ");
608 | postfix_str(&ret.prefix, o->link);
609 |
610 | return ret;
611 | }
612 |
613 | #define BASIC_CASE(type) \
614 | case __type_##type: \
615 | ret = print_##type(o, depth, prefix); \
616 | break;
617 |
618 | struct dopt display_options;
619 |
620 | /*
621 | * Display an object in a c-like format
622 | *
623 | * o: object to be displayed
624 | * depth: current indentation depth
625 | * newline: is this the begining of a new line?
626 | * prefix: prefix to be printed at the begining of each line
627 | */
628 | static pp_t _print_tree(obj_t *o, int depth, bool newline, const char *prefix)
629 | {
630 | pp_t ret = {NULL, NULL};
631 | char *margin;
632 |
633 | /* silence coverity on write-only variable */
634 | (void)ret;
635 |
636 | if (!o)
637 | fail("NULL pointer in _print_tree\n");
638 | debug("_print_tree(): %s\n", typetostr(o));
639 |
640 | switch (o->type) {
641 | BASIC_CASE(reffile);
642 | BASIC_CASE(constant);
643 | BASIC_CASE(base);
644 | BASIC_CASE(typedef);
645 | BASIC_CASE(qualifier);
646 | BASIC_CASE(func);
647 | BASIC_CASE(array);
648 | BASIC_CASE(ptr);
649 | BASIC_CASE(assembly);
650 | BASIC_CASE(weak);
651 | case __type_var:
652 | case __type_struct_member:
653 | ret = print_varlike(o, depth, prefix);
654 | break;
655 | case __type_struct:
656 | case __type_union:
657 | case __type_enum:
658 | ret = print_structlike(o, depth, prefix);
659 | break;
660 | default:
661 | fail("WIP: doesn't handle %s\n", typetostr(o));
662 | }
663 |
664 | if (!newline)
665 | return ret;
666 |
667 | if (o->type == __type_struct_member && !display_options.no_offset) {
668 | char *offstr;
669 | if (is_bitfield(o))
670 | safe_asprintf(&offstr, "0x%lx:%2i-%-2i ",
671 | o->offset, o->first_bit, o->last_bit);
672 | else
673 | safe_asprintf(&offstr, "0x%lx ", o->offset);
674 | margin = print_margin_offset(prefix, offstr, depth);
675 | free(offstr);
676 | } else
677 | margin = print_margin(prefix, depth);
678 |
679 | prefix_str_free(&ret.prefix, margin);
680 | return ret;
681 | }
682 |
683 | void obj_print_tree__prefix(obj_t *root, const char *prefix, FILE *stream)
684 | {
685 | pp_t s = _print_tree(root, 0, true, prefix);
686 |
687 | fprintf(stream, "%s%s;\n",
688 | s.prefix ? s.prefix : "",
689 | s.postfix ? s.postfix : "");
690 | free_pp(s);
691 | }
692 |
693 | void obj_print_tree(obj_t *root)
694 | {
695 | obj_print_tree__prefix(root, NULL, stdout);
696 | }
697 |
698 | static void fill_parent_rec(obj_t *o, obj_t *parent)
699 | {
700 | obj_list_t *list = NULL;
701 |
702 | o->parent = parent;
703 |
704 | if (o->member_list)
705 | list = o->member_list->first;
706 |
707 | while (list) {
708 | fill_parent_rec(list->member, o);
709 | list = list->next;
710 | }
711 |
712 | if (o->ptr)
713 | fill_parent_rec(o->ptr, o);
714 | }
715 |
716 | /*
717 | * Walk the tree and fill all the parents field
718 | */
719 | void obj_fill_parent(obj_t *root)
720 | {
721 | fill_parent_rec(root, NULL);
722 | }
723 |
724 | static int walk_list(obj_list_t *list, cb_t cb_pre, cb_t cb_in, cb_t cb_post,
725 | void *args, bool ptr_first)
726 | {
727 | int ret = CB_CONT;
728 |
729 | while (list) {
730 | ret = obj_walk_tree3(list->member, cb_pre, cb_in, cb_post,
731 | args, ptr_first);
732 | if (ret == CB_FAIL)
733 | return ret;
734 | else
735 | ret = CB_CONT;
736 | list = list->next;
737 | }
738 |
739 | return ret;
740 | }
741 |
742 | static int walk_ptr(obj_t *o, cb_t cb_pre, cb_t cb_in, cb_t cb_post,
743 | void *args, bool ptr_first)
744 | {
745 | int ret = CB_CONT;
746 |
747 | if (o->ptr) {
748 | ret = obj_walk_tree3(o->ptr, cb_pre, cb_in, cb_post,
749 | args, ptr_first);
750 | if (ret == CB_FAIL)
751 | return ret;
752 | else
753 | ret = CB_CONT;
754 | }
755 |
756 | return ret;
757 | }
758 |
759 | /*
760 | * Tree walk with prefix, infix and postfix callbacks
761 | *
762 | * o: walked tree
763 | * cb_pre: callback function called before walking the subtrees
764 | * cb_in: callback function called between walking the subtrees
765 | * cp_post: callback function called between walking the subtrees
766 | * args: argument passed to the callbacks
767 | * ptr_first: whether we walk member_list of ptr first
768 | */
769 | int obj_walk_tree3(obj_t *o, cb_t cb_pre, cb_t cb_in, cb_t cb_post,
770 | void *args, bool ptr_first)
771 | {
772 | obj_list_t *list = NULL;
773 | int ret = CB_CONT;
774 |
775 | if (cb_pre) {
776 | ret = cb_pre(o, args);
777 | if (ret)
778 | return ret;
779 | }
780 |
781 | if (o->member_list)
782 | list = o->member_list->first;
783 |
784 |
785 | if (ptr_first)
786 | ret = walk_ptr(o, cb_pre, cb_in, cb_post, args, ptr_first);
787 | else
788 | ret = walk_list(list, cb_pre, cb_in, cb_post, args, ptr_first);
789 | if (ret == CB_FAIL)
790 | return ret;
791 |
792 | if (cb_in) {
793 | ret = cb_in(o, args);
794 | if (ret)
795 | return ret;
796 | }
797 |
798 | if (ptr_first)
799 | ret = walk_list(list, cb_pre, cb_in, cb_post, args, ptr_first);
800 | else
801 | ret = walk_ptr(o, cb_pre, cb_in, cb_post, args, ptr_first);
802 | if (ret == CB_FAIL)
803 | return ret;
804 |
805 | if (cb_post) {
806 | ret = cb_post(o, args);
807 | if (ret)
808 | return ret;
809 | }
810 |
811 | return ret;
812 | }
813 |
814 | /*
815 | * Simple tree walk with a prefix callback
816 | *
817 | * It walks the member_list subtree first.
818 | */
819 | int obj_walk_tree(obj_t *root, cb_t cb, void *args)
820 | {
821 | return obj_walk_tree3(root, cb, NULL, NULL, args, false);
822 | }
823 |
824 | static void _show_node(FILE *f, obj_t *o, int margin)
825 | {
826 | if (o)
827 | fprintf(f,
828 | "\%*s<%s, \"%s\", \"%s\", %p, %p, %p, %lu, %i, %i>\n",
829 | margin, "", typetostr(o), o->name, o->base_type,
830 | o, o->parent, o->ptr,
831 | o->offset, o->first_bit, o->last_bit);
832 | else
833 | fprintf(f, "\%*s<(nil)>\n", margin, "");
834 | }
835 |
836 | static void show_node(obj_t *o, int margin)
837 | {
838 | _show_node(stdout, o, margin);
839 | }
840 |
841 | static int debug_node(obj_t *node, void *args)
842 | {
843 | int *depth = (int *) args;
844 |
845 | show_node(node, *depth * DBG_INDENT_OFFSET);
846 | (*depth)++;
847 |
848 | return CB_CONT;
849 | }
850 |
851 | static int dec_depth(obj_t *node, void *args)
852 | {
853 | int *depth = (int *) args;
854 |
855 | (*depth)--;
856 |
857 | return CB_CONT;
858 | }
859 |
860 | /*
861 | * Print a raw representation of the internal object tree
862 | */
863 | int obj_debug_tree(obj_t *root)
864 | {
865 | int depth = 0;
866 |
867 | return obj_walk_tree3(root, debug_node, NULL, dec_depth, &depth, false);
868 | }
869 |
870 | /*
871 | * Hide some RH_KABI magic
872 | *
873 | * WARNING: this code is ugly and full of add-hoc hacks, but I'm
874 | * afraid it can't be fixed. It has to follow the internals of
875 | * RH_KABI_* macros. Also, it may have to change if RH_KABI_*
876 | * functions change in the future.
877 | *
878 | * RH_KABI_REPLACE the old field by a complex construction of union
879 | * and struct used to check that the new field didn't change the
880 | * alignement. It is of the form:
881 | * union {
882 | * _new;
883 | * struct {
884 | * _orig;
885 | * } __UNIQUE_ID_rh_kabi_hideXX
886 | * union {};
887 | * }
888 | *
889 | * RH_KABI_USE2(_P) replace a single field by two field that fits in
890 | * the same space. It puts the two new field into an unnamed
891 | * struct. We don't hide that as we have no way to know if that struct
892 | * is an artifact from RH_KABI_USE2 or was added deliberately.
893 | *
894 | * RH_KABI_DEPRECATE(_FN) prefix the field name with
895 | * rh_reserved_. This is not the most specific string. It currently
896 | * appears in a few places that deprecates the field by hand, in which
897 | * it's OK to hide it too, but for some reason in
898 | * block_device_operations the reserved fields are of the form "void
899 | * *rh_reserved_ptrsX" instead of the usual "unsigned long
900 | * rh_reservedX". Treat this case as an exception.
901 | *
902 | * Most RH_KABI_* functions, don't add any recognizable code so we
903 | * can't hide them here.
904 | */
905 | static int hide_kabi_cb(obj_t *o, void *args)
906 | {
907 | obj_t *kabi_struct, *new, *old, *parent = o->parent, *keeper;
908 | obj_list_head_t *lh;
909 | obj_list_t *l;
910 | bool show_new_field = (bool) args;
911 |
912 | if (o->name) {
913 | if (!strncmp(o->name, RH_KABI_HIDE, RH_KABI_HIDE_LEN))
914 | fail("Missed a kabi unique ID\n");
915 |
916 | /* Hide RH_KABI_DEPRECATE* */
917 | if (!strncmp(o->name, "rh_reserved_", 12) &&
918 | strncmp(o->name, "rh_reserved_ptrs", 16)) {
919 | char *tmp = strdup(o->name+12);
920 | o->name = global_string_get_move(tmp);
921 | }
922 | }
923 |
924 | /* Hide RH_KABI_REPLACE */
925 | if ((o->type != __type_union) || o->name ||
926 | !(lh = o->member_list) || obj_list_empty(lh) ||
927 | !(l = lh->first) || !(new = l->member) ||
928 | !(l = l->next) || !(kabi_struct = l->member) ||
929 | (kabi_struct->type != __type_var) ||
930 | !kabi_struct->name ||
931 | strncmp(kabi_struct->name, RH_KABI_HIDE, RH_KABI_HIDE_LEN))
932 | return CB_CONT;
933 |
934 | if (!kabi_struct->ptr || kabi_struct->ptr->type != __type_struct ||
935 | !(lh = kabi_struct->ptr->member_list) || obj_list_empty(lh) ||
936 | !(l = lh->first) || !(old = l->member))
937 | fail("Unexpeted rh_kabi_hide struct format\n");
938 |
939 | /*
940 | * It is a rh_kabi_hide union
941 | * old is the first member of kabi_struct
942 | *
943 | * Need to replace that:
944 | * (parent)
945 | * (o)
946 | * (new)
947 | *
948 | *
949 | *
950 | *
951 | * ^(old)
952 | *
953 | *
954 | *
955 | * by that:
956 | *
957 | *
958 | *
959 | * or that:
960 | *
961 | *
962 | *
963 | * Parent is always an unary node, struct_member or var
964 | */
965 |
966 | if (!parent ||
967 | !((parent->type == __type_var) ||
968 | (parent->type == __type_struct_member)) ||
969 | (parent->ptr != o) || parent->name) {
970 | _show_node(stderr, parent, 0);
971 | fail("Unexpected parent\n");
972 | }
973 | if (new->type != __type_var) {
974 | _show_node(stderr, new, 0);
975 | fail("Unexpected new field\n");
976 | }
977 | if (old->type != __type_struct_member) {
978 | _show_node(stderr, old, 0);
979 | fail("Unexpected old field\n");
980 | }
981 |
982 | keeper = show_new_field ? new : old;
983 |
984 | parent->name = keeper->name;
985 | parent->ptr = keeper->ptr;
986 | parent->ptr->parent = parent;
987 | _obj_free(o, keeper);
988 | free(keeper);
989 |
990 | return CB_SKIP;
991 | }
992 |
993 | int obj_hide_kabi(obj_t *root, bool show_new_field)
994 | {
995 | return obj_walk_tree(root, hide_kabi_cb, (void *)show_new_field);
996 | }
997 |
998 | static bool obj_is_declaration(obj_t *obj)
999 | {
1000 | if (obj->type != __type_reffile || obj->ref_record == NULL)
1001 | return false;
1002 |
1003 | return record_is_declaration(obj->ref_record);
1004 | }
1005 |
1006 | static bool obj_is_kabi_hide(obj_t *obj)
1007 | {
1008 | if (obj->name == NULL)
1009 | return false;
1010 |
1011 | return strncmp(obj->name, RH_KABI_HIDE, RH_KABI_HIDE_LEN) == 0;
1012 | }
1013 |
1014 | bool obj_eq(obj_t *o1, obj_t *o2, bool ignore_versions)
1015 | {
1016 | if (o1->type != o2->type)
1017 | return false;
1018 |
1019 | if (o1->type == __type_reffile) {
1020 | if (ignore_versions) {
1021 | return record_get_key(o1->ref_record) ==
1022 | record_get_key(o2->ref_record);
1023 | }
1024 |
1025 | return o1->ref_record == o2->ref_record;
1026 | }
1027 |
1028 | /* borrow parts from cmp_nodes */
1029 | if ((o1->name != o2->name) ||
1030 | ((o1->ptr == NULL) != (o2->ptr == NULL)) ||
1031 | (has_constant(o1) && (o1->constant != o2->constant)) ||
1032 | (has_index(o1) && (o1->index != o2->index)) ||
1033 | (is_bitfield(o1) != is_bitfield(o2)) ||
1034 | (o1->alignment != o2->alignment) ||
1035 | !safe_streq(o1->ns, o2->ns) ||
1036 | (o1->byte_size != o2->byte_size))
1037 | return false;
1038 |
1039 | /* just compare bitfields */
1040 | if (is_bitfield(o1) &&
1041 | ((o1->last_bit != o2->last_bit) ||
1042 | (o1->first_bit != o2->first_bit)))
1043 | return false;
1044 |
1045 | if ((o1->member_list == NULL) !=
1046 | (o2->member_list == NULL))
1047 | return false;
1048 |
1049 | if (o1->base_type != o2->base_type)
1050 | return false;
1051 |
1052 | return true;
1053 | }
1054 |
1055 | static obj_t *obj_copy(obj_t *o1)
1056 | {
1057 | obj_t *o;
1058 |
1059 | o = safe_zmalloc(sizeof(*o));
1060 | *o = *o1;
1061 |
1062 | o->ptr = NULL;
1063 | o->member_list = NULL;
1064 |
1065 | if (o1->type == __type_reffile && o1->depend_rec_node)
1066 | o->depend_rec_node = list_node_add(o1->depend_rec_node, o);
1067 |
1068 | return o;
1069 | }
1070 |
1071 | obj_t *obj_merge(obj_t *o1, obj_t *o2, unsigned int flags);
1072 |
1073 | static obj_list_head_t *obj_members_merge(obj_list_head_t *list1,
1074 | obj_list_head_t *list2,
1075 | unsigned int flags)
1076 | {
1077 | obj_list_head_t *res = NULL;
1078 | obj_list_t *l1;
1079 | obj_list_t *l2;
1080 | obj_t *o;
1081 |
1082 | if (list1 == NULL || list2 == NULL)
1083 | return NULL;
1084 |
1085 | l1 = list1->first;
1086 | l2 = list2->first;
1087 |
1088 | while (l1 && l2) {
1089 | o = obj_merge(l1->member, l2->member, flags);
1090 | if (o == NULL)
1091 | goto cleanup;
1092 |
1093 | if (res == NULL)
1094 | res = obj_list_head_new(o);
1095 | else
1096 | obj_list_add(res, o);
1097 |
1098 | l1 = l1->next;
1099 | l2 = l2->next;
1100 | };
1101 |
1102 | if (l1 || l2)
1103 | goto cleanup;
1104 |
1105 | return res;
1106 |
1107 | cleanup:
1108 | obj_list_free(res);
1109 | return NULL;
1110 | }
1111 |
1112 | static inline bool obj_can_merge_two_lines(obj_t *o1, obj_t *o2,
1113 | unsigned int flags)
1114 | {
1115 | /*
1116 | * We cannot merge two lines if:
1117 | * - their states of being declarations are not equivalent,
1118 | * and we require them to be
1119 | */
1120 | if (flags & MERGE_FLAG_DECL_EQ &&
1121 | (obj_is_declaration(o1) != obj_is_declaration(o2)))
1122 | return false;
1123 |
1124 | /*
1125 | * We can merge the two lines if:
1126 | * - they are the same, or
1127 | * - they are both RH_KABI_HIDE, or
1128 | * - at least one of them is a declaration,
1129 | * and we can merge declarations
1130 | */
1131 | if (obj_eq(o1, o2, flags & MERGE_FLAG_VER_IGNORE))
1132 | return true;
1133 |
1134 | if (obj_is_kabi_hide(o1) && obj_is_kabi_hide(o2))
1135 | return true;
1136 |
1137 | if (flags & MERGE_FLAG_DECL_MERGE &&
1138 | (obj_is_declaration(o1) || obj_is_declaration(o2)))
1139 | return true;
1140 |
1141 | return false;
1142 | }
1143 |
1144 | obj_t *obj_merge(obj_t *o1, obj_t *o2, unsigned int flags)
1145 | {
1146 | obj_t *merged_ptr;
1147 | obj_list_head_t *merged_members;
1148 | obj_t *res = NULL;
1149 |
1150 | if (o1 == NULL || o2 == NULL)
1151 | return NULL;
1152 |
1153 | if (!obj_can_merge_two_lines(o1, o2, flags))
1154 | goto no_merge;
1155 |
1156 | merged_ptr = obj_merge(o1->ptr, o2->ptr, flags);
1157 | if (o1->ptr && !merged_ptr)
1158 | goto no_merge_ptr;
1159 |
1160 | merged_members = obj_members_merge(o1->member_list,
1161 | o2->member_list,
1162 | flags);
1163 | if (o1->member_list && !merged_members)
1164 | goto no_merge_members;
1165 |
1166 | if (obj_is_declaration(o1))
1167 | res = obj_copy(o2);
1168 | else
1169 | res = obj_copy(o1);
1170 |
1171 | res->ptr = merged_ptr;
1172 |
1173 | if (merged_members != NULL)
1174 | merged_members->object = res;
1175 | res->member_list = merged_members;
1176 |
1177 | return res;
1178 |
1179 | no_merge_members:
1180 | obj_list_free(merged_members);
1181 | no_merge_ptr:
1182 | obj_free(merged_ptr);
1183 | no_merge:
1184 | return NULL;
1185 | }
1186 |
1187 | static void dump_reffile(obj_t *o, FILE *f)
1188 | {
1189 | int version = record_get_version(o->ref_record);
1190 |
1191 | fprintf(f, "@\"%s", record_get_key(o->ref_record));
1192 | if (version > 0)
1193 | fprintf(f, "-%i", version);
1194 | fprintf(f, ".txt\"\n");
1195 | }
1196 |
1197 | static void _dump_members(obj_t *o, FILE *f, void (*dumper)(obj_t *, FILE *))
1198 | {
1199 | obj_list_head_t *l = o->member_list;
1200 | obj_list_t *list;
1201 |
1202 | if (l == NULL)
1203 | return;
1204 |
1205 | list = l->first;
1206 |
1207 | while (list) {
1208 | dumper(list->member, f);
1209 | list = list->next;
1210 | }
1211 | }
1212 |
1213 | static void dump_arg(obj_t *o, FILE *f)
1214 | {
1215 | fprintf(f, "%s ", o->name);
1216 | obj_dump(o->ptr, f);
1217 | }
1218 |
1219 | static void dump_members(obj_t *o, FILE *f)
1220 | {
1221 | _dump_members(o, f, obj_dump);
1222 | }
1223 |
1224 | static void dump_args(obj_t *o, FILE *f)
1225 | {
1226 | _dump_members(o, f, dump_arg);
1227 | }
1228 |
1229 | static void dump_struct(obj_t *o, FILE *f)
1230 | {
1231 | fprintf(f, "struct %s {\n", o->name);
1232 | dump_members(o, f);
1233 | fprintf(f, "}\n");
1234 | }
1235 | static void dump_union(obj_t *o, FILE *f)
1236 | {
1237 | fprintf(f, "union %s {\n", o->name);
1238 | dump_args(o, f);
1239 | fprintf(f, "}\n");
1240 | }
1241 |
1242 | static void dump_enum(obj_t *o, FILE *f)
1243 | {
1244 | fprintf(f, "enum %s {\n", o->name);
1245 | dump_members(o, f);
1246 | fprintf(f, "}\n");
1247 | }
1248 |
1249 | static void dump_func(obj_t *o, FILE *f)
1250 | {
1251 | fprintf(f, "func %s (\n", o->name);
1252 | dump_args(o, f);
1253 | fprintf(f, ")\n");
1254 |
1255 | obj_dump(o->ptr, f);
1256 | }
1257 |
1258 | static void dump_ptr(obj_t *o, FILE *f)
1259 | {
1260 | fprintf(f, "* ");
1261 | obj_dump(o->ptr, f);
1262 | }
1263 |
1264 | static void dump_typedef(obj_t *o, FILE *f)
1265 | {
1266 | fprintf(f, "typedef %s\n", o->name);
1267 | obj_dump(o->ptr, f);
1268 | }
1269 |
1270 | static void dump_array(obj_t *o, FILE *f)
1271 | {
1272 | fprintf(f, "[%lu]", o->index);
1273 | obj_dump(o->ptr, f);
1274 | }
1275 |
1276 | static void dump_var(obj_t *o, FILE *f)
1277 | {
1278 | fprintf(f, "var %s ", o->name);
1279 | obj_dump(o->ptr, f);
1280 | }
1281 |
1282 | static void dump_struct_member(obj_t *o, FILE *f)
1283 | {
1284 | fprintf(f, "0x%lx", o->offset);
1285 | if (o->is_bitfield)
1286 | fprintf(f, ":%d-%d", o->first_bit, o->last_bit);
1287 |
1288 | if (o->alignment != 0)
1289 | fprintf(f, " %u", o->alignment);
1290 |
1291 | fprintf(f, " %s ", o->name);
1292 | obj_dump(o->ptr, f);
1293 | }
1294 |
1295 | static void dump_qualifier(obj_t *o, FILE *f)
1296 | {
1297 | fprintf(f, "%s ", o->base_type);
1298 | obj_dump(o->ptr, f);
1299 | }
1300 |
1301 | static void dump_base(obj_t *o, FILE *f)
1302 | {
1303 | const char *type = o->base_type;
1304 |
1305 | /* variable args (...) is a special base case */
1306 | if (type[0] == '.')
1307 | fprintf(f, "%s\n", o->base_type);
1308 | else
1309 | fprintf(f, "\"%s\"\n", o->base_type);
1310 | }
1311 |
1312 | static void dump_constant(obj_t *o, FILE *f)
1313 | {
1314 | fprintf(f, "%s = 0x%lx\n", o->name, o->constant);
1315 | }
1316 |
1317 | static void dump_fail(obj_t *o, FILE *f)
1318 | {
1319 | fail("Dump call for this type unsupported!\n");
1320 | }
1321 |
1322 | struct dumper {
1323 | void (*dumper)(obj_t *o, FILE *f);
1324 | };
1325 |
1326 | static struct dumper dumpers[] = {
1327 | [__type_reffile].dumper = dump_reffile,
1328 | [__type_struct].dumper = dump_struct,
1329 | [__type_union].dumper = dump_union,
1330 | [__type_enum].dumper = dump_enum,
1331 | [__type_func].dumper = dump_func,
1332 | [__type_ptr].dumper = dump_ptr,
1333 | [__type_typedef].dumper = dump_typedef,
1334 | [__type_array].dumper = dump_array,
1335 | [__type_var].dumper = dump_var,
1336 | [__type_struct_member].dumper = dump_struct_member,
1337 | [__type_qualifier].dumper = dump_qualifier,
1338 | [__type_base].dumper = dump_base,
1339 | [__type_constant].dumper = dump_constant,
1340 | [__type_assembly].dumper = dump_fail,
1341 | [__type_weak].dumper = dump_fail,
1342 | };
1343 |
1344 | void obj_dump(obj_t *o, FILE *f)
1345 | {
1346 | if (o == NULL)
1347 | return;
1348 |
1349 | if (o->type >= NR_OBJ_TYPES)
1350 | fail("Wrong object type %d", o->type);
1351 |
1352 | dumpers[o->type].dumper(o, f);
1353 | }
1354 |
1355 | bool obj_same_declarations(obj_t *o1, obj_t *o2,
1356 | struct set *processed)
1357 | {
1358 | const int ignore_versions = true;
1359 | obj_list_t *list1;
1360 | obj_list_t *list2;
1361 |
1362 | if (o1 == o2)
1363 | return true;
1364 |
1365 | if (!obj_eq(o1, o2, ignore_versions))
1366 | return false;
1367 |
1368 | if (o1->type != o2->type ||
1369 | (o1->ptr == NULL) != (o2->ptr == NULL) ||
1370 | (o1->member_list == NULL) != (o2->member_list == NULL)) {
1371 | return false;
1372 | }
1373 |
1374 |
1375 | if (o1->type == __type_reffile &&
1376 | !record_same_declarations(o1->ref_record, o2->ref_record,
1377 | processed)) {
1378 | return false;
1379 | }
1380 |
1381 | if (o1->ptr &&
1382 | !obj_same_declarations(o1->ptr, o2->ptr, processed)) {
1383 | return false;
1384 | }
1385 |
1386 | if (o1->member_list) {
1387 | list1 = o1->member_list->first;
1388 | list2 = o2->member_list->first;
1389 |
1390 | while (list1) {
1391 | if (list2 == NULL)
1392 | return false;
1393 |
1394 | if (!obj_same_declarations(list1->member,
1395 | list2->member,
1396 | processed))
1397 | return false;
1398 |
1399 | list1 = list1->next;
1400 | list2 = list2->next;
1401 | }
1402 |
1403 | if (list1 != list2) {
1404 | /* different member_list lengths */
1405 | return false;
1406 | }
1407 | }
1408 |
1409 | return true;
1410 | }
1411 |
--------------------------------------------------------------------------------
/objects.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright(C) 2016, Red Hat, Inc., Jerome Marchand
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | /*
19 | * Internal representation of symbols
20 | */
21 |
22 | #ifndef _OBJECTS_H
23 | #define _OBJECTS_H
24 |
25 | #include
26 | #include
27 |
28 | #include "list.h"
29 | #include "utils.h"
30 |
31 | #ifdef DEBUG
32 | #define debug(args...) do { printf(args); } while (0)
33 | #else
34 | #define debug(args...)
35 | #endif
36 |
37 | struct set;
38 |
39 | enum merge_flag {
40 | MERGE_DEFAULT = 0,
41 | MERGE_FLAG_DECL_MERGE = 1 << 0,
42 | MERGE_FLAG_VER_IGNORE = 1 << 1,
43 | MERGE_FLAG_DECL_EQ = 1 << 2,
44 | };
45 |
46 | typedef enum {
47 | __type_reffile,
48 | __type_struct,
49 | __type_union,
50 | __type_enum,
51 | __type_func,
52 | __type_ptr,
53 | __type_typedef,
54 | __type_array,
55 | __type_var, /* a variable, member of an union or a function argument */
56 | __type_struct_member,
57 | __type_qualifier, /* a type qualifier such as "const" or "volatile" */
58 | __type_base,
59 | __type_constant, /* An element of an enumeration */
60 | __type_assembly,
61 | __type_weak,
62 | NR_OBJ_TYPES
63 | } obj_types;
64 |
65 | struct obj;
66 | typedef struct obj_list {
67 | struct obj *member;
68 | struct obj_list *next;
69 | } obj_list_t;
70 |
71 | typedef struct obj_list_head {
72 | obj_list_t *first, *last;
73 | struct obj *object;
74 | } obj_list_head_t;
75 |
76 | /*
77 | * Structure representing symbols. Several field are overloaded.
78 | *
79 | * type: type of the symbol (such as struct, function, pointer, base
80 | * type...)
81 | * is_bitfield: (var) It's a bitfield
82 | * first_bit, last_bit: (var) bit range within the offset.
83 | * name: name of the symbol
84 | * ref_record: (reffile) pointer to the referenced record (only while
85 | * generating records, otherwise base_type with string is used)
86 | * base_type: (base type) the type of the symbol,
87 | * (qualifier) the type qualifier (const or volatile)
88 | * (reffile) path to the file
89 | * alignment: value of DW_AT_alignment attribute or 0 if not present
90 | * member_list: (struct, union, enum) list of members
91 | * (function) list of arguments
92 | * ptr: (pointer) object pointed to
93 | * (typedef) defined type
94 | * (function) return type
95 | * (var) type
96 | * constant: (constant) constant value of an enumeration
97 | * index: (array) index of array
98 | * link: (weak) weak alias link
99 | * offset: (var) offset of a struct member
100 | * depend_rec_node: (reffile) node from dependents field of record where
101 | * this obj references.
102 | *
103 | * Note the dual parent/child relationship with the n-ary member_list and the
104 | * the unary ptr. Only functions uses both.
105 | */
106 | typedef struct obj {
107 | obj_types type;
108 | unsigned char is_bitfield, first_bit, last_bit;
109 | union {
110 | const char *name;
111 | struct record *ref_record;
112 | };
113 | const char *base_type;
114 | unsigned alignment;
115 | unsigned int byte_size;
116 | obj_list_head_t *member_list;
117 | struct obj *ptr, *parent;
118 | union {
119 | unsigned long constant;
120 | unsigned long index;
121 | char *link;
122 | unsigned long offset;
123 | struct list_node *depend_rec_node;
124 | };
125 | char *ns;
126 | } obj_t;
127 |
128 | static inline bool has_offset(obj_t *o)
129 | {
130 | return o->type == __type_struct_member;
131 | }
132 |
133 | static inline bool has_constant(obj_t *o)
134 | {
135 | return o->type == __type_constant;
136 | }
137 |
138 | static inline bool has_index(obj_t *o)
139 | {
140 | return o->type == __type_array;
141 | }
142 |
143 | static inline bool is_bitfield(obj_t *o)
144 | {
145 | return o->is_bitfield != 0;
146 | }
147 |
148 | static inline bool is_terminal(obj_t *o)
149 | {
150 | switch (o->type) {
151 | case __type_reffile:
152 | case __type_base:
153 | case __type_constant:
154 | return true;
155 | default:
156 | return false;
157 | }
158 | }
159 |
160 | static inline bool is_unary(obj_t *o)
161 | {
162 | switch (o->type) {
163 | case __type_ptr:
164 | case __type_typedef:
165 | case __type_array:
166 | case __type_var:
167 | case __type_struct_member:
168 | case __type_qualifier:
169 | return true;
170 | default:
171 | return false;
172 | }
173 | }
174 |
175 | static inline bool is_n_ary(obj_t *o)
176 | {
177 | switch (o->type) {
178 | case __type_struct:
179 | case __type_union:
180 | case __type_enum:
181 | case __type_func:
182 | return true;
183 | default:
184 | return false;
185 | }
186 | }
187 |
188 | static inline bool is_weak(obj_t *o)
189 | {
190 | return o->type == __type_weak;
191 | }
192 |
193 | /*
194 | * Display options
195 | *
196 | * Used for show and compare commands.
197 | */
198 | struct dopt {
199 | int no_offset; /* Don't display struct offset */
200 | };
201 | extern struct dopt display_options;
202 |
203 | /* Return values for tree walk callbacks */
204 | typedef enum {
205 | CB_CONT = 0, /* Continue tree walk */
206 | CB_SKIP, /* Skip the children of this node */
207 | CB_FAIL, /* Failed: stop the walk */
208 | } cb_ret_t;
209 |
210 | typedef int cb_t(obj_t *o, void *args);
211 |
212 | obj_list_t *obj_list_new(obj_t *obj);
213 | obj_list_head_t *obj_list_head_new(obj_t *obj);
214 | void obj_list_add(obj_list_head_t *head, obj_t *obj);
215 | void obj_free(obj_t *o);
216 |
217 | obj_t *obj_struct_new(char *name);
218 | obj_t *obj_union_new(char *name);
219 | obj_t *obj_enum_new(char *name);
220 | obj_t *obj_constant_new(char *name);
221 | obj_t *obj_reffile_new();
222 | obj_t *obj_func_new_add(char *name, obj_t *obj);
223 | obj_t *obj_typedef_new_add(char *name, obj_t *obj);
224 | obj_t *obj_var_new_add(char *name, obj_t *obj);
225 | obj_t *obj_struct_member_new_add(char *name, obj_t *obj);
226 | obj_t *obj_ptr_new_add(obj_t *obj);
227 | obj_t *obj_array_new_add(obj_t *obj);
228 | obj_t *obj_qualifier_new_add(obj_t *obj);
229 | obj_t *obj_assembly_new(char *name);
230 | obj_t *obj_weak_new(char *name);
231 |
232 | obj_t *obj_basetype_new(char *base_type);
233 |
234 | void obj_print_tree(obj_t *root);
235 | void obj_print_tree__prefix(obj_t *root, const char *prefix, FILE *stream);
236 | int obj_debug_tree(obj_t *root);
237 | void obj_fill_parent(obj_t *root);
238 | int obj_walk_tree(obj_t *root, cb_t cb, void *args);
239 | int obj_walk_tree3(obj_t *o, cb_t cb_pre, cb_t cb_in, cb_t cb_post,
240 | void *args, bool ptr_first);
241 |
242 | int obj_hide_kabi(obj_t *root, bool show_new_field);
243 |
244 | obj_t *obj_parse(FILE *file, char *fn);
245 | obj_t *obj_merge(obj_t *o1, obj_t *o2, unsigned int flags);
246 | void obj_dump(obj_t *o, FILE *f);
247 |
248 | bool obj_eq(obj_t *o1, obj_t *o2, bool ignore_versions);
249 |
250 | bool obj_same_declarations(obj_t *o1, obj_t *o2, struct set *processed);
251 |
252 | #endif
253 |
--------------------------------------------------------------------------------
/parser.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright(C) 2016, Red Hat, Inc., Jerome Marchand
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | #include
19 | #include
20 | #include
21 | #include
22 |
23 | #include "objects.h"
24 |
25 | /* Prevents warning: ‘input’ defined but not used */
26 | #define YY_NO_INPUT
27 |
28 | /* Why can't it get it from stdio.h? */
29 | int fileno(FILE *stream);
30 |
31 | int yylex();
32 | int yyerror(obj_t **root, char *s);
33 | extern FILE *yyin;
34 |
--------------------------------------------------------------------------------
/parser.l:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright(C) 2016, Red Hat, Inc., Jerome Marchand
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | NUM [0-9]
19 | ALPHA [a-zA-Z]
20 | IDENT_FIRST [a-zA-Z_]
21 | IDENT [a-zA-Z0-9_]
22 | FILECHAR [a-zA-Z0-9_/<>\-\.]
23 | HEX [a-fA-F0-9]
24 |
25 | %{
26 | #include "parser.h"
27 | #include "parser.tab.h"
28 | %}
29 |
30 | %option nounput
31 | %option noyywrap
32 | %s SYMBOL
33 | %x IN_STRING UNKNOWN_FIELD
34 |
35 | %%
36 | static int last_sc = INITIAL;
37 |
38 | { /* Keywords used by symbol declaration */
39 | "const" { return(CONST); }
40 | "enum" { return(ENUM); }
41 | "struct" { return(STRUCT); }
42 | "typedef" { return(TYPEDEF); }
43 | "union" { return(UNION); }
44 | "volatile" { return(VOLATILE); }
45 | "restrict" { return(RESTRICT); }
46 |
47 | "..." { return(ELLIPSIS); }
48 | }
49 |
50 |
51 | { /* keywords used only in the header */
52 | "Version:" { BEGIN(INITIAL); return(VERSION_KW); }
53 | "CU:" { BEGIN(INITIAL); return(CU_KW); }
54 | "File:" { BEGIN(INITIAL); return(FILE_KW); }
55 | "Stack:" { BEGIN(INITIAL); return(STACK_KW); }
56 | "Symbol:\n" { BEGIN(SYMBOL); return(SYMBOL_KW_NL); }
57 | {IDENT}+":" { BEGIN(UNKNOWN_FIELD); }
58 | }
59 |
60 | { /* Ignore unknown fields. */
61 | [^\n:]*"\n" { ; }
62 | /* A line that doesn't start with {IDENT}+":", i.e. not a valid field */
63 | {IDENT}*[^a-zA-Z0-9_\n:]+{IDENT}*[^\n]*"\n" { ; }
64 | }
65 |
66 | "->" { return(ARROW); }
67 |
68 | {FILECHAR}+"."[chS]|"" { yylval.str = strdup(yytext);
69 | debug("Source file: %s\n", yylval.str);
70 | return(SRCFILE);
71 | }
72 |
73 | ^"Namespace" { debug("Namespace\n");
74 | return(NAMESPACE);
75 | }
76 |
77 | {IDENT_FIRST}{IDENT}* { yylval.str = strdup(yytext);
78 | debug("Identifier: %s\n", yylval.str);
79 | return(IDENTIFIER);
80 | }
81 | "(NULL)" { yylval.str = NULL;
82 | debug("Identifier: (NULL)\n");
83 | return(IDENTIFIER);
84 | }
85 |
86 | 0[xX]{HEX}+ { yylval.ul = strtoul(yytext, NULL, 16);
87 | debug("Constant: 0x%lx\n", yylval.ul);
88 | return(CONSTANT);
89 | }
90 | {NUM}+ { yylval.ul = strtoul(yytext, NULL, 10);
91 | debug("Constant: %li\n", yylval.ul);
92 | return(CONSTANT);
93 | }
94 |
95 | [{}()\[\];:,.*@=-] { return(yytext[0]); }
96 |
97 | "\n" { return(NEWLINE); }
98 |
99 | [ \t\v\f] { ; }
100 |
101 | "\"" { last_sc = YY_START; BEGIN(IN_STRING); }
102 | [^"]* { yylval.str = strdup(yytext);
103 | debug("String: %s\n", yylval.str);
104 | return(STRING);
105 | }
106 | "\"" { BEGIN(last_sc); }
107 | . { printf("Unexpected entry \"%c\"\n", *yytext); }
108 |
109 | /* Get back to initial condition or we'll start the next file in SYMBOL */
110 | <> { BEGIN(INITIAL); yyterminate(); }
111 |
112 | %%
113 |
--------------------------------------------------------------------------------
/parser.y:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright(C) 2016, Red Hat, Inc., Jerome Marchand
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | %{
19 | #include "parser.h"
20 | #include
21 |
22 | #include "utils.h"
23 |
24 | #define abort(...) \
25 | { \
26 | fprintf(stderr, __VA_ARGS__); \
27 | YYABORT; \
28 | }
29 |
30 | #define check_and_free_keyword(identifier, expected) \
31 | { \
32 | if (strcmp(identifier, expected)) \
33 | abort("Wrong keyword: %s expected, %s received\n", \
34 | expected, identifier); \
35 | free(identifier); \
36 | }
37 |
38 |
39 | %}
40 |
41 | %union {
42 | int i;
43 | unsigned int ui;
44 | long l;
45 | unsigned long ul;
46 | void *ptr;
47 | char *str;
48 | obj_t *obj;
49 | obj_list_head_t *list;
50 | }
51 |
52 | %token IDENTIFIER STRING SRCFILE
53 | %token CONSTANT
54 |
55 | %token NEWLINE
56 | %token TYPEDEF
57 | %token CONST VOLATILE RESTRICT
58 | %token STRUCT UNION ENUM ELLIPSIS
59 | %token VERSION_KW CU_KW FILE_KW STACK_KW SYMBOL_KW_NL
60 | %token ARROW UNKNOWN_FIELD
61 | %token NAMESPACE
62 |
63 | %type type_qualifier
64 | %type typed_type base_type reference_file array_type
65 | %type type ptr_type variable_var_list func_type elt enum_elt enum_type
66 | %type union_type struct_type struct_elt
67 | %type declaration_var declaration_typedef declaration
68 | %type kabi_dw_file symbol
69 | %type asm_symbol weak_symbol
70 | %type elt_list arg_list enum_list struct_list
71 | %type alignment byte_size
72 | %type namespace
73 |
74 | %parse-param {obj_t **root}
75 |
76 | %%
77 |
78 | kabi_dw_file:
79 | fmt_version header SYMBOL_KW_NL symbol
80 | {
81 | $$ = *root = $symbol;
82 | obj_fill_parent(*root);
83 | }
84 | ;
85 |
86 | fmt_version:
87 | VERSION_KW CONSTANT '.' CONSTANT NEWLINE
88 | {
89 | if (($2 != FILEFMT_VERSION_MAJOR) |
90 | ($4 > FILEFMT_VERSION_MINOR))
91 | abort("Unsupported file version: %lu.%lu\n", $2, $4);
92 | }
93 | ;
94 |
95 | header:
96 | /* empty */
97 | | header header_field
98 | ;
99 |
100 | header_field:
101 | cu_field
102 | | source_file_field
103 | | stack_field
104 | | UNKNOWN_FIELD
105 | ;
106 |
107 | cu_field:
108 | CU_KW STRING NEWLINE
109 | {
110 | free($STRING);
111 | }
112 | ;
113 |
114 | source_file_field:
115 | FILE_KW SRCFILE ':' CONSTANT NEWLINE
116 | {
117 | free($SRCFILE);
118 | }
119 | ;
120 |
121 | stack_field:
122 | STACK_KW NEWLINE stack_list
123 | ;
124 |
125 | stack_list:
126 | /* empty */
127 | | stack_list stack_elt NEWLINE
128 | ;
129 |
130 | stack_elt:
131 | ARROW STRING
132 | {
133 | free($STRING);
134 | }
135 | ;
136 |
137 | symbol:
138 | declaration NEWLINE
139 | {
140 | $$ = $declaration;
141 | }
142 | | alignment declaration NEWLINE
143 | {
144 | $$ = $declaration;
145 | $$->alignment = $alignment;
146 | }
147 | | alignment namespace declaration NEWLINE
148 | {
149 | $$ = $declaration;
150 | $$->alignment = $alignment;
151 | $$->ns = $namespace;
152 | }
153 | | namespace declaration NEWLINE
154 | {
155 | $$ = $declaration;
156 | $$->ns = $namespace;
157 | }
158 | | byte_size declaration NEWLINE
159 | {
160 | $$ = $declaration;
161 | $$->byte_size = $byte_size;
162 | }
163 | | byte_size alignment declaration NEWLINE
164 | {
165 | $$ = $declaration;
166 | $$->byte_size = $byte_size;
167 | $$->alignment = $alignment;
168 | }
169 | | byte_size namespace declaration NEWLINE
170 | {
171 | $$ = $declaration;
172 | $$->byte_size = $byte_size;
173 | $$->ns = $namespace;
174 | }
175 | | byte_size alignment namespace declaration NEWLINE
176 | {
177 | $$ = $declaration;
178 | $$->byte_size = $byte_size;
179 | $$->alignment = $alignment;
180 | $$->ns = $namespace;
181 | }
182 |
183 | alignment:
184 | IDENTIFIER CONSTANT NEWLINE
185 | {
186 | check_and_free_keyword($IDENTIFIER, "Alignment");
187 | $$ = $CONSTANT;
188 | }
189 |
190 | byte_size:
191 | IDENTIFIER IDENTIFIER CONSTANT NEWLINE
192 | {
193 | check_and_free_keyword($1, "Byte");
194 | check_and_free_keyword($2, "size");
195 | $$ = $CONSTANT;
196 | }
197 |
198 | namespace:
199 | NAMESPACE IDENTIFIER NEWLINE
200 | {
201 | $$ = $IDENTIFIER;
202 | }
203 |
204 | /* Possible types are struct union enum func typedef and var */
205 | declaration:
206 | struct_type
207 | | union_type
208 | | enum_type
209 | | func_type
210 | | declaration_typedef
211 | | declaration_var
212 | | weak_symbol
213 | | asm_symbol
214 | ;
215 |
216 | declaration_typedef:
217 | TYPEDEF IDENTIFIER NEWLINE type
218 | {
219 | $$ = obj_typedef_new_add($IDENTIFIER, $type);
220 | }
221 | ;
222 |
223 | declaration_var:
224 | IDENTIFIER IDENTIFIER type
225 | {
226 | check_and_free_keyword($1, "var");
227 | $$ = obj_var_new_add($2, $type);
228 | }
229 | ;
230 |
231 | type:
232 | base_type
233 | | reference_file
234 | | struct_type
235 | | union_type
236 | | enum_type
237 | | func_type
238 | | ptr_type
239 | | array_type
240 | | typed_type
241 | ;
242 |
243 | struct_type:
244 | STRUCT IDENTIFIER '{' NEWLINE '}'
245 | {
246 | $$ = obj_struct_new($IDENTIFIER);
247 | }
248 | | STRUCT IDENTIFIER '{' NEWLINE struct_list NEWLINE '}'
249 | {
250 | $$ = obj_struct_new($IDENTIFIER);
251 | $$->member_list = $struct_list;
252 | }
253 | ;
254 |
255 | struct_list:
256 | struct_elt
257 | {
258 | $$ = obj_list_head_new($struct_elt);
259 | }
260 | | struct_list NEWLINE struct_elt
261 | {
262 | obj_list_add($1, $struct_elt);
263 | $$ = $1;
264 | }
265 | ;
266 |
267 | struct_elt:
268 | CONSTANT IDENTIFIER type
269 | {
270 | $$ = obj_struct_member_new_add($IDENTIFIER, $type);
271 | $$->offset = $CONSTANT;
272 | }
273 | /* with alignment */
274 | | CONSTANT CONSTANT IDENTIFIER type
275 | {
276 | $$ = obj_struct_member_new_add($IDENTIFIER, $type);
277 | $$->offset = $1;
278 | $$->alignment = $2;
279 | }
280 | | CONSTANT ':' CONSTANT '-' CONSTANT IDENTIFIER type
281 | {
282 | if ($5 > UCHAR_MAX || $3 > $5)
283 | abort("Invalid offset: %lx:%lu:%lu\n", $1, $3, $5);
284 | $$ = obj_struct_member_new_add($IDENTIFIER, $type);
285 | $$->offset = $1;
286 | $$->is_bitfield = 1;
287 | $$->first_bit = $3;
288 | $$->last_bit = $5;
289 | }
290 | /* with alignment */
291 | | CONSTANT ':' CONSTANT '-' CONSTANT CONSTANT IDENTIFIER type
292 | {
293 | if ($5 > UCHAR_MAX || $3 > $5)
294 | abort("Invalid offset: %lx:%lu:%lu\n", $1, $3, $5);
295 | $$ = obj_struct_member_new_add($IDENTIFIER, $type);
296 | $$->offset = $1;
297 | $$->is_bitfield = 1;
298 | $$->first_bit = $3;
299 | $$->last_bit = $5;
300 | $$->alignment = $6;
301 | }
302 | ;
303 |
304 | union_type:
305 | UNION IDENTIFIER '{' NEWLINE '}'
306 | {
307 | $$ = obj_union_new($IDENTIFIER);
308 | }
309 | | UNION IDENTIFIER '{' NEWLINE elt_list NEWLINE '}'
310 | {
311 | $$ = obj_union_new($IDENTIFIER);
312 | $$->member_list = $elt_list;
313 | $elt_list->object = $$;
314 | }
315 | ;
316 |
317 | enum_type:
318 | ENUM IDENTIFIER '{' NEWLINE enum_list NEWLINE '}'
319 | {
320 | $$ = obj_enum_new($IDENTIFIER);
321 | $$->member_list = $enum_list;
322 | $enum_list->object = $$;
323 | }
324 | ;
325 |
326 | enum_list:
327 | enum_elt
328 | {
329 | $$ = obj_list_head_new($enum_elt);
330 | }
331 | | enum_list NEWLINE enum_elt
332 | {
333 | obj_list_add($1, $enum_elt);
334 | $$ = $1;
335 | }
336 | ;
337 |
338 | enum_elt:
339 | IDENTIFIER '=' CONSTANT
340 | {
341 | $$ = obj_constant_new($IDENTIFIER);
342 | $$->constant = $CONSTANT;
343 | }
344 | ;
345 |
346 | func_type:
347 | IDENTIFIER IDENTIFIER '(' NEWLINE arg_list ')' NEWLINE type
348 | {
349 | check_and_free_keyword($1, "func");
350 | $$ = obj_func_new_add($2, $type);
351 | $$->member_list = $arg_list;
352 | if ($arg_list)
353 | $arg_list->object = $$;
354 | }
355 | | IDENTIFIER reference_file /* protype define as typedef */
356 | {
357 | check_and_free_keyword($IDENTIFIER, "func");
358 | $$ = obj_func_new_add(NULL, $reference_file);
359 | }
360 | ;
361 |
362 | arg_list:
363 | /* empty */
364 | {
365 | $$ = NULL;
366 | }
367 | | elt_list NEWLINE
368 | {
369 | $$ = $elt_list;
370 | }
371 | | elt_list NEWLINE variable_var_list NEWLINE
372 | {
373 | obj_list_add($elt_list, $variable_var_list);
374 | $$ = $elt_list;
375 | }
376 | ;
377 |
378 | variable_var_list:
379 | IDENTIFIER ELLIPSIS
380 | {
381 | /* TODO: there may be a better solution */
382 | $$ = obj_var_new_add(NULL, obj_basetype_new(strdup("...")));
383 | }
384 | ;
385 |
386 | elt_list:
387 | elt
388 | {
389 | $$ = obj_list_head_new($elt);
390 | }
391 | | elt_list NEWLINE elt
392 | {
393 | obj_list_add($1, $elt);
394 | $$ = $1;
395 | }
396 | ;
397 |
398 | elt:
399 | IDENTIFIER type
400 | {
401 | $$ = obj_var_new_add($IDENTIFIER, $type);
402 | }
403 | ;
404 |
405 | ptr_type:
406 | '*' type
407 | {
408 | $$ = obj_ptr_new_add($type);
409 | }
410 | ;
411 |
412 | array_type:
413 | '['CONSTANT ']' type
414 | {
415 | $$ = obj_array_new_add($type);
416 | $$->index = $CONSTANT;
417 | }
418 | ;
419 |
420 | typed_type:
421 | type_qualifier type
422 | {
423 | $$ = obj_qualifier_new_add($type);
424 | $$->base_type = $type_qualifier;
425 | }
426 | ;
427 |
428 | type_qualifier:
429 | CONST
430 | {
431 | debug("Qualifier: const\n");
432 | $$ = strdup("const");
433 | }
434 | | VOLATILE
435 | {
436 | debug("Qualifier: volatile\n");
437 | $$ = strdup("volatile");
438 | }
439 | | RESTRICT
440 | {
441 | debug("Qualifier: restrict\n");
442 | $$ = strdup("restrict");
443 | }
444 | ;
445 |
446 | base_type:
447 | STRING
448 | {
449 | debug("Base type: %s\n", $STRING);
450 | $$ = obj_basetype_new($STRING);
451 | }
452 | ;
453 |
454 | reference_file:
455 | '@' STRING
456 | {
457 | $$ = obj_reffile_new();
458 | $$->base_type = $STRING;
459 | }
460 | ;
461 |
462 | asm_symbol:
463 | IDENTIFIER IDENTIFIER
464 | {
465 | check_and_free_keyword($1, "assembly");
466 | $$ = obj_assembly_new($2);
467 | }
468 | ;
469 |
470 | weak_symbol:
471 | IDENTIFIER IDENTIFIER ARROW IDENTIFIER
472 | {
473 | check_and_free_keyword($1, "weak");
474 | $$ = obj_weak_new($2);
475 | $$->link = $4;
476 | }
477 | ;
478 |
479 |
480 | %%
481 |
482 | extern void usage(void);
483 |
484 | obj_t *obj_parse(FILE *file, char *fn) {
485 | obj_t *root = NULL;
486 |
487 | #ifdef DEBUG
488 | yydebug = 1;
489 | #else
490 | yydebug = 0;
491 | #endif
492 |
493 | yyin = file;
494 | yyparse(&root);
495 | if (!root)
496 | fail("No object build for file %s\n", fn);
497 |
498 | return root;
499 | }
500 |
501 | int yyerror(obj_t **root, char *s)
502 | {
503 | fprintf(stderr, "error: %s\n", s);
504 | return 0;
505 | }
506 |
--------------------------------------------------------------------------------
/record.h:
--------------------------------------------------------------------------------
1 | #ifndef RECORD_H_
2 | #define RECORD_H_
3 |
4 | #include
5 | #include
6 |
7 | #include "list.h"
8 | #include "stack.h"
9 | #include "objects.h"
10 |
11 | #define RECORD_VERSION_DECLARATION -1
12 |
13 | /*
14 | * Structure of the database record:
15 | *
16 | * key: record key, usually includes path the file, where the type is
17 | * defined (may include pseudo path, like );
18 | * Does not contain version and the .txt suffix.
19 | *
20 | * version: type's version, used when we need to add another type of the same
21 | * name. It may happend, for example, when because of defines the same
22 | * structure has changed for different compilation units.
23 | *
24 | * It is not for the case, when the same structure defined in
25 | * different files -- it will have different keys, since it includes
26 | * the path;
27 | *
28 | * ref_count: reference counter, needed since the ownership is shared with the
29 | * internal database;
30 | *
31 | * cu: compilation unit, where the type for the record defined;
32 | *
33 | * origin: "File :" string, describing the source, where the type
34 | * for the record defined;
35 | *
36 | * stack: stack of types to reach this one.
37 | * Ex.: on the toplevel
38 | * struct A {
39 | * struct B fieldA;
40 | * }
41 | * in another file:
42 | * struct B {
43 | * basetype fieldB;
44 | * }
45 | * the "struct B" description will contain key of the "struct A"
46 | * description record in the stack;
47 | *
48 | * obj: pointer to the abstract type object, representing the toplevel type of
49 | * the record.
50 | *
51 | * link: name of weak link alisas for the weak aliases.
52 | *
53 | * free: type specific function to free the record
54 | * (there are normal, weak and assembly records).
55 | *
56 | * dump: type specific function for record output.
57 | *
58 | * dependents: objects that reference this record.
59 | *
60 | * list_node: node containing the record, record only belong to one list
61 | * at a time(usually record_list.records)
62 | *
63 | * failed: number of times the record could not be used for merging
64 | */
65 | struct record {
66 | const char *key;
67 | int version;
68 | int ref_count;
69 | char *cu;
70 | const char *origin;
71 | stack_t *stack;
72 | obj_t *obj;
73 | char *link;
74 | void (*free)(struct record *);
75 | void (*dump)(struct record *, FILE *);
76 |
77 | struct list dependents;
78 | struct list_node *list_node;
79 | unsigned int failed;
80 | };
81 |
82 | static inline const char *record_get_key(struct record *record)
83 | {
84 | return record->key;
85 | }
86 |
87 | static inline int record_get_version(struct record *record)
88 | {
89 | return record->version;
90 | }
91 |
92 | static inline bool record_is_declaration(struct record *record)
93 | {
94 | return record->version == RECORD_VERSION_DECLARATION;
95 | }
96 |
97 | bool record_same_declarations(struct record *r1, struct record *r2,
98 | struct set *processed);
99 |
100 | #endif /* RECORD_H_ */
101 |
--------------------------------------------------------------------------------
/show.c:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright(C) 2017, Red Hat, Inc.
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | #include
19 | #include
20 | #include
21 | #include "objects.h"
22 | #include "utils.h"
23 |
24 | struct {
25 | bool debug;
26 | bool hide_kabi;
27 | bool hide_kabi_new;
28 | FILE *file;
29 | } show_config = {false, false, false, NULL};
30 |
31 | static void show_usage()
32 | {
33 | printf("Usage:\n"
34 | "\tshow [options] kabi_file...\n"
35 | "\nOptions:\n"
36 | " -h, --help:\t\tshow this message\n"
37 | " -k, --hide-kabi:\thide changes made by RH_KABI_REPLACE()\n"
38 | " -n, --hide-kabi-new:\n\t\t\thide the kabi trickery made by"
39 | " RH_KABI_REPLACE, but show the new field\n"
40 |
41 | " -d, --debug:\tprint the raw tree\n"
42 | " --no-offset:\tdon't display the offset of struct fields\n");
43 | exit(1);
44 | }
45 |
46 | /*
47 | * Performs the show command
48 | */
49 | int show(int argc, char **argv)
50 | {
51 | obj_t *root;
52 | int opt, opt_index, ret = 0;
53 | struct option loptions[] = {
54 | {"debug", no_argument, 0, 'd'},
55 | {"hide-kabi", no_argument, 0, 'k'},
56 | {"hide-kabi-new", no_argument, 0, 'n'},
57 | {"help", no_argument, 0, 'h'},
58 | {"no-offset", no_argument, &display_options.no_offset, 1},
59 | {0, 0, 0, 0}
60 | };
61 |
62 | memset(&display_options, 0, sizeof(display_options));
63 |
64 | while ((opt = getopt_long(argc, argv, "dknh",
65 | loptions, &opt_index)) != -1) {
66 | switch (opt) {
67 | case 0:
68 | break;
69 | case 'd':
70 | show_config.debug = true;
71 | break;
72 | case 'n':
73 | show_config.hide_kabi_new = true;
74 | /* fall through */
75 | case 'k':
76 | show_config.hide_kabi = true;
77 | break;
78 | case 'h':
79 | default:
80 | show_usage();
81 | }
82 | }
83 |
84 | if (optind >= argc)
85 | show_usage();
86 |
87 | while (optind < argc) {
88 | char *fn = argv[optind++];
89 |
90 | show_config.file = safe_fopen(fn);
91 |
92 | root = obj_parse(show_config.file, fn);
93 |
94 | if (show_config.hide_kabi)
95 | obj_hide_kabi(root, show_config.hide_kabi_new);
96 |
97 | if (show_config.debug)
98 | obj_debug_tree(root);
99 |
100 | obj_print_tree(root);
101 | if (optind < argc)
102 | putchar('\n');
103 |
104 | obj_free(root);
105 | fclose(show_config.file);
106 | }
107 |
108 | return ret;
109 | }
110 |
--------------------------------------------------------------------------------
/show.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright(C) 2017, Red Hat, Inc.
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 |
19 | #ifndef KABI_DW_SHOW_H_
20 | #define KABI_DW_SHOW_H_
21 |
22 | int show(int argc, char **argv);
23 |
24 | #endif
25 |
--------------------------------------------------------------------------------
/stack.c:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright(C) 2016, Red Hat, Inc., Stanislav Kozina
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | /*
19 | * A trivial stack implementation.
20 | */
21 |
22 | #include
23 | #include
24 | #include
25 | #include
26 |
27 | #include "utils.h"
28 | #include "stack.h"
29 |
30 | #define INIT_CAPACITY 10
31 |
32 | stack_t *stack_init(void)
33 | {
34 | stack_t *st = safe_zmalloc(sizeof(*st));
35 |
36 | st->st_capacity = INIT_CAPACITY;
37 | st->st_count = 0;
38 | st->st_data = safe_zmalloc(st->st_capacity * sizeof(*st->st_data));
39 |
40 | return st;
41 | }
42 |
43 | void stack_destroy(stack_t *st)
44 | {
45 | if (st->st_count > 0)
46 | fail("Stack not empty!\n");
47 |
48 | free(st->st_data);
49 | (void) memset(st, 0, sizeof(*st));
50 | free(st);
51 | }
52 |
53 | void stack_push(stack_t *st, void *data)
54 | {
55 | if (st->st_count == st->st_capacity) {
56 | st->st_capacity *= 2;
57 | st->st_data = realloc(st->st_data,
58 | st->st_capacity * sizeof(*st->st_data));
59 | }
60 |
61 | st->st_data[st->st_count] = data;
62 | st->st_count++;
63 | }
64 |
65 | void *stack_pop(stack_t *st)
66 | {
67 | if (st->st_count == 0)
68 | return NULL;
69 |
70 | st->st_count--;
71 | return st->st_data[st->st_count];
72 | }
73 |
74 | void *stack_head(stack_t *st)
75 | {
76 | if (st->st_count == 0)
77 | return NULL;
78 |
79 | return st->st_data[st->st_count-1];
80 | }
81 |
82 | void walk_stack(stack_t *st, void (*cb)(void *, void *), void *arg)
83 | {
84 | unsigned int i;
85 |
86 | for (i = 0; i < st->st_count; i++)
87 | cb(st->st_data[i], arg);
88 | }
89 |
90 | void walk_stack_backward(stack_t *st, void (*cb)(void *, void *), void *arg)
91 | {
92 | unsigned int i;
93 |
94 | for (i = st->st_count; i > 0; i--)
95 | cb(st->st_data[i - 1], arg);
96 | }
97 |
--------------------------------------------------------------------------------
/stack.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright(C) 2016, Red Hat, Inc., Stanislav Kozina
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | #ifndef STACK_H_
19 | #define STACK_H_
20 |
21 | typedef struct {
22 | unsigned int st_capacity; /* Total size of the stack */
23 | unsigned int st_count; /* Number of items stored */
24 | void **st_data; /* Stack itself */
25 | } stack_t;
26 |
27 | extern stack_t *stack_init(void);
28 | extern void stack_destroy(stack_t *);
29 | extern void stack_push(stack_t *, void *);
30 | extern void *stack_pop(stack_t *);
31 | extern void *stack_head(stack_t *);
32 | extern void walk_stack(stack_t *, void (*)(void *, void *), void *);
33 | extern void walk_stack_backward(stack_t *, void (*cb)(void *, void *), void *);
34 |
35 | #endif /* STACK_H_ */
36 |
--------------------------------------------------------------------------------
/utils.c:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright(C) 2016, Red Hat, Inc., Stanislav Kozina
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | /*
19 | * This file contains couple of generally useful functions.
20 | */
21 |
22 | #include
23 | #include
24 | #include
25 | #include
26 | #include
27 | #include
28 | #include
29 | #include
30 | #include
31 | #include
32 | #include /* dirname() */
33 |
34 | #include "main.h"
35 | #include "utils.h"
36 | #include "hash.h"
37 |
38 | /*
39 | * Sort function for scandir.
40 | * Walk regular file first, then process subdirectories.
41 | */
42 | int reg_first(const struct dirent **a, const struct dirent **b)
43 | {
44 | if ((*a)->d_type == DT_REG && (*b)->d_type != DT_REG)
45 | return -1;
46 | if ((*b)->d_type == DT_REG && (*a)->d_type != DT_REG)
47 | return 1;
48 |
49 | /*
50 | * Backup to default collation
51 | * Note: the behavior depends on LC_COLLATE
52 | */
53 | return alphasort(a, b);
54 | }
55 |
56 | /*
57 | * Call cb() on all nodes in the directory structure @path.
58 | * If list_dirs == true run cb() on subdirectories as well, otherwise list only
59 | * files.
60 | * The cb() has to return true if we continue directory walk or false if we're
61 | * all done.
62 | */
63 | void walk_dir(char *path, bool list_dirs, walk_rv_t (*cb)(char *, void *),
64 | void *arg)
65 | {
66 | struct dirent **entlist;
67 | walk_rv_t cb_rv = WALK_CONT;
68 | int entries, i;
69 |
70 | assert(path != NULL && strlen(path) >= 1);
71 |
72 | entries = scandir(path, &entlist, NULL, reg_first);
73 | if (entries == -1) {
74 | fail("Failed to scan module directory %s: %s\n", path,
75 | strerror(errno));
76 | }
77 |
78 | /* process all the files and directories within directory */
79 | for (i = 0; i < entries; i++) {
80 | struct dirent *ent = entlist[i];
81 | struct stat entstat;
82 | char *new_path;
83 |
84 | if ((strcmp(ent->d_name, "..") == 0) ||
85 | (strcmp(ent->d_name, ".") == 0)) {
86 | free(ent);
87 | entlist[i] = NULL;
88 | continue;
89 | }
90 |
91 | if (path[strlen(path) - 1] == '/')
92 | safe_asprintf(&new_path, "%s%s", path, ent->d_name);
93 | else
94 | safe_asprintf(&new_path, "%s/%s", path, ent->d_name);
95 |
96 | if (lstat(new_path, &entstat) != 0) {
97 | fail("Failed to stat directory %s: %s\n", new_path,
98 | strerror(errno));
99 | }
100 |
101 | if (S_ISDIR(entstat.st_mode)) {
102 | if (list_dirs) {
103 | cb_rv = cb(new_path, arg);
104 | if (cb_rv != WALK_CONT)
105 | goto out;
106 | }
107 |
108 | /* Ignore symlinks */
109 | if (!S_ISLNK(entstat.st_mode))
110 | walk_dir(new_path, list_dirs, cb, arg);
111 | } else if (S_ISREG(entstat.st_mode)) {
112 | cb_rv = cb(new_path, arg);
113 | }
114 |
115 | out:
116 | free(new_path);
117 | free(ent);
118 | entlist[i] = NULL;
119 |
120 | if (cb_rv == WALK_STOP)
121 | break;
122 | if (cb_rv == WALK_SKIP) {
123 | cb_rv = WALK_CONT;
124 | break;
125 | }
126 | }
127 |
128 | for (i = 0; i < entries; i++)
129 | if (entlist[i])
130 | free(entlist[i]);
131 | free(entlist);
132 | }
133 |
134 | int check_is_directory(char *dir)
135 | {
136 | struct stat dirstat;
137 |
138 | if (stat(dir, &dirstat) != 0)
139 | return errno;
140 |
141 | if (!S_ISDIR(dirstat.st_mode))
142 | return ENOTDIR;
143 |
144 | return 0;
145 | }
146 |
147 | static void safe_mkdir(char *path)
148 | {
149 | if (mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0)
150 | fail("%s", strerror(errno));
151 | }
152 |
153 | void rec_mkdir(char *path)
154 | {
155 | char *buf;
156 | char *pos;
157 | size_t len = strlen(path);
158 |
159 | assert(path != NULL && len > 0);
160 |
161 | buf = safe_strdup(path);
162 |
163 | /* Get rid of trailing slashes */
164 | for (pos = buf + len - 1; pos > buf && *pos == '/'; --pos)
165 | *pos = '\0';
166 |
167 | pos = buf;
168 | while (pos != NULL) {
169 | int rv;
170 | char *next;
171 |
172 | /* Skip multiple slashes */
173 | for (next = pos + 1; *next == '/'; next++)
174 | ;
175 |
176 | pos = strchr(next, '/');
177 | if (pos != NULL)
178 | *pos = '\0';
179 | rv = check_is_directory(buf);
180 | if (rv != 0) {
181 | if (rv == ENOENT)
182 | safe_mkdir(buf);
183 | else
184 | fail("%s", strerror(rv));
185 | }
186 |
187 | if (pos != NULL)
188 | *pos = '/';
189 | }
190 |
191 | free(buf);
192 | }
193 |
194 | void safe_rename(const char *oldpath, const char *newpath)
195 | {
196 | char *temp;
197 |
198 | temp = safe_strdup(newpath);
199 | /* dirname() modifies its buffer! */
200 | rec_mkdir(dirname(temp));
201 | free(temp);
202 |
203 | if (rename(oldpath, newpath) != 0)
204 | fail("rename() failed: %s\n", strerror(errno));
205 | }
206 |
207 | struct norm_ctx {
208 | char *path;
209 | char *p;
210 | char *outp;
211 | };
212 |
213 | /* actually, second last, skip the whole directory */
214 | static char *last_slash(char *str, char *end)
215 | {
216 | char c = '/';
217 | int met = 0;
218 |
219 | for (; end > str; end--) {
220 | if (*end == c) {
221 | if (met)
222 | return end;
223 | else
224 | met = 1;
225 | }
226 | }
227 | return NULL;
228 | }
229 |
230 | typedef void *(*state_t)(struct norm_ctx *);
231 |
232 | static void *initial(struct norm_ctx *ctx);
233 | static void *normal(struct norm_ctx *ctx);
234 | static void *one_dot(struct norm_ctx *ctx);
235 | static void *two_dots(struct norm_ctx *ctx);
236 | static void *slash(struct norm_ctx *ctx);
237 | static void *end(struct norm_ctx *ctx);
238 |
239 | static void *initial(struct norm_ctx *ctx)
240 | {
241 | char c = *ctx->p++;
242 |
243 | switch (c) {
244 | case '\0':
245 | *ctx->outp = c;
246 | return end;
247 | case '/':
248 | *ctx->outp++ = c;
249 | return slash;
250 | case '.':
251 | return one_dot;
252 | default:
253 | *ctx->outp++ = c;
254 | }
255 | return normal;
256 | }
257 |
258 | static void *normal(struct norm_ctx *ctx)
259 | {
260 | char c = *ctx->p++;
261 |
262 | switch (c) {
263 | case '\0':
264 | *ctx->outp++ = c;
265 | return end;
266 | case '/':
267 | *ctx->outp++ = c;
268 | return slash;
269 | default:
270 | *ctx->outp++ = c;
271 | }
272 | return normal;
273 | }
274 |
275 | static void *slash(struct norm_ctx *ctx)
276 | {
277 | char c = *ctx->p++;
278 |
279 | switch (c) {
280 | case '\0':
281 | fail("Cannot normalize path %s", ctx->path);
282 | case '/':
283 | return slash;
284 | case '.':
285 | return one_dot;
286 | default:
287 | *ctx->outp++ = c;
288 | }
289 | return normal;
290 | }
291 |
292 | static void *one_dot(struct norm_ctx *ctx)
293 | {
294 | char c = *ctx->p++;
295 |
296 | switch (c) {
297 | case '\0':
298 | *--ctx->outp = c;
299 | return end;
300 | case '/':
301 | return slash;
302 | case '.':
303 | return two_dots;
304 | default:
305 | *ctx->outp++ = '.';
306 | *ctx->outp++ = c;
307 | }
308 | return normal;
309 | }
310 |
311 | static void *two_dots(struct norm_ctx *ctx)
312 | {
313 | char c = *ctx->p++;
314 | char *p;
315 |
316 | switch (c) {
317 | case '\0':
318 | p = last_slash(ctx->path, ctx->outp);
319 | if (p == NULL)
320 | p = ctx->path;
321 | *p = c;
322 | return end;
323 | case '/':
324 | p = last_slash(ctx->path, ctx->outp);
325 | if (p == NULL) {
326 | ctx->outp = ctx->path;
327 | return normal;
328 | }
329 | ctx->outp = ++p;
330 | return slash;
331 | default:
332 | *ctx->outp++ = '.';
333 | *ctx->outp++ = '.';
334 | *ctx->outp++ = c;
335 | }
336 | return normal;
337 | }
338 |
339 | static void *end(struct norm_ctx *ctx)
340 | {
341 | fail("Cannot normalize path %s", ctx->path);
342 | }
343 |
344 | char *path_normalize(char *path)
345 | {
346 | struct norm_ctx ctx = {
347 | .path = path,
348 | .p = path,
349 | .outp = path,
350 | };
351 | state_t state = initial;
352 |
353 | while (state != end)
354 | state = state(&ctx);
355 |
356 | return path;
357 | }
358 |
359 | /* Removes the two dashes at the end of the prefix */
360 | #define IS_PREFIX(s, prefix) !strncmp(s, prefix, strlen(prefix) - 2)
361 |
362 | static void split_filename(const char *filename, char **prefix,
363 | char **name, int *version)
364 | {
365 | /* GNU version of basename never modifies its argument */
366 | char *base = basename((char *)filename);
367 |
368 | version = 0;
369 |
370 | if ((sscanf(base, "%m[a-z]--%m[^.-].txt", prefix, name) != 2) &&
371 | (sscanf(base, "%m[a-z]--%m[^.-]-%i.txt",
372 | prefix, name, version) != 3))
373 | fail("Unexpected file name: %s\n", filename);
374 | }
375 |
376 | /*
377 | * Get the type of a symbol from the name of the kabi file
378 | *
379 | * It allocates the string which must be freed by the caller.
380 | */
381 | char *filenametotype(const char *filename)
382 | {
383 | char *prefix = NULL, *name = NULL, *type = NULL;
384 | int version = 0;
385 |
386 | split_filename(filename, &prefix, &name, &version);
387 |
388 | if (IS_PREFIX(prefix, TYPEDEF_FILE))
389 | type = name;
390 | else if (IS_PREFIX(prefix, STRUCT_FILE) ||
391 | IS_PREFIX(prefix, UNION_FILE) ||
392 | IS_PREFIX(prefix, ENUM_FILE))
393 | safe_asprintf(&type, "%s %s", prefix, name);
394 | else
395 | fail("Unexpected file prefix: %s\n", prefix);
396 |
397 | free(prefix);
398 | if (name != type)
399 | free(name);
400 |
401 | return type;
402 | }
403 |
404 | /*
405 | * Get the name of a symbol from the name of the kabi file
406 | *
407 | * It allocates the string which must be freed by the caller.
408 | */
409 | char *filenametosymbol(const char *filename)
410 | {
411 | char *prefix = NULL, *name = NULL;
412 | int version = 0;
413 |
414 | split_filename(filename, &prefix, &name, &version);
415 | free(prefix);
416 |
417 | return name;
418 | }
419 |
420 | struct hash *global_string_keeper;
421 |
422 | void global_string_keeper_init(void)
423 | {
424 | global_string_keeper = hash_new(1 << 20, free);
425 | }
426 |
427 | void global_string_keeper_free(void)
428 | {
429 | hash_free(global_string_keeper);
430 | }
431 |
432 | const char *global_string_get_copy(const char *string)
433 | {
434 | const char *result;
435 |
436 | if (string == NULL)
437 | return NULL;
438 |
439 | result = hash_find(global_string_keeper, string);
440 | if (result == NULL) {
441 | result = safe_strdup(string);
442 | hash_add(global_string_keeper, result, result);
443 | }
444 |
445 | return result;
446 | }
447 |
448 | const char *global_string_get_move(char *string)
449 | {
450 | const char *result;
451 |
452 | if (string == NULL)
453 | return NULL;
454 |
455 | result = hash_find(global_string_keeper, string);
456 | if (result == NULL) {
457 | result = string;
458 | hash_add(global_string_keeper, result, result);
459 | } else {
460 | free(string);
461 | }
462 |
463 | return result;
464 | }
465 |
--------------------------------------------------------------------------------
/utils.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright(C) 2016, Red Hat, Inc., Stanislav Kozina
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | #ifndef UTILS_H_
19 | #define UTILS_H_
20 |
21 | #include
22 | #include
23 | #include
24 | #include
25 | #include
26 |
27 | /*
28 | * Changes to file format that keep backward compatibility call for
29 | * incementing the minor number, changes that don't calls for
30 | * incrementing the major number.
31 | */
32 | #define _VERSION(s,j) _STR(s,j)
33 | #define _STR(s,j) _STR2(Version: s##.j\n)
34 | #define _STR2(s) #s
35 |
36 | #define FILEFMT_VERSION_MAJOR 1
37 | #define FILEFMT_VERSION_MINOR 0
38 | #define FILEFMT_VERSION_STRING \
39 | _VERSION(FILEFMT_VERSION_MAJOR,FILEFMT_VERSION_MINOR)
40 |
41 | #define fail(fmt, ...) { \
42 | fprintf(stderr, "%s():%d ", __func__, __LINE__); \
43 | fprintf(stderr, fmt, ## __VA_ARGS__); \
44 | exit(1); \
45 | }
46 |
47 | static inline void safe_asprintf(char **strp, const char *fmt, ...)
48 | {
49 | va_list arglist;
50 |
51 | va_start(arglist, fmt);
52 | if (vasprintf(strp, fmt, arglist) == -1)
53 | fail("asprintf failed: %s", strerror(errno));
54 | va_end(arglist);
55 | }
56 |
57 | static inline void *safe_zmalloc(size_t size)
58 | {
59 | void *result = malloc(size);
60 |
61 | if (result == NULL)
62 | fail("Malloc of size %zu failed", size);
63 | memset(result, 0, size);
64 | return result;
65 | }
66 |
67 | static inline void *safe_realloc(void *ptr, size_t size)
68 | {
69 | void *result = realloc(ptr, size);
70 |
71 | if (result == NULL)
72 | fail("Rellloc of size %zu failed: %s\n", size, strerror(errno));
73 | return result;
74 | }
75 |
76 | static inline void *safe_strdup(const char *s)
77 | {
78 | void *result = strdup(s);
79 |
80 | if (result == NULL)
81 | fail("strdup() of \"%s\" failed", s);
82 | return result;
83 | }
84 |
85 | static inline void *safe_strdup_or_null(const char *s)
86 | {
87 | if (s == NULL)
88 | return NULL;
89 | return safe_strdup(s);
90 | }
91 |
92 | static inline bool safe_streq(const char *s1, const char *s2)
93 | {
94 | if ((s1 == NULL) != (s2 == NULL))
95 | return false;
96 | if (s1)
97 | return !strcmp(s1, s2);
98 | return true;
99 | }
100 |
101 | static inline bool safe_strendswith(const char *s1, const char *s2)
102 | {
103 | int len1, len2;
104 |
105 | if ((s1 == NULL) != (s2 == NULL))
106 | return false;
107 |
108 | if (!s1)
109 | return true;
110 |
111 | len1 = strlen(s1);
112 | len2 = strlen(s2);
113 |
114 | if ((len1 == 0) || (len2 == 0))
115 | return false;
116 |
117 | if (len2 > len1)
118 | return false;
119 |
120 | return strcmp(s1 + len1 - len2, s2) == 0;
121 | }
122 |
123 | static inline ssize_t safe_getline(char **lineptr, size_t *n, FILE *stream)
124 | {
125 | ssize_t ret = getline(lineptr, n, stream);
126 |
127 | if ((ret == -1) && (errno != ENOENT))
128 | fail("getline failed: %s\n", strerror(errno));
129 |
130 | return ret;
131 | }
132 |
133 | static inline FILE *safe_fopen(char *filename)
134 | {
135 | FILE *file;
136 |
137 | file = fopen(filename, "r");
138 | if (!file)
139 | fail("Failed to open kABI file: %s\n", filename);
140 |
141 | return file;
142 | }
143 |
144 | typedef enum {
145 | WALK_CONT,
146 | WALK_STOP,
147 | WALK_SKIP,
148 | } walk_rv_t;
149 |
150 | extern void walk_dir(char *, bool, walk_rv_t (*)(char *, void *), void *);
151 | extern int check_is_directory(char *);
152 | extern void rec_mkdir(char *);
153 | extern void safe_rename(const char *, const char *);
154 | extern char *path_normalize(char *);
155 | extern char *filenametotype(const char *);
156 | extern char *filenametosymbol(const char *);
157 |
158 | extern void global_string_keeper_init(void);
159 | extern void global_string_keeper_free(void);
160 | extern const char *global_string_get_copy(const char *string);
161 | extern const char *global_string_get_move(char *string);
162 |
163 | #endif /* UTILS_H */
164 |
--------------------------------------------------------------------------------