pax_global_header00006660000000000000000000000064147504662040014522gustar00rootroot0000000000000052 comment=c61d07b55c99a4a1c386554a62689cf67fadad44 djstub-0.6/000077500000000000000000000000001475046620400126625ustar00rootroot00000000000000djstub-0.6/LICENSE000066400000000000000000001045151475046620400136750ustar00rootroot00000000000000 GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . djstub-0.6/Makefile000066400000000000000000000016141475046620400143240ustar00rootroot00000000000000PROG = djstubify prefix ?= /usr/local BINDIR ?= $(prefix)/bin CFLAGS ?= -Wall -Og -g # set to mini or full STYPE = mini STUB = $(STYPE)stub.exe STUB_S = $(STYPE)stub.S CPPFLAGS += \ -D_binary_stub_exe_start=_binary_$(STYPE)stub_exe_start \ -D_binary_stub_exe_end=_binary_$(STYPE)stub_exe_end \ -D_binary_stub_exe_size=_binary_$(STYPE)stub_exe_size all: $(PROG) .PHONY: $(STYPE) $(STUB): $(STYPE) $(MAKE) -C $< binstub.o: $(STUB_S) $(STUB) $(CC) -c -o $@ $< $(PROG): stubify.o binstub.o $(CC) $(LDFLAGS) -o $@ $^ install: install -d $(DESTDIR)$(BINDIR) install -m 0755 $(PROG) $(DESTDIR)$(BINDIR) install -m 0755 djstrip $(DESTDIR)$(BINDIR) install -m 0755 djlink $(DESTDIR)$(BINDIR) uninstall: $(RM) $(DESTDIR)$(BINDIR)/$(PROG) $(RM) $(DESTDIR)$(BINDIR)/djstrip $(RM) $(DESTDIR)$(BINDIR)/djlink deb: debuild -i -us -uc -b clean: $(MAKE) -C $(STYPE) clean rm -f *.o $(STUB) $(PROG) djstub-0.6/README.md000066400000000000000000000031651475046620400141460ustar00rootroot00000000000000# djstub djstub project provides a dj64-compatible and go32-compatible stubs that support COFF, PE and ELF payloads. Its primary target is [dj64dev](https://github.com/stsp/dj64dev) suite, but it can also work with djgpp-built executables. ## what it does? - It can stubify or re-stub COFF and PE executables. See `djstubify`. - It can link the ELF binaries into a dj64 executables. See `djlink`. - It can strip the debug info from dj64 executables. See `djstrip`. ## what stubs does it provide? There are 2 stubs available: [full](https://github.com/stsp/djstub/tree/main/full), which has all the loaders within, and [mini](https://github.com/stsp/djstub/tree/main/mini), which relies on a `"DJ64STUB"` DPMI extension, which is supposed to have the same loaders inside DPMI host. Full stub requires [gcc-ia16](https://gitlab.com/tkchia/gcc-ia16) to build, and is therefore currently not maintained. Mini stub uses [SmallerC](https://github.com/alexfru/SmallerC) and is actively maintained. If you want to use this project outside of dosemu2 (which means djgpp + bare-metal DOS or windows), you may need full stub. Otherwise, mini stub is enough. ## usage examples ``` $ djstubify -i comcom64.exe dj64 file format Overlay 0 (i386/ELF DOS payload) at 23368, size 30548 Overlay 1 (x86_64/ELF host payload) at 53916, size 87048 Overlay 2 (x86_64/ELF host debug info) at 140964, size 174936 Overlay name: comcom64.exe Stub version: 4 Stub flags: 0x0b07 ``` ``` $ djlink -d dosemu_hello.exe.dbg libtmp.so -n hello.exe -o hello.exe tmp.elf ``` These examples are [documented](https://github.com/stsp/dj64dev/blob/master/README.md) in a dj64dev project. djstub-0.6/debian/000077500000000000000000000000001475046620400141045ustar00rootroot00000000000000djstub-0.6/debian/changelog000066400000000000000000000007741475046620400157660ustar00rootroot00000000000000djstub (0.6-1) noble; urgency=low * 0.6 -- Stas Sergeev Tue, 4 Feb 2025 22:40:00 +0300 djstub (0.3-1) noble; urgency=low * 0.3 -- Stas Sergeev Tue, 31 Dec 2024 13:00:00 +0300 djstub (0.2-1) noble; urgency=low * 0.2 -- Stas Sergeev Mon, 30 Sep 2024 12:00:00 +0300 djstub (0.1-1) kinetic; urgency=low * initial release -- Stas Sergeev Mon, 19 Dec 2022 20:00:00 +0500 djstub-0.6/debian/compat000066400000000000000000000000031475046620400153030ustar00rootroot0000000000000010 djstub-0.6/debian/control000066400000000000000000000007371475046620400155160ustar00rootroot00000000000000Source: djstub Section: contrib/otherosfs Priority: optional Maintainer: Stas Sergeev Standards-Version: 3.9.7 Build-Depends: smallerc, make, gcc, binutils, debhelper (>= 9~) Homepage: https://github.com/stsp/djstub Package: djstub Architecture: any Depends: ${misc:Depends}, ${shlibs:Depends}, bash, binutils Description: advanced djgpp-compatible stub subsystem go32-compatible stub, with support of COFF, PE and ELF formats. djstub-0.6/debian/copyright000066400000000000000000000014751475046620400160460ustar00rootroot00000000000000go32-compatible COFF stub. Copyright (C) 2022, stsp This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . On Debian systems, the complete text of the GNU General Public License version 3 can be found in `/usr/share/common-licenses/GPL-3'. djstub-0.6/debian/rules000077500000000000000000000001341475046620400151620ustar00rootroot00000000000000#!/usr/bin/make -f %: dh $@ override_dh_auto_install: dh_auto_install $@ -- prefix=/usr djstub-0.6/debian/source/000077500000000000000000000000001475046620400154045ustar00rootroot00000000000000djstub-0.6/debian/source/format000066400000000000000000000000141475046620400166120ustar00rootroot000000000000003.0 (quilt) djstub-0.6/djlink000077500000000000000000000025601475046620400140660ustar00rootroot00000000000000#!/usr/bin/env bash if [ -z "$OBJCOPY" ]; then OBJCOPY=objcopy fi if [ -z "$STRIP" ]; then STRIP=strip fi if [ "$#" -lt 1 ]; then echo "Usage: djlink [-d ] []" exit 0 fi while getopts "d:v" opt; do case "$opt" in d) D="$OPTARG" ;; v) djstubify -v exit $? ;; \?) exit 1 ;; esac done shift $(($OPTIND-1)) SO=$1 shift if [ ! -f "$SO" ]; then echo "$0: file $SO missing" exit 1 fi if [ -n "$D" ]; then if [ -z "`readelf -S $SO | grep .debug_info`" ]; then echo "$0: -d is specified but $SO is stripped" exit 1 fi DBG="/tmp/$D" $OBJCOPY --only-keep-debug $SO $DBG TMP=$(mktemp) $STRIP -o $TMP $SO $OBJCOPY --add-gnu-debuglink=$DBG $TMP OFFS=`LC_ALL=C readelf -S $TMP | grep .gnu_debuglink | \ tr -s '[:space:]' | cut -d " " -f 6 | sed -E -e 's/0*/0x/'` SO=$TMP DOPT="-n $OFFS -l $DBG" fi ID="`LC_ALL=C readelf -S $SO | grep .dj64startup`" if [ -n "$ID" ]; then SZ=`LC_ALL=C readelf -S $SO | grep -A 1 .dj64startup | tail -n 1 | \ tr -s '[:space:]' | cut -d " " -f 2 | sed -E -e 's/0*/0x/'` OFF=`echo "$ID" | tr -s '[:space:]' | cut -d " " -f 6 | sed -E -e 's/0*/0x/'` DOPT="$DOPT -S $SZ -O $OFF -f 0x8000" fi CMD="djstubify -g $* -l $SO $DOPT" #echo "$CMD" $CMD [ -z "$TMP" ] || rm "$TMP" if [ -n "$DBG" -a -f "$DBG" ]; then rm $DBG fi djstub-0.6/djstrip000077500000000000000000000002651475046620400142720ustar00rootroot00000000000000#!/usr/bin/env bash if [ -z "$1" ]; then echo "Usage: djstrip " exit 0 fi O2=`djstubify -i $1 | grep "debug info"` if [ -z "$O2" ]; then exit 1 fi djstubify -s $1 djstub-0.6/djstub.spec.rpkg000066400000000000000000000013061475046620400157730ustar00rootroot00000000000000# # spec file for package djstub # Name: {{{ git_dir_name }}} Version: {{{ git_dir_version }}} Release: 1%{?dist} Summary: advanced djgpp-compatible stub Group: Development/Tools License: GPLv3+ URL: https://github.com/stsp/djstub VCS: {{{ git_dir_vcs }}} Source0: {{{ git_dir_archive }}} BuildRequires: make BuildRequires: gcc BuildRequires: binutils BuildRequires: smallerc Requires: bash Requires: binutils %description djstub is a go32-compatible stub, with support of COFF, PE and ELF formats. Can be used with djgpp and dj64 tool-chains. %prep {{{ git_dir_setup_macro }}} %build make %check %install make install DESTDIR=%{buildroot} prefix=%{_prefix} %files %defattr(-,root,root) %{_bindir}/* djstub-0.6/full/000077500000000000000000000000001475046620400136245ustar00rootroot00000000000000djstub-0.6/full/Makefile000066400000000000000000000006351475046620400152700ustar00rootroot00000000000000TOP = .. TGT = $(TOP)/fullstub.exe CC = ia16-elf-gcc CFLAGS = -mcmodel=small -mdosx32 -Os -Wall LDFLAGS = -mcmodel=small -mdosx32 -li86 CFILES = stub.c coff.c elf.c util.c OBJECTS = $(CFILES:.c=.o) all: $(TGT) $(OBJECTS): $(CFILES) $(wildcard *.h) ifneq ($(shell $(CC) --version 2>/dev/null),) $(TGT): $(OBJECTS) $(CC) $^ $(LDFLAGS) -o _$@ lfanew -o $@ _$@ rm _$@ else $(error ia16-gcc not installed) endif djstub-0.6/full/coff.c000066400000000000000000000122531475046620400147100ustar00rootroot00000000000000/* * COFF loader. * Copyright (C) 2023, stsp2@yandex.ru * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ #include #include #include #include #include #include "util.h" #include "coff.h" #define STUB_DEBUG 0 #if STUB_DEBUG #define stub_debug(...) printf(__VA_ARGS__) #else #define stub_debug(...) #endif struct coff_header { unsigned short f_magic; /* Magic number */ unsigned short f_nscns; /* Number of Sections */ int32_t f_timdat; /* Time & date stamp */ int32_t f_symptr; /* File pointer to Symbol Table */ int32_t f_nsyms; /* Number of Symbols */ unsigned short f_opthdr; /* sizeof(Optional Header) */ unsigned short f_flags; /* Flags */ }; struct opt_header { unsigned short magic; /* Magic Number */ unsigned short vstamp; /* Version stamp */ uint32_t tsize; /* Text size in bytes */ uint32_t dsize; /* Initialised data size */ uint32_t bsize; /* Uninitialised data size */ uint32_t entry; /* Entry point */ uint32_t text_start; /* Base of Text used for this file */ uint32_t data_start; /* Base of Data used for this file */ }; struct scn_header { char s_name[8]; /* Section Name */ int32_t s_paddr; /* Physical Address */ int32_t s_vaddr; /* Virtual Address */ int32_t s_size; /* Section Size in Bytes */ int32_t s_scnptr; /* File offset to the Section data */ int32_t s_relptr; /* File offset to the Relocation table for this Section */ int32_t s_lnnoptr; /* File offset to the Line Number table for this Section */ unsigned short s_nreloc; /* Number of Relocation table entries */ unsigned short s_nlnno; /* Number of Line Number table entries */ int32_t s_flags; /* Flags for this section */ }; enum { SCT_TEXT, SCT_DATA, SCT_BSS, SCT_MAX }; static struct scn_header scns[SCT_MAX]; struct coff_h { uint32_t length; uint32_t entry; }; static void read_section(char __far *buf, int ifile, long coffset, int sc) { long bytes; lseek(ifile, coffset + scns[sc].s_scnptr, SEEK_SET); bytes = _long_read(ifile, buf, scns[sc].s_vaddr, scns[sc].s_size); stub_debug("read returned %li\n", bytes); if (bytes != scns[sc].s_size) { fprintf(stderr, "err reading %li bytes, got %li\n", scns[sc].s_size, bytes); _exit(EXIT_FAILURE); } } static void *read_coff_headers(int ifile) { struct coff_header chdr; struct opt_header ohdr; struct coff_h *h; int rc; rc = read(ifile, &chdr, sizeof(chdr)); /* get the COFF header */ if (rc != sizeof(chdr)) { fprintf(stderr, "bad COFF header\n"); return NULL; } if (chdr.f_opthdr < sizeof(ohdr)) { fprintf(stderr, "opt header size mismatch: %i %zi\n", chdr.f_opthdr, sizeof(ohdr)); return NULL; } rc = read(ifile, &ohdr, sizeof(ohdr)); /* get the COFF opt header */ if (rc != sizeof(ohdr)) { fprintf(stderr, "bad COFF opt header\n"); return NULL; } if (chdr.f_opthdr > sizeof(ohdr)) lseek(ifile, chdr.f_opthdr - sizeof(ohdr), SEEK_CUR); rc = read(ifile, scns, sizeof(scns[0]) * SCT_MAX); if (rc != sizeof(scns[0]) * SCT_MAX) { fprintf(stderr, "failed to read section headers\n"); return NULL; } #if STUB_DEBUG for (int i = 0; i < SCT_MAX; i++) { struct scn_header *h = &scns[i]; stub_debug("Section %s pa 0x%lx va 0x%lx size 0x%lx foffs 0x%lx\n", h->s_name, h->s_paddr, h->s_vaddr, h->s_size, h->s_scnptr); } #endif h = malloc(sizeof(*h)); assert(h); h->entry = ohdr.entry; h->length = scns[SCT_BSS].s_vaddr + scns[SCT_BSS].s_size; return h; } static uint32_t get_coff_va(void *handle) { return 0; // ??? } static uint32_t get_coff_length(void *handle) { struct coff_h *h = handle; return h->length; } static uint32_t get_coff_entry(void *handle) { struct coff_h *h = handle; return h->entry; } static void read_coff_sections(void *handle, char __far *ptr, int ifile, uint32_t offset) { struct coff_h *h = handle; read_section(ptr, ifile, offset, SCT_TEXT); read_section(ptr, ifile, offset, SCT_DATA); farmemset(ptr, scns[SCT_BSS].s_vaddr, 0, scns[SCT_BSS].s_size + (scns[SCT_BSS].s_size & 1)); free(h); } struct ldops coff_ops = { read_coff_headers, get_coff_va, get_coff_length, get_coff_entry, read_coff_sections, }; djstub-0.6/full/coff.h000066400000000000000000000001301475046620400147040ustar00rootroot00000000000000#ifndef COFF_H #define COFF_H #include "stub.h" extern struct ldops coff_ops; #endif djstub-0.6/full/elf.c000066400000000000000000000102661475046620400145430ustar00rootroot00000000000000/* * ELF loader. * Copyright (C) 2023, @stsp * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ #include #include #include #include #include #include #include "util.h" #include "stub.h" #include "elf.h" #include "elfp.h" #define STUB_DEBUG 0 #if STUB_DEBUG #define stub_debug(...) printf(__VA_ARGS__) #else #define stub_debug(...) #endif struct elf_h { uint32_t va; uint32_t length; uint32_t entry; int phnum; Elf32_Phdr phdr[0]; }; static void *read_elf_headers(int ifile) { Elf32_Ehdr ehdr; struct elf_h *h; int i, rc; long beg = 0, end = 0; rc = read(ifile, &ehdr, sizeof(ehdr)); /* get the ELF header */ if (rc != sizeof(ehdr)) { fprintf(stderr, "cant read ELF header\n"); return NULL; } if (memcmp(&ehdr.e_ident, ELFMAG, SELFMAG) || ehdr.e_ehsize != sizeof(ehdr) || ehdr.e_phentsize != sizeof(Elf32_Phdr)) { fprintf(stderr, "bad ELF header\n"); return NULL; } if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) { fprintf(stderr, "bad ELF class %i\n", ehdr.e_ident[EI_CLASS]); return NULL; } if (ehdr.e_phoff > sizeof(ehdr)) lseek(ifile, ehdr.e_phoff - sizeof(ehdr), SEEK_CUR); h = malloc(sizeof(*h) + sizeof(Elf32_Phdr) * ehdr.e_phnum); assert(h); rc = read(ifile, h->phdr, sizeof(Elf32_Phdr) * ehdr.e_phnum); if (rc != sizeof(Elf32_Phdr) * ehdr.e_phnum) { fprintf(stderr, "can't read phdr\n"); return NULL; } h->phnum = ehdr.e_phnum; for (i = 0; i < ehdr.e_phnum; i++) { Elf32_Phdr *phdr = &h->phdr[i]; if (phdr->p_type != PT_LOAD) continue; if (phdr->p_align != 4096) { fprintf(stderr, "unsupported ELF alignment %li\n", phdr->p_align); return NULL; } if (phdr->p_vaddr < beg || !beg) beg = phdr->p_vaddr; if (phdr->p_vaddr + phdr->p_memsz > end) end = phdr->p_vaddr + phdr->p_memsz; #if STUB_DEBUG stub_debug("PHDR pa 0x%lx va 0x%lx size 0x%lx foffs 0x%lx\n", phdr->p_paddr, phdr->p_vaddr, phdr->p_filesz, phdr->p_offset); #endif } h->entry = ehdr.e_entry; h->va = beg; h->length = end - beg; return h; } static uint32_t get_elf_length(void *handle) { struct elf_h *h = handle; return h->length; } static uint32_t get_elf_entry(void *handle) { struct elf_h *h = handle; return h->entry; } static uint32_t get_elf_va(void *handle) { struct elf_h *h = handle; return h->va; } static void read_elf_sections(void *handle, char __far *ptr, int ifile, uint32_t offset) { struct elf_h *h = handle; int i; for (i = 0; i < h->phnum; i++) { Elf32_Phdr *phdr = &h->phdr[i]; long bytes; if (phdr->p_type != PT_LOAD) continue; lseek(ifile, offset + phdr->p_offset, SEEK_SET); bytes = _long_read(ifile, ptr, phdr->p_vaddr, phdr->p_filesz); stub_debug("read returned %li\n", bytes); if (bytes != phdr->p_filesz) { fprintf(stderr, "err reading %li bytes, got %li\n", phdr->p_filesz, bytes); _exit(EXIT_FAILURE); } if (phdr->p_memsz > phdr->p_filesz) { uint32_t len = phdr->p_memsz - phdr->p_filesz; len += len & 1; // word-align farmemset(ptr, phdr->p_vaddr + phdr->p_filesz, 0, len); } } free(h); } struct ldops elf_ops = { read_elf_headers, get_elf_va, get_elf_length, get_elf_entry, read_elf_sections, }; djstub-0.6/full/elf.h000066400000000000000000000735061475046620400145560ustar00rootroot00000000000000/* This file defines standard ELF types, structures, and macros. Copyright (C) 1995-2023 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. The GNU C Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with the GNU C Library; if not, see . */ #ifndef _ELF_H #define _ELF_H 1 /* Standard ELF types. */ #include /* Type for a 16-bit quantity. */ typedef uint16_t Elf32_Half; typedef uint16_t Elf64_Half; /* Types for signed and unsigned 32-bit quantities. */ typedef uint32_t Elf32_Word; typedef int32_t Elf32_Sword; typedef uint32_t Elf64_Word; typedef int32_t Elf64_Sword; /* Types for signed and unsigned 64-bit quantities. */ typedef uint64_t Elf32_Xword; typedef int64_t Elf32_Sxword; typedef uint64_t Elf64_Xword; typedef int64_t Elf64_Sxword; /* Type of addresses. */ typedef uint32_t Elf32_Addr; typedef uint64_t Elf64_Addr; /* Type of file offsets. */ typedef uint32_t Elf32_Off; typedef uint64_t Elf64_Off; /* Type for section indices, which are 16-bit quantities. */ typedef uint16_t Elf32_Section; typedef uint16_t Elf64_Section; /* Type for version symbol information. */ typedef Elf32_Half Elf32_Versym; typedef Elf64_Half Elf64_Versym; /* The ELF file header. This appears at the start of every ELF file. */ #define EI_NIDENT (16) typedef struct { unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ Elf32_Half e_type; /* Object file type */ Elf32_Half e_machine; /* Architecture */ Elf32_Word e_version; /* Object file version */ Elf32_Addr e_entry; /* Entry point virtual address */ Elf32_Off e_phoff; /* Program header table file offset */ Elf32_Off e_shoff; /* Section header table file offset */ Elf32_Word e_flags; /* Processor-specific flags */ Elf32_Half e_ehsize; /* ELF header size in bytes */ Elf32_Half e_phentsize; /* Program header table entry size */ Elf32_Half e_phnum; /* Program header table entry count */ Elf32_Half e_shentsize; /* Section header table entry size */ Elf32_Half e_shnum; /* Section header table entry count */ Elf32_Half e_shstrndx; /* Section header string table index */ } Elf32_Ehdr; typedef struct { unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ Elf64_Half e_type; /* Object file type */ Elf64_Half e_machine; /* Architecture */ Elf64_Word e_version; /* Object file version */ Elf64_Addr e_entry; /* Entry point virtual address */ Elf64_Off e_phoff; /* Program header table file offset */ Elf64_Off e_shoff; /* Section header table file offset */ Elf64_Word e_flags; /* Processor-specific flags */ Elf64_Half e_ehsize; /* ELF header size in bytes */ Elf64_Half e_phentsize; /* Program header table entry size */ Elf64_Half e_phnum; /* Program header table entry count */ Elf64_Half e_shentsize; /* Section header table entry size */ Elf64_Half e_shnum; /* Section header table entry count */ Elf64_Half e_shstrndx; /* Section header string table index */ } Elf64_Ehdr; /* Fields in the e_ident array. The EI_* macros are indices into the array. The macros under each EI_* macro are the values the byte may have. */ #define EI_MAG0 0 /* File identification byte 0 index */ #define ELFMAG0 0x7f /* Magic number byte 0 */ #define EI_MAG1 1 /* File identification byte 1 index */ #define ELFMAG1 'E' /* Magic number byte 1 */ #define EI_MAG2 2 /* File identification byte 2 index */ #define ELFMAG2 'L' /* Magic number byte 2 */ #define EI_MAG3 3 /* File identification byte 3 index */ #define ELFMAG3 'F' /* Magic number byte 3 */ /* Conglomeration of the identification bytes, for easy testing as a word. */ #define ELFMAG "\177ELF" #define SELFMAG 4 #define EI_CLASS 4 /* File class byte index */ #define ELFCLASSNONE 0 /* Invalid class */ #define ELFCLASS32 1 /* 32-bit objects */ #define ELFCLASS64 2 /* 64-bit objects */ #define ELFCLASSNUM 3 #define EI_DATA 5 /* Data encoding byte index */ #define ELFDATANONE 0 /* Invalid data encoding */ #define ELFDATA2LSB 1 /* 2's complement, little endian */ #define ELFDATA2MSB 2 /* 2's complement, big endian */ #define ELFDATANUM 3 #define EI_VERSION 6 /* File version byte index */ /* Value must be EV_CURRENT */ #define EI_OSABI 7 /* OS ABI identification */ #define ELFOSABI_NONE 0 /* UNIX System V ABI */ #define ELFOSABI_SYSV 0 /* Alias. */ #define ELFOSABI_HPUX 1 /* HP-UX */ #define ELFOSABI_NETBSD 2 /* NetBSD. */ #define ELFOSABI_GNU 3 /* Object uses GNU ELF extensions. */ #define ELFOSABI_LINUX ELFOSABI_GNU /* Compatibility alias. */ #define ELFOSABI_SOLARIS 6 /* Sun Solaris. */ #define ELFOSABI_AIX 7 /* IBM AIX. */ #define ELFOSABI_IRIX 8 /* SGI Irix. */ #define ELFOSABI_FREEBSD 9 /* FreeBSD. */ #define ELFOSABI_TRU64 10 /* Compaq TRU64 UNIX. */ #define ELFOSABI_MODESTO 11 /* Novell Modesto. */ #define ELFOSABI_OPENBSD 12 /* OpenBSD. */ #define ELFOSABI_ARM_AEABI 64 /* ARM EABI */ #define ELFOSABI_ARM 97 /* ARM */ #define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */ #define EI_ABIVERSION 8 /* ABI version */ #define EI_PAD 9 /* Byte index of padding bytes */ /* Legal values for e_type (object file type). */ #define ET_NONE 0 /* No file type */ #define ET_REL 1 /* Relocatable file */ #define ET_EXEC 2 /* Executable file */ #define ET_DYN 3 /* Shared object file */ #define ET_CORE 4 /* Core file */ #define ET_NUM 5 /* Number of defined types */ #define ET_LOOS 0xfe00 /* OS-specific range start */ #define ET_HIOS 0xfeff /* OS-specific range end */ #define ET_LOPROC 0xff00 /* Processor-specific range start */ #define ET_HIPROC 0xffff /* Processor-specific range end */ /* Legal values for e_machine (architecture). */ #define EM_NONE 0 /* No machine */ #define EM_M32 1 /* AT&T WE 32100 */ #define EM_SPARC 2 /* SUN SPARC */ #define EM_386 3 /* Intel 80386 */ #define EM_68K 4 /* Motorola m68k family */ #define EM_88K 5 /* Motorola m88k family */ #define EM_IAMCU 6 /* Intel MCU */ #define EM_860 7 /* Intel 80860 */ #define EM_MIPS 8 /* MIPS R3000 big-endian */ #define EM_S370 9 /* IBM System/370 */ #define EM_MIPS_RS3_LE 10 /* MIPS R3000 little-endian */ /* reserved 11-14 */ #define EM_PARISC 15 /* HPPA */ /* reserved 16 */ #define EM_VPP500 17 /* Fujitsu VPP500 */ #define EM_SPARC32PLUS 18 /* Sun's "v8plus" */ #define EM_960 19 /* Intel 80960 */ #define EM_PPC 20 /* PowerPC */ #define EM_PPC64 21 /* PowerPC 64-bit */ #define EM_S390 22 /* IBM S390 */ #define EM_SPU 23 /* IBM SPU/SPC */ /* reserved 24-35 */ #define EM_V800 36 /* NEC V800 series */ #define EM_FR20 37 /* Fujitsu FR20 */ #define EM_RH32 38 /* TRW RH-32 */ #define EM_RCE 39 /* Motorola RCE */ #define EM_ARM 40 /* ARM */ #define EM_FAKE_ALPHA 41 /* Digital Alpha */ #define EM_SH 42 /* Hitachi SH */ #define EM_SPARCV9 43 /* SPARC v9 64-bit */ #define EM_TRICORE 44 /* Siemens Tricore */ #define EM_ARC 45 /* Argonaut RISC Core */ #define EM_H8_300 46 /* Hitachi H8/300 */ #define EM_H8_300H 47 /* Hitachi H8/300H */ #define EM_H8S 48 /* Hitachi H8S */ #define EM_H8_500 49 /* Hitachi H8/500 */ #define EM_IA_64 50 /* Intel Merced */ #define EM_MIPS_X 51 /* Stanford MIPS-X */ #define EM_COLDFIRE 52 /* Motorola Coldfire */ #define EM_68HC12 53 /* Motorola M68HC12 */ #define EM_MMA 54 /* Fujitsu MMA Multimedia Accelerator */ #define EM_PCP 55 /* Siemens PCP */ #define EM_NCPU 56 /* Sony nCPU embedded RISC */ #define EM_NDR1 57 /* Denso NDR1 microprocessor */ #define EM_STARCORE 58 /* Motorola Start*Core processor */ #define EM_ME16 59 /* Toyota ME16 processor */ #define EM_ST100 60 /* STMicroelectronic ST100 processor */ #define EM_TINYJ 61 /* Advanced Logic Corp. Tinyj emb.fam */ #define EM_X86_64 62 /* AMD x86-64 architecture */ #define EM_PDSP 63 /* Sony DSP Processor */ #define EM_PDP10 64 /* Digital PDP-10 */ #define EM_PDP11 65 /* Digital PDP-11 */ #define EM_FX66 66 /* Siemens FX66 microcontroller */ #define EM_ST9PLUS 67 /* STMicroelectronics ST9+ 8/16 mc */ #define EM_ST7 68 /* STmicroelectronics ST7 8 bit mc */ #define EM_68HC16 69 /* Motorola MC68HC16 microcontroller */ #define EM_68HC11 70 /* Motorola MC68HC11 microcontroller */ #define EM_68HC08 71 /* Motorola MC68HC08 microcontroller */ #define EM_68HC05 72 /* Motorola MC68HC05 microcontroller */ #define EM_SVX 73 /* Silicon Graphics SVx */ #define EM_ST19 74 /* STMicroelectronics ST19 8 bit mc */ #define EM_VAX 75 /* Digital VAX */ #define EM_CRIS 76 /* Axis Communications 32-bit emb.proc */ #define EM_JAVELIN 77 /* Infineon Technologies 32-bit emb.proc */ #define EM_FIREPATH 78 /* Element 14 64-bit DSP Processor */ #define EM_ZSP 79 /* LSI Logic 16-bit DSP Processor */ #define EM_MMIX 80 /* Donald Knuth's educational 64-bit proc */ #define EM_HUANY 81 /* Harvard University machine-independent object files */ #define EM_PRISM 82 /* SiTera Prism */ #define EM_AVR 83 /* Atmel AVR 8-bit microcontroller */ #define EM_FR30 84 /* Fujitsu FR30 */ #define EM_D10V 85 /* Mitsubishi D10V */ #define EM_D30V 86 /* Mitsubishi D30V */ #define EM_V850 87 /* NEC v850 */ #define EM_M32R 88 /* Mitsubishi M32R */ #define EM_MN10300 89 /* Matsushita MN10300 */ #define EM_MN10200 90 /* Matsushita MN10200 */ #define EM_PJ 91 /* picoJava */ #define EM_OPENRISC 92 /* OpenRISC 32-bit embedded processor */ #define EM_ARC_COMPACT 93 /* ARC International ARCompact */ #define EM_XTENSA 94 /* Tensilica Xtensa Architecture */ #define EM_VIDEOCORE 95 /* Alphamosaic VideoCore */ #define EM_TMM_GPP 96 /* Thompson Multimedia General Purpose Proc */ #define EM_NS32K 97 /* National Semi. 32000 */ #define EM_TPC 98 /* Tenor Network TPC */ #define EM_SNP1K 99 /* Trebia SNP 1000 */ #define EM_ST200 100 /* STMicroelectronics ST200 */ #define EM_IP2K 101 /* Ubicom IP2xxx */ #define EM_MAX 102 /* MAX processor */ #define EM_CR 103 /* National Semi. CompactRISC */ #define EM_F2MC16 104 /* Fujitsu F2MC16 */ #define EM_MSP430 105 /* Texas Instruments msp430 */ #define EM_BLACKFIN 106 /* Analog Devices Blackfin DSP */ #define EM_SE_C33 107 /* Seiko Epson S1C33 family */ #define EM_SEP 108 /* Sharp embedded microprocessor */ #define EM_ARCA 109 /* Arca RISC */ #define EM_UNICORE 110 /* PKU-Unity & MPRC Peking Uni. mc series */ #define EM_EXCESS 111 /* eXcess configurable cpu */ #define EM_DXP 112 /* Icera Semi. Deep Execution Processor */ #define EM_ALTERA_NIOS2 113 /* Altera Nios II */ #define EM_CRX 114 /* National Semi. CompactRISC CRX */ #define EM_XGATE 115 /* Motorola XGATE */ #define EM_C166 116 /* Infineon C16x/XC16x */ #define EM_M16C 117 /* Renesas M16C */ #define EM_DSPIC30F 118 /* Microchip Technology dsPIC30F */ #define EM_CE 119 /* Freescale Communication Engine RISC */ #define EM_M32C 120 /* Renesas M32C */ /* reserved 121-130 */ #define EM_TSK3000 131 /* Altium TSK3000 */ #define EM_RS08 132 /* Freescale RS08 */ #define EM_SHARC 133 /* Analog Devices SHARC family */ #define EM_ECOG2 134 /* Cyan Technology eCOG2 */ #define EM_SCORE7 135 /* Sunplus S+core7 RISC */ #define EM_DSP24 136 /* New Japan Radio (NJR) 24-bit DSP */ #define EM_VIDEOCORE3 137 /* Broadcom VideoCore III */ #define EM_LATTICEMICO32 138 /* RISC for Lattice FPGA */ #define EM_SE_C17 139 /* Seiko Epson C17 */ #define EM_TI_C6000 140 /* Texas Instruments TMS320C6000 DSP */ #define EM_TI_C2000 141 /* Texas Instruments TMS320C2000 DSP */ #define EM_TI_C5500 142 /* Texas Instruments TMS320C55x DSP */ #define EM_TI_ARP32 143 /* Texas Instruments App. Specific RISC */ #define EM_TI_PRU 144 /* Texas Instruments Prog. Realtime Unit */ /* reserved 145-159 */ #define EM_MMDSP_PLUS 160 /* STMicroelectronics 64bit VLIW DSP */ #define EM_CYPRESS_M8C 161 /* Cypress M8C */ #define EM_R32C 162 /* Renesas R32C */ #define EM_TRIMEDIA 163 /* NXP Semi. TriMedia */ #define EM_QDSP6 164 /* QUALCOMM DSP6 */ #define EM_8051 165 /* Intel 8051 and variants */ #define EM_STXP7X 166 /* STMicroelectronics STxP7x */ #define EM_NDS32 167 /* Andes Tech. compact code emb. RISC */ #define EM_ECOG1X 168 /* Cyan Technology eCOG1X */ #define EM_MAXQ30 169 /* Dallas Semi. MAXQ30 mc */ #define EM_XIMO16 170 /* New Japan Radio (NJR) 16-bit DSP */ #define EM_MANIK 171 /* M2000 Reconfigurable RISC */ #define EM_CRAYNV2 172 /* Cray NV2 vector architecture */ #define EM_RX 173 /* Renesas RX */ #define EM_METAG 174 /* Imagination Tech. META */ #define EM_MCST_ELBRUS 175 /* MCST Elbrus */ #define EM_ECOG16 176 /* Cyan Technology eCOG16 */ #define EM_CR16 177 /* National Semi. CompactRISC CR16 */ #define EM_ETPU 178 /* Freescale Extended Time Processing Unit */ #define EM_SLE9X 179 /* Infineon Tech. SLE9X */ #define EM_L10M 180 /* Intel L10M */ #define EM_K10M 181 /* Intel K10M */ /* reserved 182 */ #define EM_AARCH64 183 /* ARM AARCH64 */ /* reserved 184 */ #define EM_AVR32 185 /* Amtel 32-bit microprocessor */ #define EM_STM8 186 /* STMicroelectronics STM8 */ #define EM_TILE64 187 /* Tilera TILE64 */ #define EM_TILEPRO 188 /* Tilera TILEPro */ #define EM_MICROBLAZE 189 /* Xilinx MicroBlaze */ #define EM_CUDA 190 /* NVIDIA CUDA */ #define EM_TILEGX 191 /* Tilera TILE-Gx */ #define EM_CLOUDSHIELD 192 /* CloudShield */ #define EM_COREA_1ST 193 /* KIPO-KAIST Core-A 1st gen. */ #define EM_COREA_2ND 194 /* KIPO-KAIST Core-A 2nd gen. */ #define EM_ARCV2 195 /* Synopsys ARCv2 ISA. */ #define EM_OPEN8 196 /* Open8 RISC */ #define EM_RL78 197 /* Renesas RL78 */ #define EM_VIDEOCORE5 198 /* Broadcom VideoCore V */ #define EM_78KOR 199 /* Renesas 78KOR */ #define EM_56800EX 200 /* Freescale 56800EX DSC */ #define EM_BA1 201 /* Beyond BA1 */ #define EM_BA2 202 /* Beyond BA2 */ #define EM_XCORE 203 /* XMOS xCORE */ #define EM_MCHP_PIC 204 /* Microchip 8-bit PIC(r) */ #define EM_INTELGT 205 /* Intel Graphics Technology */ /* reserved 206-209 */ #define EM_KM32 210 /* KM211 KM32 */ #define EM_KMX32 211 /* KM211 KMX32 */ #define EM_EMX16 212 /* KM211 KMX16 */ #define EM_EMX8 213 /* KM211 KMX8 */ #define EM_KVARC 214 /* KM211 KVARC */ #define EM_CDP 215 /* Paneve CDP */ #define EM_COGE 216 /* Cognitive Smart Memory Processor */ #define EM_COOL 217 /* Bluechip CoolEngine */ #define EM_NORC 218 /* Nanoradio Optimized RISC */ #define EM_CSR_KALIMBA 219 /* CSR Kalimba */ #define EM_Z80 220 /* Zilog Z80 */ #define EM_VISIUM 221 /* Controls and Data Services VISIUMcore */ #define EM_FT32 222 /* FTDI Chip FT32 */ #define EM_MOXIE 223 /* Moxie processor */ #define EM_AMDGPU 224 /* AMD GPU */ /* reserved 225-242 */ #define EM_RISCV 243 /* RISC-V */ #define EM_BPF 247 /* Linux BPF -- in-kernel virtual machine */ #define EM_CSKY 252 /* C-SKY */ #define EM_LOONGARCH 258 /* LoongArch */ #define EM_NUM 259 /* Old spellings/synonyms. */ #define EM_ARC_A5 EM_ARC_COMPACT /* If it is necessary to assign new unofficial EM_* values, please pick large random numbers (0x8523, 0xa7f2, etc.) to minimize the chances of collision with official or non-GNU unofficial values. */ #define EM_ALPHA 0x9026 /* Legal values for e_version (version). */ #define EV_NONE 0 /* Invalid ELF version */ #define EV_CURRENT 1 /* Current version */ #define EV_NUM 2 /* Section header. */ typedef struct { Elf32_Word sh_name; /* Section name (string tbl index) */ Elf32_Word sh_type; /* Section type */ Elf32_Word sh_flags; /* Section flags */ Elf32_Addr sh_addr; /* Section virtual addr at execution */ Elf32_Off sh_offset; /* Section file offset */ Elf32_Word sh_size; /* Section size in bytes */ Elf32_Word sh_link; /* Link to another section */ Elf32_Word sh_info; /* Additional section information */ Elf32_Word sh_addralign; /* Section alignment */ Elf32_Word sh_entsize; /* Entry size if section holds table */ } Elf32_Shdr; typedef struct { Elf64_Word sh_name; /* Section name (string tbl index) */ Elf64_Word sh_type; /* Section type */ Elf64_Xword sh_flags; /* Section flags */ Elf64_Addr sh_addr; /* Section virtual addr at execution */ Elf64_Off sh_offset; /* Section file offset */ Elf64_Xword sh_size; /* Section size in bytes */ Elf64_Word sh_link; /* Link to another section */ Elf64_Word sh_info; /* Additional section information */ Elf64_Xword sh_addralign; /* Section alignment */ Elf64_Xword sh_entsize; /* Entry size if section holds table */ } Elf64_Shdr; /* Special section indices. */ #define SHN_UNDEF 0 /* Undefined section */ #define SHN_LORESERVE 0xff00 /* Start of reserved indices */ #define SHN_LOPROC 0xff00 /* Start of processor-specific */ #define SHN_BEFORE 0xff00 /* Order section before all others (Solaris). */ #define SHN_AFTER 0xff01 /* Order section after all others (Solaris). */ #define SHN_HIPROC 0xff1f /* End of processor-specific */ #define SHN_LOOS 0xff20 /* Start of OS-specific */ #define SHN_HIOS 0xff3f /* End of OS-specific */ #define SHN_ABS 0xfff1 /* Associated symbol is absolute */ #define SHN_COMMON 0xfff2 /* Associated symbol is common */ #define SHN_XINDEX 0xffff /* Index is in extra table. */ #define SHN_HIRESERVE 0xffff /* End of reserved indices */ /* Legal values for sh_type (section type). */ #define SHT_NULL 0 /* Section header table entry unused */ #define SHT_PROGBITS 1 /* Program data */ #define SHT_SYMTAB 2 /* Symbol table */ #define SHT_STRTAB 3 /* String table */ #define SHT_RELA 4 /* Relocation entries with addends */ #define SHT_HASH 5 /* Symbol hash table */ #define SHT_DYNAMIC 6 /* Dynamic linking information */ #define SHT_NOTE 7 /* Notes */ #define SHT_NOBITS 8 /* Program space with no data (bss) */ #define SHT_REL 9 /* Relocation entries, no addends */ #define SHT_SHLIB 10 /* Reserved */ #define SHT_DYNSYM 11 /* Dynamic linker symbol table */ #define SHT_INIT_ARRAY 14 /* Array of constructors */ #define SHT_FINI_ARRAY 15 /* Array of destructors */ #define SHT_PREINIT_ARRAY 16 /* Array of pre-constructors */ #define SHT_GROUP 17 /* Section group */ #define SHT_SYMTAB_SHNDX 18 /* Extended section indices */ #define SHT_RELR 19 /* RELR relative relocations */ #define SHT_NUM 20 /* Number of defined types. */ #define SHT_LOOS 0x60000000 /* Start OS-specific. */ #define SHT_GNU_ATTRIBUTES 0x6ffffff5 /* Object attributes. */ #define SHT_GNU_HASH 0x6ffffff6 /* GNU-style hash table. */ #define SHT_GNU_LIBLIST 0x6ffffff7 /* Prelink library list */ #define SHT_CHECKSUM 0x6ffffff8 /* Checksum for DSO content. */ #define SHT_LOSUNW 0x6ffffffa /* Sun-specific low bound. */ #define SHT_SUNW_move 0x6ffffffa #define SHT_SUNW_COMDAT 0x6ffffffb #define SHT_SUNW_syminfo 0x6ffffffc #define SHT_GNU_verdef 0x6ffffffd /* Version definition section. */ #define SHT_GNU_verneed 0x6ffffffe /* Version needs section. */ #define SHT_GNU_versym 0x6fffffff /* Version symbol table. */ #define SHT_HISUNW 0x6fffffff /* Sun-specific high bound. */ #define SHT_HIOS 0x6fffffff /* End OS-specific type */ #define SHT_LOPROC 0x70000000 /* Start of processor-specific */ #define SHT_HIPROC 0x7fffffff /* End of processor-specific */ #define SHT_LOUSER 0x80000000 /* Start of application-specific */ #define SHT_HIUSER 0x8fffffff /* End of application-specific */ /* Legal values for sh_flags (section flags). */ #define SHF_WRITE (1 << 0) /* Writable */ #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */ #define SHF_EXECINSTR (1 << 2) /* Executable */ #define SHF_MERGE (1 << 4) /* Might be merged */ #define SHF_STRINGS (1 << 5) /* Contains nul-terminated strings */ #define SHF_INFO_LINK (1 << 6) /* `sh_info' contains SHT index */ #define SHF_LINK_ORDER (1 << 7) /* Preserve order after combining */ #define SHF_OS_NONCONFORMING (1 << 8) /* Non-standard OS specific handling required */ #define SHF_GROUP (1 << 9) /* Section is member of a group. */ #define SHF_TLS (1 << 10) /* Section hold thread-local data. */ #define SHF_COMPRESSED (1 << 11) /* Section with compressed data. */ #define SHF_MASKOS 0x0ff00000 /* OS-specific. */ #define SHF_MASKPROC 0xf0000000 /* Processor-specific */ #define SHF_GNU_RETAIN (1 << 21) /* Not to be GCed by linker. */ #define SHF_ORDERED (1 << 30) /* Special ordering requirement (Solaris). */ #define SHF_EXCLUDE (1U << 31) /* Section is excluded unless referenced or allocated (Solaris).*/ /* Section compression header. Used when SHF_COMPRESSED is set. */ typedef struct { Elf32_Word ch_type; /* Compression format. */ Elf32_Word ch_size; /* Uncompressed data size. */ Elf32_Word ch_addralign; /* Uncompressed data alignment. */ } Elf32_Chdr; typedef struct { Elf64_Word ch_type; /* Compression format. */ Elf64_Word ch_reserved; Elf64_Xword ch_size; /* Uncompressed data size. */ Elf64_Xword ch_addralign; /* Uncompressed data alignment. */ } Elf64_Chdr; /* Legal values for ch_type (compression algorithm). */ #define ELFCOMPRESS_ZLIB 1 /* ZLIB/DEFLATE algorithm. */ #define ELFCOMPRESS_ZSTD 2 /* Zstandard algorithm. */ #define ELFCOMPRESS_LOOS 0x60000000 /* Start of OS-specific. */ #define ELFCOMPRESS_HIOS 0x6fffffff /* End of OS-specific. */ #define ELFCOMPRESS_LOPROC 0x70000000 /* Start of processor-specific. */ #define ELFCOMPRESS_HIPROC 0x7fffffff /* End of processor-specific. */ /* Section group handling. */ #define GRP_COMDAT 0x1 /* Mark group as COMDAT. */ /* Symbol table entry. */ typedef struct { Elf32_Word st_name; /* Symbol name (string tbl index) */ Elf32_Addr st_value; /* Symbol value */ Elf32_Word st_size; /* Symbol size */ unsigned char st_info; /* Symbol type and binding */ unsigned char st_other; /* Symbol visibility */ Elf32_Section st_shndx; /* Section index */ } Elf32_Sym; typedef struct { Elf64_Word st_name; /* Symbol name (string tbl index) */ unsigned char st_info; /* Symbol type and binding */ unsigned char st_other; /* Symbol visibility */ Elf64_Section st_shndx; /* Section index */ Elf64_Addr st_value; /* Symbol value */ Elf64_Xword st_size; /* Symbol size */ } Elf64_Sym; /* The syminfo section if available contains additional information about every dynamic symbol. */ typedef struct { Elf32_Half si_boundto; /* Direct bindings, symbol bound to */ Elf32_Half si_flags; /* Per symbol flags */ } Elf32_Syminfo; typedef struct { Elf64_Half si_boundto; /* Direct bindings, symbol bound to */ Elf64_Half si_flags; /* Per symbol flags */ } Elf64_Syminfo; /* Possible values for si_boundto. */ #define SYMINFO_BT_SELF 0xffff /* Symbol bound to self */ #define SYMINFO_BT_PARENT 0xfffe /* Symbol bound to parent */ #define SYMINFO_BT_LOWRESERVE 0xff00 /* Beginning of reserved entries */ /* Possible bitmasks for si_flags. */ #define SYMINFO_FLG_DIRECT 0x0001 /* Direct bound symbol */ #define SYMINFO_FLG_PASSTHRU 0x0002 /* Pass-through symbol for translator */ #define SYMINFO_FLG_COPY 0x0004 /* Symbol is a copy-reloc */ #define SYMINFO_FLG_LAZYLOAD 0x0008 /* Symbol bound to object to be lazy loaded */ /* Syminfo version values. */ #define SYMINFO_NONE 0 #define SYMINFO_CURRENT 1 #define SYMINFO_NUM 2 /* How to extract and insert information held in the st_info field. */ #define ELF32_ST_BIND(val) (((unsigned char) (val)) >> 4) #define ELF32_ST_TYPE(val) ((val) & 0xf) #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) /* Both Elf32_Sym and Elf64_Sym use the same one-byte st_info field. */ #define ELF64_ST_BIND(val) ELF32_ST_BIND (val) #define ELF64_ST_TYPE(val) ELF32_ST_TYPE (val) #define ELF64_ST_INFO(bind, type) ELF32_ST_INFO ((bind), (type)) /* Legal values for ST_BIND subfield of st_info (symbol binding). */ #define STB_LOCAL 0 /* Local symbol */ #define STB_GLOBAL 1 /* Global symbol */ #define STB_WEAK 2 /* Weak symbol */ #define STB_NUM 3 /* Number of defined types. */ #define STB_LOOS 10 /* Start of OS-specific */ #define STB_GNU_UNIQUE 10 /* Unique symbol. */ #define STB_HIOS 12 /* End of OS-specific */ #define STB_LOPROC 13 /* Start of processor-specific */ #define STB_HIPROC 15 /* End of processor-specific */ /* Legal values for ST_TYPE subfield of st_info (symbol type). */ #define STT_NOTYPE 0 /* Symbol type is unspecified */ #define STT_OBJECT 1 /* Symbol is a data object */ #define STT_FUNC 2 /* Symbol is a code object */ #define STT_SECTION 3 /* Symbol associated with a section */ #define STT_FILE 4 /* Symbol's name is file name */ #define STT_COMMON 5 /* Symbol is a common data object */ #define STT_TLS 6 /* Symbol is thread-local data object*/ #define STT_NUM 7 /* Number of defined types. */ #define STT_LOOS 10 /* Start of OS-specific */ #define STT_GNU_IFUNC 10 /* Symbol is indirect code object */ #define STT_HIOS 12 /* End of OS-specific */ #define STT_LOPROC 13 /* Start of processor-specific */ #define STT_HIPROC 15 /* End of processor-specific */ /* Symbol table indices are found in the hash buckets and chain table of a symbol hash table section. This special index value indicates the end of a chain, meaning no further symbols are found in that bucket. */ #define STN_UNDEF 0 /* End of a chain. */ /* How to extract and insert information held in the st_other field. */ #define ELF32_ST_VISIBILITY(o) ((o) & 0x03) /* For ELF64 the definitions are the same. */ #define ELF64_ST_VISIBILITY(o) ELF32_ST_VISIBILITY (o) /* Symbol visibility specification encoded in the st_other field. */ #define STV_DEFAULT 0 /* Default symbol visibility rules */ #define STV_INTERNAL 1 /* Processor specific hidden class */ #define STV_HIDDEN 2 /* Sym unavailable in other modules */ #define STV_PROTECTED 3 /* Not preemptible, not exported */ /* Relocation table entry without addend (in section of type SHT_REL). */ typedef struct { Elf32_Addr r_offset; /* Address */ Elf32_Word r_info; /* Relocation type and symbol index */ } Elf32_Rel; /* I have seen two different definitions of the Elf64_Rel and Elf64_Rela structures, so we'll leave them out until Novell (or whoever) gets their act together. */ /* The following, at least, is used on Sparc v9, MIPS, and Alpha. */ typedef struct { Elf64_Addr r_offset; /* Address */ Elf64_Xword r_info; /* Relocation type and symbol index */ } Elf64_Rel; /* Relocation table entry with addend (in section of type SHT_RELA). */ typedef struct { Elf32_Addr r_offset; /* Address */ Elf32_Word r_info; /* Relocation type and symbol index */ Elf32_Sword r_addend; /* Addend */ } Elf32_Rela; typedef struct { Elf64_Addr r_offset; /* Address */ Elf64_Xword r_info; /* Relocation type and symbol index */ Elf64_Sxword r_addend; /* Addend */ } Elf64_Rela; /* RELR relocation table entry */ typedef Elf32_Word Elf32_Relr; typedef Elf64_Xword Elf64_Relr; /* How to extract and insert information held in the r_info field. */ #define ELF32_R_SYM(val) ((val) >> 8) #define ELF32_R_TYPE(val) ((val) & 0xff) #define ELF32_R_INFO(sym, type) (((sym) << 8) + ((type) & 0xff)) #define ELF64_R_SYM(i) ((i) >> 32) #define ELF64_R_TYPE(i) ((i) & 0xffffffff) #define ELF64_R_INFO(sym,type) ((((Elf64_Xword) (sym)) << 32) + (type)) /* Program segment header. */ typedef struct { Elf32_Word p_type; /* Segment type */ Elf32_Off p_offset; /* Segment file offset */ Elf32_Addr p_vaddr; /* Segment virtual address */ Elf32_Addr p_paddr; /* Segment physical address */ Elf32_Word p_filesz; /* Segment size in file */ Elf32_Word p_memsz; /* Segment size in memory */ Elf32_Word p_flags; /* Segment flags */ Elf32_Word p_align; /* Segment alignment */ } Elf32_Phdr; typedef struct { Elf64_Word p_type; /* Segment type */ Elf64_Word p_flags; /* Segment flags */ Elf64_Off p_offset; /* Segment file offset */ Elf64_Addr p_vaddr; /* Segment virtual address */ Elf64_Addr p_paddr; /* Segment physical address */ Elf64_Xword p_filesz; /* Segment size in file */ Elf64_Xword p_memsz; /* Segment size in memory */ Elf64_Xword p_align; /* Segment alignment */ } Elf64_Phdr; /* Special value for e_phnum. This indicates that the real number of program headers is too large to fit into e_phnum. Instead the real value is in the field sh_info of section 0. */ #define PN_XNUM 0xffff /* Legal values for p_type (segment type). */ #define PT_NULL 0 /* Program header table entry unused */ #define PT_LOAD 1 /* Loadable program segment */ #define PT_DYNAMIC 2 /* Dynamic linking information */ #define PT_INTERP 3 /* Program interpreter */ #define PT_NOTE 4 /* Auxiliary information */ #define PT_SHLIB 5 /* Reserved */ #define PT_PHDR 6 /* Entry for header table itself */ #define PT_TLS 7 /* Thread-local storage segment */ #define PT_NUM 8 /* Number of defined types */ #define PT_LOOS 0x60000000 /* Start of OS-specific */ #define PT_GNU_EH_FRAME 0x6474e550 /* GCC .eh_frame_hdr segment */ #define PT_GNU_STACK 0x6474e551 /* Indicates stack executability */ #define PT_GNU_RELRO 0x6474e552 /* Read-only after relocation */ #define PT_GNU_PROPERTY 0x6474e553 /* GNU property */ #define PT_GNU_SFRAME 0x6474e554 /* SFrame segment. */ #define PT_LOSUNW 0x6ffffffa #define PT_SUNWBSS 0x6ffffffa /* Sun Specific segment */ #define PT_SUNWSTACK 0x6ffffffb /* Stack segment */ #define PT_HISUNW 0x6fffffff #define PT_HIOS 0x6fffffff /* End of OS-specific */ #define PT_LOPROC 0x70000000 /* Start of processor-specific */ #define PT_HIPROC 0x7fffffff /* End of processor-specific */ /* Legal values for p_flags (segment flags). */ #define PF_X (1 << 0) /* Segment is executable */ #define PF_W (1 << 1) /* Segment is writable */ #define PF_R (1 << 2) /* Segment is readable */ #define PF_MASKOS 0x0ff00000 /* OS-specific */ #define PF_MASKPROC 0xf0000000 /* Processor-specific */ /* rest of the file is removed by @stsp */ #endif /* elf.h */ djstub-0.6/full/elfp.h000066400000000000000000000001271475046620400147230ustar00rootroot00000000000000#ifndef ELFP_H #define ELFP_H #include "stub.h" extern struct ldops elf_ops; #endif djstub-0.6/full/stub.c000066400000000000000000000237611475046620400147560ustar00rootroot00000000000000/* * go32-compatible COFF, PE32 and ELF loader stub. * Copyright (C) 2022, stsp * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ #include #include #include #include #include #include #include #include #include #include "stubinfo.h" #include "coff.h" #include "elfp.h" #include "util.h" #include "stub.h" #define STUB_DEBUG 0 #if STUB_DEBUG #define stub_debug(...) printf(__VA_ARGS__) #else #define stub_debug(...) #endif typedef struct { uint32_t offset32; unsigned short selector; } DPMI_FP; static unsigned short psp_sel; static char __far *client_memory; static DPMI_FP clnt_entry; static _GO32_StubInfo stubinfo; static void link_umb(int on) { asm volatile("int $0x21\n" : : "a"(0x5803), "b"(on) : "cc"); if (on) { asm volatile("int $0x21\n" : : "a"(0x5801), "b"(0x80) : "cc"); } } static void dpmi_init(void) { union REGPACK r = {}; void __far (*sw)(void); unsigned mseg = 0, f; int err; #define CF 1 r.w.ax = 0x1687; intr(0x2f, &r); if ((r.w.flags & CF) || r.w.ax != 0) { fprintf(stderr, "DPMI unavailable\n"); exit(EXIT_FAILURE); } if (!(r.w.bx & 1)) { fprintf(stderr, "DPMI-32 unavailable\n"); exit(EXIT_FAILURE); } sw = MK_FP(r.w.es, r.w.di); if (r.w.si) { link_umb(1); err = _dos_allocmem(r.w.si, &mseg); link_umb(0); if (err) { fprintf(stderr, "malloc of %i para failed\n", r.w.si); exit(EXIT_FAILURE); } } asm volatile( "mov %[mseg], %%es\n" "lcall *%[sw]\n" "pushf\n" "pop %0\n" "mov %%es, %1\n" "push %%ds\n" "pop %%es\n" : "=r"(f), "=r"(psp_sel) : "a"(1), [mseg]"r"(mseg), [sw]"m"(sw) : "cc", "memory"); if (f & CF) { fprintf(stderr, "DPMI init failed\n"); exit(EXIT_FAILURE); } } static const char *_basename(const char *name) { const char *p; p = strrchr(name, '\\'); if (!p) p = name; else p++; return p; } #if 0 static char *_fname(char *name) { char *p, *p1; p = strrchr(name, '\\'); if (!p) p = name; else p++; p1 = strrchr(p, '.'); if (p1) p1[0] = '\0'; return p; } #endif int main(int argc, char *argv[], char *envp[]) { int ifile; off_t coffset = 0; uint32_t coffsize = 0; uint32_t noffset = 0; uint32_t nsize = 0; uint32_t noffset2 = 0; uint32_t nsize2 = 0; int rc, i; #define BUF_SIZE 0x40 char buf[BUF_SIZE]; int done = 0; uint32_t va; uint32_t va_size; uint32_t mem_lin; uint32_t mem_base; unsigned short clnt_ds; unsigned short stubinfo_fs; unsigned short mem_hi, mem_lo, si, di; unsigned long alloc_size; unsigned long stubinfo_mem; dpmi_dos_block db; void *handle; struct ldops *ops = NULL; if (argc == 0) { fprintf(stderr, "no env\n"); exit(EXIT_FAILURE); } dpmi_init(); ifile = open(argv[0], O_RDONLY); if (ifile == -1) { fprintf(stderr, "cannot open %s\n", argv[0]); exit(EXIT_FAILURE); } while (!done) { int cnt = 0; stub_debug("Expecting header at 0x%lx\n", coffset); rc = read(ifile, buf, BUF_SIZE); if (rc != BUF_SIZE) { perror("fread()"); exit(EXIT_FAILURE); } if (buf[0] == 'M' && buf[1] == 'Z' && buf[8] == 4 /* lfanew */) { uint32_t offs; cnt++; stub_debug("Found exe header %i at 0x%lx\n", cnt, coffset); memcpy(&offs, &buf[0x3c], sizeof(offs)); coffset = offs; memcpy(&coffsize, &buf[0x1c], sizeof(coffsize)); if (coffsize) noffset = coffset + coffsize; memcpy(&nsize, &buf[0x20], sizeof(nsize)); if (nsize) noffset2 = noffset + nsize; memcpy(&nsize2, &buf[0x24], sizeof(nsize2)); strncpy(stubinfo.payload2_name, &buf[0x30], 12); stubinfo.payload2_name[12] = '\0'; } else if (buf[0] == 0x4c && buf[1] == 0x01) { /* it's a COFF */ done = 1; ops = &coff_ops; } else if (buf[0] == 0x7f && buf[1] == 0x45 && buf[2] == 0x4c && buf[3] == 0x46) { /* it's an ELF */ done = 1; ops = &elf_ops; } else { fprintf(stderr, "not an exe %s at %lx\n", argv[0], coffset); exit(EXIT_FAILURE); } lseek(ifile, coffset, SEEK_SET); } assert(ops); handle = ops->read_headers(ifile); if (!handle) exit(EXIT_FAILURE); va = ops->get_va(handle); va_size = ops->get_length(handle); clnt_entry.offset32 = ops->get_entry(handle); stub_debug("va 0x%lx va_size 0x%lx entry 0x%lx\n", va, va_size, clnt_entry.offset32); strncpy(stubinfo.magic, "go32stub,v3,stsp", sizeof(stubinfo.magic)); stubinfo.size = sizeof(stubinfo); i = 0; while(*envp) { i += strlen(*envp) + 1; envp++; } if (i) { i += strlen(argv[0]) + 1; i += 3; } stub_debug("env size %i\n", i); stubinfo.env_size = i; stubinfo.minstack = 0x80000; stubinfo.minkeep = 0x4000; strncpy(stubinfo.argv0, _basename(argv[0]), sizeof(stubinfo.argv0)); stubinfo.argv0[sizeof(stubinfo.argv0) - 1] = '\0'; // strncpy(stubinfo.basename, _fname(argv0), sizeof(stubinfo.basename)); // stubinfo.basename[sizeof(stubinfo.basename) - 1] = '\0'; strncpy(stubinfo.dpmi_server, "CWSDPMI.EXE", sizeof(stubinfo.dpmi_server)); #define max(a, b) ((a) > (b) ? (a) : (b)) stubinfo.initial_size = max(va_size, 0x10000); stubinfo.psp_selector = psp_sel; /* DJGPP relies on ds_selector, cs_selector and ds_segment all mapping * the same real-mode memory block. */ link_umb(1); db = _DPMIAllocateDOSMemoryBlock(stubinfo.minkeep >> 4); link_umb(0); stub_debug("rm seg 0x%x\n", db.rm); stubinfo.ds_selector = db.pm; stubinfo.ds_segment = db.rm; /* create alias */ asm volatile("int $0x31\n" : "=a"(stubinfo.cs_selector) : "a"(0xa), "b"(db.pm)); /* set descriptor access rights */ asm volatile("int $0x31\n" : : "a"(9), "b"(stubinfo.cs_selector), "c"(0x00fb) : "cc"); clnt_entry.selector = _DPMIAllocateLDTDescriptors(1); clnt_ds = _DPMIAllocateLDTDescriptors(1); alloc_size = PAGE_ALIGN(stubinfo.initial_size); /* allocate mem */ asm volatile("int $0x31\n" : "=b"(mem_hi), "=c"(mem_lo), "=S"(si), "=D"(di) : "a"(0x501), "b"((uint16_t)(alloc_size >> 16)), "c"((uint16_t)(alloc_size & 0xffff)) : "cc"); stubinfo.memory_handle = ((uint32_t)si << 16) | di; mem_lin = ((uint32_t)mem_hi << 16) | mem_lo; mem_base = mem_lin - va; stubinfo.mem_base = mem_base; stub_debug("mem_lin 0x%lx mem_base 0x%lx\n", mem_lin, mem_base); client_memory = MK_FP(clnt_ds, 0); /* set base */ asm volatile("int $0x31\n" : : "a"(7), "b"(clnt_entry.selector), "c"((uint16_t)(mem_base >> 16)), "d"((uint16_t)(mem_base & 0xffff)) : "cc"); /* set descriptor access rights */ asm volatile("int $0x31\n" : : "a"(9), "b"(clnt_entry.selector), "c"(0xc0fb) : "cc"); /* set limit */ asm volatile("int $0x31\n" : : "a"(8), "b"(clnt_entry.selector), "c"(0xffff), "d"(0xffff) : "cc"); /* set base */ asm volatile("int $0x31\n" : : "a"(7), "b"(clnt_ds), "c"((uint16_t)(mem_base >> 16)), "d"((uint16_t)(mem_base & 0xffff)) : "cc"); /* set descriptor access rights */ asm volatile("int $0x31\n" : : "a"(9), "b"(clnt_ds), "c"(0xc0f3) : "cc"); /* set limit */ asm volatile("int $0x31\n" : : "a"(8), "b"(clnt_ds), "c"(0xffff), "d"(0xffff) : "cc"); /* create ds alias */ asm volatile( "mov %%ds, %%bx\n" "int $0x31\n" : "=a"(stubinfo_fs) : "a"(0xa)); stubinfo_mem = _DPMIGetSegmentBaseAddress(stubinfo_fs) + (uintptr_t)&stubinfo; mem_hi = stubinfo_mem >> 16; mem_lo = stubinfo_mem & 0xffff; /* set base */ asm volatile("int $0x31\n" : : "a"(7), "b"(stubinfo_fs), "c"(mem_hi), "d"(mem_lo) : "cc"); ops->read_sections(handle, client_memory, ifile, coffset); stubinfo.self_fd = ifile; stubinfo.self_offs = coffset; stubinfo.self_size = coffsize; stubinfo.payload_offs = noffset; stubinfo.payload_size = nsize; stubinfo.payload2_offs = noffset2; stubinfo.payload2_size = nsize2; if (stubinfo.payload2_name[0]) strcat(stubinfo.payload2_name, ".dbg"); lseek(ifile, noffset, SEEK_SET); if (nsize > 0) stub_debug("Found payload of size %li at 0x%lx\n", nsize, noffset); stubinfo.stubinfo_ver = 2; stub_debug("Jump to entry...\n"); asm volatile( ".arch i386\n" "mov %%ax, %%fs\n" "push %%ds\n" "pop %%es\n" "mov %1, %%ds\n" "ljmpl *%%es:%2\n" ".arch i286\n" : : "a"(stubinfo_fs), "r"(clnt_ds), "m"(clnt_entry) : "memory"); return 0; } djstub-0.6/full/stub.h000066400000000000000000000004721475046620400147550ustar00rootroot00000000000000#ifndef STUB_H #define STUB_H struct ldops { void *(*read_headers)(int ifile); uint32_t (*get_va)(void *handle); uint32_t (*get_length)(void *handle); uint32_t (*get_entry)(void *handle); void (*read_sections)(void *handle, char __far *ptr, int ifile, uint32_t offset); }; #endif djstub-0.6/full/stubinfo.h000066400000000000000000000032431475046620400156300ustar00rootroot00000000000000#ifndef __dj_include_stub_h__ #define __dj_include_stub_h__ #define STUBINFO 0 #define STUBINFO_MAGIC 0 #define STUBINFO_SIZE 0x10 #define STUBINFO_MINSTACK 0x14 #define STUBINFO_MEMORY_HANDLE 0x18 #define STUBINFO_INITIAL_SIZE 0x1c #define STUBINFO_MINKEEP 0x20 #define STUBINFO_DS_SELECTOR 0x22 #define STUBINFO_DS_SEGMENT 0x24 #define STUBINFO_PSP_SELECTOR 0x26 #define STUBINFO_CS_SELECTOR 0x28 #define STUBINFO_ENV_SIZE 0x2a #define STUBINFO_BASENAME 0x2c #define STUBINFO_ARGV0 0x34 #define STUBINFO_DPMI_SERVER 0x44 #define STUBINFO_STUBINFO_VER 0x54 #define STUBINFO_SELF_FD 0x58 #define STUBINFO_SELF_OFFS 0x5C #define STUBINFO_SELF_SIZE 0x60 #define STUBINFO_PAYLOAD_OFFS 0x64 #define STUBINFO_PAYLOAD_SIZE 0x68 #define STUBINFO_PAYLOAD2_OFFS 0x6C #define STUBINFO_PAYLOAD2_SIZE 0x70 #define STUBINFO_PAYLOAD2_NAME 0x74 #define STUBINFO_MEM_BASE 0x88 #define STUBINFO_END 0x8C #ifndef __ASSEMBLER__ #include typedef struct { char magic[16]; uint32_t size; uint32_t minstack; uint32_t memory_handle; uint32_t initial_size; unsigned short minkeep; unsigned short ds_selector; unsigned short ds_segment; unsigned short psp_selector; unsigned short cs_selector; unsigned short env_size; char basename[8]; char argv0[16]; char dpmi_server[16]; /* standard djgpp stubinfo ends here */ uint32_t stubinfo_ver; int32_t self_fd; uint32_t self_offs; int32_t self_size; uint32_t payload_offs; int32_t payload_size; uint32_t payload2_offs; int32_t payload2_size; char payload2_name[20]; uint32_t mem_base; } _GO32_StubInfo; #endif static_assert(sizeof(_GO32_StubInfo) == STUBINFO_END, "size mismatch"); #endif /* __dj_include_stub_h__ */ djstub-0.6/full/util.c000066400000000000000000000052431475046620400147510ustar00rootroot00000000000000/* * Copyright (C) 2023, stsp2@yandex.ru * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ #include #include #include #include #include "util.h" static void farmemcpy(char __far *ptr, unsigned long offset, char *src, unsigned long length) { unsigned dummy; assert(!(length & 1)); // speed up memcpy asm volatile( ".arch i386\n" "push %%es\n" "mov %%dx, %%es\n" "mov %[size], %%ecx\n" "movzwl %%di, %%edi\n" "add %[offs], %%edi\n" "shr $1, %%ecx\n" "addr32 rep movsw\n" "pop %%es\n" "xor %%ecx, %%ecx\n" "xor %%edi, %%edi\n" ".arch i286\n" : "=c"(dummy), "=D"(dummy) : "a"(0), "d"(FP_SEG(ptr)), "D"(FP_OFF(ptr)), "S"(src), [size]"m"(length), [offs]"m"(offset) : "memory"); } void farmemset(char __far *ptr, uint32_t vaddr, uint16_t val, uint32_t size) { unsigned dummy; assert(!(size & 1)); // speed up memcpy asm volatile( ".arch i386\n" "push %%es\n" "mov %%dx, %%es\n" "mov %[size], %%ecx\n" "movzwl %%di, %%edi\n" "add %[offs], %%edi\n" "shr $1, %%ecx\n" "addr32 rep stosw\n" "pop %%es\n" "xor %%ecx, %%ecx\n" "xor %%edi, %%edi\n" ".arch i286\n" : "=c"(dummy), "=D"(dummy) : "a"(val), "d"(FP_SEG(ptr)), "D"(FP_OFF(ptr)), [size]"m"(size), [offs]"m"(vaddr) : "memory"); } long _long_read(int file, char __far *buf, unsigned long offset, unsigned long size) { char tmp[PAGE_SIZE]; unsigned long done = 0; #define min(a, b) ((a) < (b) ? (a) : (b)) while (size) { unsigned todo = min(size, sizeof(tmp)); size_t rd = read(file, tmp, todo); if (rd) { /* word-align memcpy */ farmemcpy(buf, offset + done, tmp, rd + (rd & 1)); done += rd; size -= rd; } if (rd < todo) break; } return done; } djstub-0.6/full/util.h000066400000000000000000000006551475046620400147600ustar00rootroot00000000000000#ifndef UTIL_H #define UTIL_H #ifndef PAGE_SIZE #define PAGE_SIZE 4096 #endif #ifndef PAGE_MASK #define PAGE_MASK (~(PAGE_SIZE-1)) #endif /* to align the pointer to the (next) page boundary */ #define PAGE_ALIGN(addr) (((addr)+PAGE_SIZE-1)&PAGE_MASK) void farmemset(char __far *ptr, uint32_t vaddr, uint16_t val, uint32_t size); long _long_read(int file, char __far *buf, unsigned long offset, unsigned long size); #endif djstub-0.6/mini/000077500000000000000000000000001475046620400136165ustar00rootroot00000000000000djstub-0.6/mini/Makefile000066400000000000000000000003741475046620400152620ustar00rootroot00000000000000TOP = .. TGT = $(TOP)/ministub.exe CC = smlrcc SMFLAGS = -dosp ifneq ($(SMPATH),) SMFLAGS += -I $(SMPATH)/v0100/include -SL $(SMPATH) define P PATH=$(SMPATH):$(PATH) endef endif all: $(TGT) $(TGT): ministub.c $(P) $(CC) $(SMFLAGS) $< -o $@ clean: djstub-0.6/mini/ministub.c000066400000000000000000000101271475046620400156150ustar00rootroot00000000000000/* * mini stub for dj64. * Copyright (C) 2024, @stsp * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ #include #include #include #include #include #define DJSTUB_API_VER 5 typedef struct { unsigned long edi; unsigned long esi; unsigned long ebp; unsigned long resrvd; unsigned long ebx; unsigned long edx; unsigned long ecx; unsigned long eax; unsigned short flags; unsigned short es; unsigned short ds; unsigned short fs; unsigned short gs; unsigned short ip; unsigned short cs; unsigned short sp; unsigned short ss; } __dpmi_int_regs; int __dpmi_int(int intno, __dpmi_int_regs* regs); int DPMIQueryExtension(unsigned short *sel, unsigned short *off, const char *name) { asm( "push es\n" "mov eax, 0x168a\n" "mov esi, [ebp+16]\n" // name "int 0x2f\n" "test al, al\n" "jnz fail\n" "mov ebx, [ebp+8]\n" // sel "mov [ebx], es\n" "mov ebx, [ebp+12]\n" "mov [ebx], edi\n" "pop es\n" "xor ah, ah\n" "fail:\n" ); } static void enter_stub(unsigned sel, unsigned off, int argc, char *argv[], int envc, char *envp[], unsigned psp, int fd, int ver) { asm( "mov eax, [ebp+32]\n" // psp "mov ecx, [ebp+40]\n" // ver "shl ecx, 16\n" "or eax, ecx\n" "mov ecx, [ebp+16]\n" // argc "mov edx, [ebp+20]\n" // argv "mov ebx, [ebp+24]\n" // envc "mov esi, [ebp+28]\n" // envp "mov edi, [ebp+36]\n" // fd "push dword [ebp+8]\n " // sel "push dword [ebp+12]\n" // off "call far [ss:esp]\n" "add esp, 8\n" ); } extern void* __dpmi_psp; extern void* __dpmi_env; static unsigned psp; int main(int argc, char *argv[]) { static const char *ext_nm = "DJ64STUB"; unsigned char *env = __dpmi_env; char **envp; unsigned short sel, off; int envc, i, letter; int err; int fd; __dpmi_int_regs regs; if (argc == 0) { puts("no env"); return 1; } for (envc = i = letter = 0;; i++) { if (env[i] == '\0') { letter = 0; if (env[i + 1] == '\0') break; } else { if (!letter) { letter = 1; envc++; } } } envp = malloc((envc + 1) * sizeof(char *)); envp[envc] = NULL; if (envc) { int envc2; for (envc2 = i = letter = 0;; i++) { if (env[i] == '\0') { letter = 0; if (env[i + 1] == '\0') break; } else { if (!letter) { letter = 1; envp[envc2++] = &env[i]; } } } } asm( "mov eax, 0\n" // alloc desc for PSP "mov ecx, 1\n" // 1 desc "int 31h\n" "mov [_psp], ax\n" "mov eax, 7\n" // set base "mov bx, [_psp]\n" "extern ___dpmi_psp\n" "mov cx, [___dpmi_psp + 2]\n" "mov dx, [___dpmi_psp]\n" "int 31h\n" "mov eax, 8\n" // set limit "mov bx, [_psp]\n" "mov cx, 0\n" "mov dx, 0ffh\n" // to PSP size "int 31h\n" ); err = DPMIQueryExtension(&sel, &off, ext_nm); if (err) { printf("%s unsupported (%x)\n", ext_nm, err); return 1; } fd = open(argv[0], O_RDONLY); if (fd == -1) { printf("unable to open %s: %s\n", argv[0], strerror(errno)); return 1; } memset(®s, 0, sizeof(regs)); /* nuke out initial stub as it is no longer needed */ regs.eax = 0x4a00; regs.ebx = 0x10; // leave only PSP regs.es = (unsigned)__dpmi_psp >> 4; __dpmi_int(0x21, ®s); enter_stub(sel, off, argc, argv, envc, envp, psp, fd, DJSTUB_API_VER); close(fd); puts("stub returned"); return 0; } djstub-0.6/ministub.S000066400000000000000000000005021475046620400146350ustar00rootroot00000000000000.data .globl _binary_ministub_exe_start #ifdef __ELF__ .size _binary_ministub_exe_start,0 #endif _binary_ministub_exe_start: .incbin "ministub.exe" .globl _binary_ministub_exe_end #ifdef __ELF__ .size _binary_ministub_exe_end,0 #endif _binary_ministub_exe_end: #ifdef __ELF__ .section .note.GNU-stack,"",%progbits #endif djstub-0.6/stubify.c000066400000000000000000000346661475046620400145320ustar00rootroot00000000000000/* * go32-compatible COFF, PE32 and ELF loader stub. * Copyright (C) 2022, stsp * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ /* Based on djgpp's stubify.c, original copyrights below: */ /* Copyright (C) 2003 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1999 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */ /* * Note: the previous copyrights are provided only to give credits * to previous authors. This file was entirely rewritten and * COPYING.DJ doesn't apply to this project. * See LICENSE file for an actual license information. */ #include #include #include #include #include #include #include #include #include #include #include "elf.h" extern char _binary_stub_exe_end[]; extern char _binary_stub_exe_size[]; extern char _binary_stub_exe_start[]; #define v_printf if(verbose)printf static int verbose; static int rmstub; #define MAX_OVL 5 static char *overlay[MAX_OVL]; static int strip; static uint8_t stub_ver = 6; static uint8_t flags_ver = 0; static const int version = 0; static int copy_file(const char *ovl, int ofile) { char buf[4096]; int rbytes; int ret = 0; int iovfile = open(ovl, O_RDONLY); if (iovfile < 0) { perror(ovl); return -1; } while ((rbytes = read(iovfile, buf, 4096)) > 0) { int wb; wb = write(ofile, buf, rbytes); if (wb != rbytes) { ret = -1; break; } } close(iovfile); return ret; } static const char *payload_dsc[] = { "%s DOS payload", "%s/ELF host payload", "%s/ELF debug info", }; #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) static const char *elf_mach(int mach) { switch (mach) { case EM_386: return "i386"; case EM_X86_64: return "x86_64"; case EM_ARM: return "arm32"; case EM_AARCH64: return "aarch64"; case EM_RISCV: return "risc-v"; } return "unsupported ELF machine type"; } static const char *elf_id(int fd, long offs) { Elf64_Ehdr ehdr; unsigned char *buf = (unsigned char *)&ehdr; int rd = pread(fd, buf, sizeof(ehdr), offs); if (rd != sizeof(ehdr)) return "???"; if (memcmp(buf, ELFMAG, SELFMAG) != 0) return "not an ELF"; switch (buf[EI_CLASS]) { case ELFCLASS32: { Elf32_Ehdr ehdr32; memcpy(&ehdr32, buf, sizeof(ehdr32)); return elf_mach(ehdr32.e_machine); } case ELFCLASS64: return elf_mach(ehdr.e_machine); } return "unsupported ELF class"; } static const char *identify(int num, int fd, long offs) { switch (num) { case 0: return payload_dsc[num]; case 1: case 2: { static char ret[256]; snprintf(ret, sizeof(ret), payload_dsc[num], elf_id(fd, offs)); return ret; } } return "???"; } static int move_tmp(const char *src, const char *dst) { int err = rename(src, dst); if (err && errno == EXDEV) { int fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (fd == -1) { perror("open()"); return -1; } copy_file(src, fd); close(fd); err = unlink(src); if (err) { perror("unlink()"); return -1; } } if (err) { perror("rename()"); return -1; } return 0; } static int coff2exe(const char *fname, const char *oname, int info) { char ibuf[1024], ibuf0[256]; int has_o0 = 0; #define IPRINTF(...) \ snprintf(ibuf + strlen(ibuf), sizeof(ibuf) - strlen(ibuf), __VA_ARGS__) int ifile; int ofile; char buf[4096]; int rbytes; long coffset = 0; uint32_t ooffs = 0; unsigned char mzhdr_buf[0x40]; uint32_t coff_file_size = 0; int rmoverlay = 0; int can_copy_ovl = 0; int i; int ret = 0; char tmpl[] = "/tmp/djstub_XXXXXX"; const uint32_t stub_size = _binary_stub_exe_end - _binary_stub_exe_start; ibuf[0] = '\0'; ibuf0[0] = '\0'; ifile = open(fname, O_RDONLY); if (ifile < 0) { perror("open()"); return -1; } while (1) { int rc; lseek(ifile, coffset, SEEK_SET); rc = read(ifile, buf, 0x40); assert(rc == 0x40); if (buf[0] == 'M' && buf[1] == 'Z') /* stubbed already, skip stub */ { if (buf[8] == 4 && buf[9] == 0) // lfanew { uint32_t offs; uint16_t flags = 0; int dyn = 0; int stub_v4 = (buf[0x3b] >= 4 && buf[0x3b] < 0x20); int stub_v6 = stub_v4 && buf[0x3b] >= 6; if (stub_v6) { flags_ver = buf[0x3a]; memcpy(&flags, &buf[0x38], sizeof(flags)); } else if (stub_v4) { flags_ver = buf[0x3b]; memcpy(&flags, &buf[0x2c], sizeof(flags)); } if (stub_v4 && (flags & 0x80)) dyn++; memcpy(&offs, &buf[0x3c], sizeof(offs)); coffset = offs; memcpy(&coff_file_size, &buf[0x1c], sizeof(coff_file_size)); memcpy(mzhdr_buf, buf, sizeof(mzhdr_buf)); can_copy_ovl++; if (rmstub || strip) rmoverlay++; if (info) { strcat(ibuf, "dj64 file format\n"); if (dyn) strcat(ibuf, "DOS payload dynamic\n"); } if (info || strip) { int cnt = 0; uint32_t sz = 0; char name[20] = {}; for (i = 0x1c; i < 0x28; i += 4) { uint32_t sz0; memcpy(&sz0, &buf[i], sizeof(sz0)); if (!sz0) { if (strip && sz) { coff_file_size = offs - sz - coffset; memset(&mzhdr_buf[i - 4], 0, 4); } break; } sz = sz0; if (!cnt) { has_o0++; if (stub_v6) { memcpy(&ooffs, &buf[0x2c], sizeof(ooffs)); offs += ooffs; } } if (stub_v6 && cnt == 1 && (flags & 0x8000)) offs = coffset; if (info) { int prname = 0; if (stub_v6 && cnt + dyn == 1 && buf[0x28]) { uint32_t noff; memcpy(&noff, &buf[0x28], sizeof(noff)); lseek(ifile, offs + noff, SEEK_SET); read(ifile, name, 16); name[16] = '\0'; } prname = (name[0] && cnt + dyn == 2); IPRINTF("Overlay %i (%s%s%s) at %i, size %i\n", cnt, identify(cnt + dyn, ifile, offs), prname ? " for " : "", prname ? name : "", offs, sz); } offs += sz; cnt++; } if (stub_v4 && !stub_v6) { if (buf[0x2e]) IPRINTF("Overlay name: %.12s\n", buf + 0x2e); } if (stub_v4) { IPRINTF("Stub version: %i\n", buf[0x3b]); IPRINTF("Stub flags: 0x%04x\n", flags); } } } else { int blocks = (unsigned char)buf[4] + (unsigned char)buf[5] * 256; int partial = (unsigned char)buf[2] + (unsigned char)buf[3] * 256; if (info) strcat(ibuf, "exe/djgpp file format\n"); coffset += blocks * 512; if (partial) coffset += partial - 512; } } else if (buf[0] == 0x4c && buf[1] == 0x01) /* it's a COFF */ { if (info) { if (has_o0) strcpy(ibuf0, "i386/COFF"); else IPRINTF("COFF payload at %li\n", coffset); } break; } else if (buf[0] == 0x7f && buf[1] == 0x45 && buf[2] == 0x4c && buf[3] == 0x46) /* it's an ELF */ { if (info) { if (has_o0) snprintf(ibuf0, sizeof(ibuf0), "%s/ELF", elf_id(ifile, coffset + ooffs)); else IPRINTF("ELF payload for %s at %li\n", elf_id(ifile, coffset), coffset); } break; } else { fprintf(stderr, "Warning: input file is neither COFF nor stubbed COFF\n"); break; } } if (!coff_file_size) coff_file_size = lseek(ifile, 0, SEEK_END) - coffset; lseek(ifile, coffset, SEEK_SET); if (can_copy_ovl) { /* copy entire overlay info except 0x3c */ memcpy(_binary_stub_exe_start + 0x1c, mzhdr_buf + 0x1c, 0x3c - 0x1c); } else { /* assume there is just 1 overlay */ memcpy(_binary_stub_exe_start + 0x1c, &coff_file_size, sizeof(coff_file_size)); } _binary_stub_exe_start[0x3a] = flags_ver; _binary_stub_exe_start[0x3b] = stub_ver; memcpy(_binary_stub_exe_start + 0x3c, &stub_size, sizeof(stub_size)); if (info) { if (has_o0) printf(ibuf, ibuf0); else printf("%s", ibuf); close(ifile); return 0; } if (oname) { ofile = open(oname, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (ofile < 0) { perror("open()"); return -1; } } else { ofile = mkstemp(tmpl); if (ofile < 0) { perror("mkstemp()"); return -1; } } if (!rmstub) { int rc = write(ofile, _binary_stub_exe_start, _binary_stub_exe_end-_binary_stub_exe_start); assert(rc == _binary_stub_exe_end - _binary_stub_exe_start); } /* copy either entire payload, or, in case of rmoverlay, only COFF */ while (coff_file_size > 0 && (rbytes=read(ifile, buf, 4096)) > 0) { int wb; if (rmoverlay && rbytes > coff_file_size) rbytes = coff_file_size; wb = write(ofile, buf, rbytes); if (rmoverlay) coff_file_size -= rbytes; if (wb != rbytes) { perror("write()"); close(ifile); close(ofile); unlink(oname); return -1; } } close(ifile); close(ofile); if (!oname) ret = move_tmp(tmpl, fname); return ret; } static void print_help(void) { fprintf(stderr, "Usage: stubify [-v] [-l ] [-o ] \n\n" " may be COFF or stubbed .exe.\n\n" "Options:\n" "-v -> print version\n" "-d -> verbose messages for debugging\n" "-i -> display file info\n" "-s -> strip last overlay\n" "-r -> remove stub (and overlay, if any)\n" "-l -> link in file as an overlay\n" "-n -> write name offset into an overlay info\n" "-o -> write output into \n" "-f -> write into an overlay info\n" "-V -> write into flags version field\n" "-g -> generate a new file\n" ); } int main(int argc, char **argv) { int generate = 0; char *oname = NULL; int info = 0; int c; int noverlay = 0; uint32_t nmoffs = 0; uint32_t osize = 0; uint32_t ooffs = 0; uint16_t stub_flags = 0; int rc; if (_binary_stub_exe_start[0] != 'M' || _binary_stub_exe_start[1] != 'Z' || _binary_stub_exe_start[8] != 4 || _binary_stub_exe_start[9] != 0) { fprintf(stderr, "stub corrupted, bad build\n"); return EXIT_FAILURE; } while ((c = getopt(argc, argv, "dvV:irS:sgl:O:o:n:f:")) != -1) { switch (c) { case 'v': #define _S(x) #x #define S(x) _S(x) printf("djstubify version 0.%i\n", version + stub_ver); return 0; case 'd': verbose = 1; break; case 'i': info = 1; break; case 'r': rmstub = 1; break; case 'S': osize = strtol(optarg, NULL, 0); break; case 's': strip = 1; break; case 'g': generate = 1; break; case 'l': assert(noverlay < MAX_OVL); overlay[noverlay++] = optarg; break; case 'n': nmoffs = strtol(optarg, NULL, 0); break; case 'f': stub_flags |= strtol(optarg, NULL, 0); break; case 'O': ooffs = strtol(optarg, NULL, 0); break; case 'o': oname = optarg; break; case 'V': flags_ver = atoi(optarg); if (!flags_ver) { fprintf(stderr, "Bad stub version: %s\n", optarg); return 1; } break; default: fprintf(stderr, "Unknown option: %c\n", c); print_help(); return 1; } } v_printf("stubify for dj64 executables, copyright (C) 2023 stsp\n"); if (generate) { int ofile; int i; const uint32_t stub_size = _binary_stub_exe_end - _binary_stub_exe_start; if (!oname) { fprintf(stderr, "djstubify: -o missing\n"); return EXIT_FAILURE; } if (noverlay) { struct stat sb; int dyn = 0; if (osize) { memcpy(_binary_stub_exe_start + 0x1c, &osize, 4); dyn++; } /* store overlay sizes in overlay info */ for (i = 0; i < noverlay; i++) { int rc = stat(overlay[i], &sb); if (rc == -1) { fprintf(stderr, "failed to stat %s: %s\n", overlay[i], strerror(errno)); return EXIT_FAILURE; } memcpy(_binary_stub_exe_start + 0x1c + (i + dyn) * 4, &sb.st_size, 4); } if (ooffs) memcpy(_binary_stub_exe_start + 0x2c, &ooffs, 4); } memcpy(_binary_stub_exe_start + 0x28, &nmoffs, sizeof(nmoffs)); memcpy(_binary_stub_exe_start + 0x38, &stub_flags, sizeof(stub_flags)); _binary_stub_exe_start[0x3a] = flags_ver; _binary_stub_exe_start[0x3b] = stub_ver; memcpy(_binary_stub_exe_start + 0x3c, &stub_size, sizeof(stub_size)); ofile = open(oname, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (ofile < 0) { fprintf(stderr, "Cannot open output file to generate\n"); perror("open()"); return 1; } v_printf("stubify: generate %s\n", oname); rc = write(ofile, _binary_stub_exe_start, _binary_stub_exe_end-_binary_stub_exe_start); assert(rc == _binary_stub_exe_end - _binary_stub_exe_start); for (i = 0; i < noverlay; i++) { int rc = copy_file(overlay[i], ofile); if (rc < 0) { fprintf(stderr, "failed to copy overlays\n"); close(ofile); unlink(oname); return EXIT_FAILURE; } } close(ofile); } else { int err; if (argc - optind < 1) { print_help(); return 1; } err = coff2exe(argv[argc - 1], oname, info); if (err) return 1; } return 0; }