2

Given a file, I want to check if this is a DLL, or a shared object (Linux) or a dylib (Mac OS X), or something different. My main interest is differentiating executable and DLL on Linux and Mac OS X. For windows, the extension should be enough for my problem.

I already checked that the magic number technique doesn't work for Linux as executable and shared objects both have the same number.

PierreBdR
  • 42,120
  • 10
  • 46
  • 62
  • From my bit of research, it looks like you didn't look deeply enough into 'magic' - there's probably more than just a single number. See my answer. – Carl Smotricz Nov 25 '09 at 18:44
  • Ed, a DLL usually contains executable code. That doesn't necessarily make it executable standalone. There is likely no entry point. The second L stands for 'library', or "extra code bucket," essentially. – Carl Smotricz Nov 25 '09 at 18:54
  • 1
    Yeah, I understand the semantics, and it is still an executable file. http://support.microsoft.com/kb/87934 – Ed S. Mar 29 '10 at 23:48
  • @EdS Unfortunately, the article you looked at is a very old one, referring to Windows 3.1. In 16-bit Windows, DLLs *were* executables. In fact, they were identical just about every way to EXE files. The file extension was really the only difference. Before Windows 3.0 (or maybe 3.1?), the DLL suffix hadn't been invented yet and even the system DLLs had EXE file extensions. **That's no longer the case, though.** There are some very important semantic differences between DLLs and EXE files that go beyond the file extension in 32-bit Windows. – Cody Gray - on strike Aug 09 '12 at 23:34
  • @CodyGray: By the definition I was using, an executable file is a file which contains executable instructions. .EXE is a format (PE) that Windows uses, but it just one format of many. Looking back (this was almost three years ago!) I do suppose that my interpretation is not very helpful here in regards to the question. And yeah, that article is severely outdated, I probably should have read that more thoroughly before linking it :) – Ed S. Aug 09 '12 at 23:36

3 Answers3

9

The Unix (Linux and Mac OS X) command file knows how to identify file types.

man file tells you about the 'magic' information used to do this, and in particular where the file with that information is to be found.

man 5 magic describes the file in detail and should also tell you where it lives. You can take a look in there and pull anything you need from it. And/or just crib code from the source of file.

Update:
Note that on Linux a file could be both an executable and a shared library at the same time.
One example is /lib/libc.so.6 (a shared library which can be executed).
Another example: any executable built with -pie flag can be used as a shared library. See this answer for details.

Community
  • 1
  • 1
Carl Smotricz
  • 66,391
  • 18
  • 125
  • 167
1

Use the file tool (for Linux and Mac OS).

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
0

Write a program, try to load the DLL - if it succeeds, it is one :)

Edit: Ok this might as well succeed for executables :-/

Heinrich Ulbricht
  • 10,064
  • 4
  • 54
  • 85