39

Assuming I have booted a 32-bit Windows Server with the /3GB switch, how can I make a .NET application use the additional address space?

wildcard
  • 701
  • 2
  • 6
  • 9
  • 3
    And LARGEADDRESSAWARE can gives you 4GB minus 64KB on 64 bits Windows ([see on "The Old New Thing"](http://blogs.msdn.com/b/oldnewthing/archive/2014/10/23/10566700.aspx)) – MuiBienCarlota Jun 06 '15 at 17:23
  • @MuiBienCarlota link seems to be broken. – Alex Hope O'Connor Oct 09 '22 at 18:33
  • @AlexHopeO'Connor You're rigth, but I can't edit my comment. We can find it here : [Using /LARGEADDRESSAWARE on 64-bit Windows for 32-bit programs](https://devblogs.microsoft.com/oldnewthing/20050601-24/?p=35483) – MuiBienCarlota Oct 12 '22 at 13:47

5 Answers5

57

The flag is part of the image header, so you need to modify that using editbin.

editbin /LARGEADDRESSAWARE <your exe>

Use dumpbin /headers and look for the presence of Application can handle large (>2GB) addresses to see if the flag is set or not.

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
  • 2
    look for the `Application can handle large (>2GB) addresses` string in dumpbin output – Aviad Rozenhek May 12 '11 at 14:52
  • Does setting the header in an executable after the fact make a .NET application "large address aware", or do you actually need to write or compile your app differently to make it "large address aware"? Are there any pitfalls/gotchas in doing this to any 32 bit executable that is running out of memory on a 64 bit windows system (win7 or newer)? – Martin Szabo Nov 21 '16 at 17:56
  • Do you need to do this for each DLL in your library or just the main exe? – rollsch May 01 '19 at 02:51
  • I tried to just use ```editbin /LARGEADDRESSAWARE``` on my existing .exe only (C# WPF, x86), not on any dll, it works (on Windows 10 64bit) – Guang Aug 20 '21 at 17:17
8

From what I can tell you have to use the editbin utility shown in the existing answer. There does not appear to be any way to set the flag using Visual Studio .NET, it looks like they encourage people to compile for 64 bit if possible instead of using the flag

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=93771

WildCrustacean
  • 5,896
  • 2
  • 31
  • 42
3

Add those lines to Post build:

call "$(DevEnvDir)..\tools\vsdevcmd.bat"
editbin /largeaddressaware "$(TargetPath)"

From: vsvars32.bat in Visual Studio 2017

Pedro77
  • 5,176
  • 7
  • 61
  • 91
2

For doing it automaticaly from Visual studio, please refer to this question : flag from visual studio.

Community
  • 1
  • 1
Thomas
  • 5,603
  • 5
  • 32
  • 48
2

Thus far there haven't been an answer giving a cross-platform and open-source way to set LAA bit on a PE executable, so I decided to fill the gap.

Note: make sure you have a backup!

You can that with reverse-engineering framework radare2. In case you use a Linux distro, radare2 is typically in the repository. Unfortunately, the capability to set the bit isn't built-in, nonetheless it is quite easy with the following script:

e cfg.newshell=true      # allows nested $(…) commands
s/ PE\0\0                # search PE file signature
s +4                     # skip the signature
echo "Original content:"
pf.pe_image_file_header.characteristics
echo "Patching the file…"
s+ 0x12                  # go to the characteristics field
wv2 $(?v $(pv2) \| 0x20) # 0x20 is the LAA bit, binary-OR it in the address
s-
echo "The new content:"
pf.pe_image_file_header.characteristics

Here's a demo how you use it (the script is in script.r2 file) with a notepad.exe file:

 λ r2 -qi script.r2 -nnw notepad.exe
Searching 4 bytes in [0x1-0x620ca]
0x00000080 hit0_0 .mode.$PE\u0000\u0000Ld`J.
Original content:
      characteristics : 0x00000096 = characteristics (bitfield) = 0x00000107 : IMAGE_FILE_RELOCS_STRIPPED | IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_LINE_NUMS_STRIPPED | IMAGE_FILE_32BIT_MACHINE
Patching the file…
The new content:
      characteristics : 0x00000096 = characteristics (bitfield) = 0x00000127 : IMAGE_FILE_RELOCS_STRIPPED | IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_LINE_NUMS_STRIPPED | IMAGE_FILE_LARGE_ADDRESS_AWARE | IMAGE_FILE_32BIT_MACHINE

To double check it worked you can also use use objdump -p notepad.exe | grep "large address aware" command and see that it has output.

Hi-Angel
  • 4,933
  • 8
  • 63
  • 86
  • When I run the script I get a `SIGSEGV` error. Is there any way to fix it? – 3174N Apr 02 '21 at 07:32
  • 1
    @3174N it's a bug in your version of radare, under no circumstances it should segfault. I recommend reporting it to the maintainers of your version. As for workarounds: well, in the meanwhile you could [build radare yourself](https://github.com/radareorg/radare2#install--update), I expect it wouldn't have that problem *(at least it didn't last time I tested)*. – Hi-Angel Apr 02 '21 at 08:00
  • 2
    Building radare myself did resolve the issue. Thank you! – 3174N Apr 02 '21 at 08:59