@Override
    public void writeBytes(RandomAccessFile raf, DataConverter dc, PortableExecutable template) 
		throws IOException {
		OptionalHeader optionalHeader = template.getNTHeader().getOptionalHeader();
		DataDirectory [] originalDataDirs = optionalHeader.getDataDirectories();
		if (optionalHeader.getNumberOfRvaAndSizes() <= OptionalHeader.IMAGE_DIRECTORY_ENTRY_DEBUG) {
			return;
		}
		if (originalDataDirs[OptionalHeader.IMAGE_DIRECTORY_ENTRY_DEBUG] == null || 
			originalDataDirs[OptionalHeader.IMAGE_DIRECTORY_ENTRY_DEBUG].getSize() == 0) {
			return;
		}
		DebugDataDirectory templateDDD = (DebugDataDirectory) template.getNTHeader().getOptionalHeader().getDataDirectories()[OptionalHeader.IMAGE_DIRECTORY_ENTRY_DEBUG];
		DebugDirectory [] templateDD = templateDDD.getParser().getDebugDirectories();
		DebugDirectory [] dd = parser.getDebugDirectories();
		for (int i = 0; i < dd.length; i++) {
			dd[i].writeHeader(raf, dc);

			if (dd[i].getSizeOfData() == 0 || dd[i].getPointerToRawData() == 0) {
				continue;
			}

			int ptr = dd[i].getPointerToRawData();
	        if (!ntHeader.checkPointer(ptr)) {
	        	Msg.error(this, "Invalid pointer "+Long.toHexString(ptr));
	        	continue;
	        }
			raf.seek(ptr);
			raf.write(templateDD[i].toBytes(dc));
		}	
	}
@see ghidra.app.util.bin.format.pe.DataDirectory#writeBytes(java.io.RandomAccessFile, ghidra.util.DataConverter, ghidra.app.util.bin.format.pe.PortableExecutable)
Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/DebugDataDirectory.java